MCPcopy Create free account
hub / github.com/ericls/imgdd / makeUploadHandler

Function makeUploadHandler

httpserver/image_handlers.go:32–193  ·  view source on GitHub ↗
(
	conf *HttpServerConfigDef,
	identityManager *IdentityManager,
	storageDefRepo storage.StorageDefRepo,
	imageRepo image.ImageRepo,
	limiter *ratelimit.RateLimiter,
)

Source from the content-addressed store, hash-verified

30}
31
32func makeUploadHandler(
33 conf *HttpServerConfigDef,
34 identityManager *IdentityManager,
35 storageDefRepo storage.StorageDefRepo,
36 imageRepo image.ImageRepo,
37 limiter *ratelimit.RateLimiter,
38) http.HandlerFunc {
39 return func(w http.ResponseWriter, r *http.Request) {
40 if !conf.AllowUpload {
41 http.Error(w, "Image upload is disabled", http.StatusForbidden)
42 return
43 }
44 r.ParseMultipartForm(conf.ImageMaxUploadBytes)
45 _, fileHeader, err := r.FormFile("image")
46 if err != nil {
47 httpLogger.Error().Err(err).Msg("Unable to read file")
48 http.Error(w, "Unable to read file", http.StatusBadRequest)
49 return
50 }
51 src, err := fileHeader.Open()
52 if err != nil {
53 httpLogger.Error().Err(err).Msg("Unable to open file")
54 http.Error(w, "Unable to open file", http.StatusBadRequest)
55 return
56 }
57 defer src.Close()
58
59 imgBytes, _ := io.ReadAll(src)
60 exifRemovedBytes, err := utils.MaybeRemoveExif(imgBytes)
61 if err != nil || len(exifRemovedBytes) == 0 {
62 exifRemovedBytes = imgBytes
63 }
64
65 bytesLength := len(exifRemovedBytes)
66 if bytesLength == 0 {
67 http.Error(w, "Empty file", http.StatusBadRequest)
68 return
69 }
70
71 orgUser := identity.GetCurrentOrganizationUser(identityManager.ContextUserManager, r.Context())
72
73 baseLimit := conf.ImageMaxUploadBytes
74 if conf.GuestImageMaxUploadBytes > 0 {
75 baseLimit = conf.GuestImageMaxUploadBytes
76 }
77 effectiveLimit := baseLimit
78 if orgUser != nil && orgUser.UploadLimitBytes != nil {
79 effectiveLimit = *orgUser.UploadLimitBytes
80 if effectiveLimit > conf.ImageMaxUploadBytes {
81 effectiveLimit = conf.ImageMaxUploadBytes
82 }
83 }
84 if int64(bytesLength) > effectiveLimit {
85 http.Error(w, "File too large", http.StatusBadRequest)
86 return
87 }
88
89 declaredMimeType := utils.GetMimeTypeFromFilename(fileHeader.Filename)

Calls 15

GetURLMethod · 0.95
MaybeRemoveExifFunction · 0.92
GetMimeTypeFromFilenameFunction · 0.92
DetectMIMETypeFunction · 0.92
MakeImageCheckerFunction · 0.92
CheckAllFunction · 0.92
GetStorageFunction · 0.92
GetImageDimensionsFunction · 0.92
ExtractIPFunction · 0.85
IsSecureFunction · 0.85
IsRateLimitedMethod · 0.80