gitea源码

base.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 avatarStorageHandler(storageSetting *setting.Storage, prefix string, objStore storage.ObjectStorage) http.HandlerFunc {
  19. prefix = strings.Trim(prefix, "/")
  20. funcInfo := routing.GetFuncInfo(avatarStorageHandler, prefix)
  21. if storageSetting.ServeDirect() {
  22. return func(w http.ResponseWriter, req *http.Request) {
  23. if req.Method != http.MethodGet && req.Method != http.MethodHead {
  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. defer routing.RecordFuncInfo(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), req.Method, nil)
  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 func(w http.ResponseWriter, req *http.Request) {
  49. if req.Method != http.MethodGet && req.Method != http.MethodHead {
  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. defer routing.RecordFuncInfo(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.SetCacheControlInHeader(w.Header(), httpcache.CacheControlForPublicStatic())
  83. http.ServeContent(w, req, path.Base(rPath), fi.ModTime(), fr)
  84. }
  85. }