You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.go 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package web
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "os"
  9. "path"
  10. "strings"
  11. "code.gitea.io/gitea/modules/httpcache"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/storage"
  15. "code.gitea.io/gitea/modules/util"
  16. "code.gitea.io/gitea/modules/web/routing"
  17. )
  18. func storageHandler(storageSetting *setting.Storage, prefix string, objStore storage.ObjectStorage) http.HandlerFunc {
  19. prefix = strings.Trim(prefix, "/")
  20. funcInfo := routing.GetFuncInfo(storageHandler, prefix)
  21. if storageSetting.MinioConfig.ServeDirect {
  22. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  23. if req.Method != "GET" && req.Method != "HEAD" {
  24. http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
  25. return
  26. }
  27. if !strings.HasPrefix(req.URL.Path, "/"+prefix+"/") {
  28. http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
  29. return
  30. }
  31. routing.UpdateFuncInfo(req.Context(), funcInfo)
  32. rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
  33. rPath = util.PathJoinRelX(rPath)
  34. u, err := objStore.URL(rPath, path.Base(rPath))
  35. if err != nil {
  36. if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
  37. log.Warn("Unable to find %s %s", prefix, rPath)
  38. http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
  39. return
  40. }
  41. log.Error("Error whilst getting URL for %s %s. Error: %v", prefix, rPath, err)
  42. http.Error(w, fmt.Sprintf("Error whilst getting URL for %s %s", prefix, rPath), http.StatusInternalServerError)
  43. return
  44. }
  45. http.Redirect(w, req, u.String(), http.StatusTemporaryRedirect)
  46. })
  47. }
  48. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  49. if req.Method != "GET" && req.Method != "HEAD" {
  50. http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
  51. return
  52. }
  53. if !strings.HasPrefix(req.URL.Path, "/"+prefix+"/") {
  54. http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
  55. return
  56. }
  57. routing.UpdateFuncInfo(req.Context(), funcInfo)
  58. rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
  59. rPath = util.PathJoinRelX(rPath)
  60. if rPath == "" || rPath == "." {
  61. http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
  62. return
  63. }
  64. fi, err := objStore.Stat(rPath)
  65. if err != nil {
  66. if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
  67. log.Warn("Unable to find %s %s", prefix, rPath)
  68. http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
  69. return
  70. }
  71. log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
  72. http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError)
  73. return
  74. }
  75. fr, err := objStore.Open(rPath)
  76. if err != nil {
  77. log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
  78. http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError)
  79. return
  80. }
  81. defer fr.Close()
  82. httpcache.ServeContentWithCacheControl(w, req, path.Base(rPath), fi.ModTime(), fr)
  83. })
  84. }