| 30 | } |
| 31 | |
| 32 | func 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) |