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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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) func(next http.Handler) http.Handler {
  19. prefix = strings.Trim(prefix, "/")
  20. funcInfo := routing.GetFuncInfo(storageHandler, prefix)
  21. return func(next http.Handler) http.Handler {
  22. if storageSetting.MinioConfig.ServeDirect {
  23. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  24. if req.Method != "GET" && req.Method != "HEAD" {
  25. next.ServeHTTP(w, req)
  26. return
  27. }
  28. if !strings.HasPrefix(req.URL.Path, "/"+prefix+"/") {
  29. next.ServeHTTP(w, req)
  30. return
  31. }
  32. routing.UpdateFuncInfo(req.Context(), funcInfo)
  33. rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
  34. rPath = util.PathJoinRelX(rPath)
  35. u, err := objStore.URL(rPath, path.Base(rPath))
  36. if err != nil {
  37. if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
  38. log.Warn("Unable to find %s %s", prefix, rPath)
  39. http.Error(w, "file not found", http.StatusNotFound)
  40. return
  41. }
  42. log.Error("Error whilst getting URL for %s %s. Error: %v", prefix, rPath, err)
  43. http.Error(w, fmt.Sprintf("Error whilst getting URL for %s %s", prefix, rPath), http.StatusInternalServerError)
  44. return
  45. }
  46. http.Redirect(w, req, u.String(), http.StatusTemporaryRedirect)
  47. })
  48. }
  49. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  50. if req.Method != "GET" && req.Method != "HEAD" {
  51. next.ServeHTTP(w, req)
  52. return
  53. }
  54. if !strings.HasPrefix(req.URL.Path, "/"+prefix+"/") {
  55. next.ServeHTTP(w, req)
  56. return
  57. }
  58. routing.UpdateFuncInfo(req.Context(), funcInfo)
  59. rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
  60. rPath = util.PathJoinRelX(rPath)
  61. if rPath == "" || rPath == "." {
  62. http.Error(w, "file not found", http.StatusNotFound)
  63. return
  64. }
  65. fi, err := objStore.Stat(rPath)
  66. if err != nil {
  67. if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
  68. log.Warn("Unable to find %s %s", prefix, rPath)
  69. http.Error(w, "file not found", http.StatusNotFound)
  70. return
  71. }
  72. log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
  73. http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError)
  74. return
  75. }
  76. fr, err := objStore.Open(rPath)
  77. if err != nil {
  78. log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
  79. http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError)
  80. return
  81. }
  82. defer fr.Close()
  83. httpcache.ServeContentWithCacheControl(w, req, path.Base(rPath), fi.ModTime(), fr)
  84. })
  85. }
  86. }