aboutsummaryrefslogtreecommitdiff
path: root/mediaapi
diff options
context:
space:
mode:
authorFero <ferologics@users.noreply.github.com>2021-04-14 11:53:24 +0200
committerGitHub <noreply@github.com>2021-04-14 10:53:24 +0100
commit653e30619cebdc6b0f234a5ab872abddfd311656 (patch)
treec390adbd1a9b2472748cd19b42b33eb1781d6003 /mediaapi
parent080ae6a8291e2bafb569eb9f163f67f85275d678 (diff)
Remove the Content-Length requirement on upload request (#1831)
* Remove the Content-Length requirement * Make sure that the file size does not exceed max content size * Address review comment - universally check if temp file size exceeds max file size
Diffstat (limited to 'mediaapi')
-rw-r--r--mediaapi/routing/upload.go24
1 files changed, 14 insertions, 10 deletions
diff --git a/mediaapi/routing/upload.go b/mediaapi/routing/upload.go
index 2c575374..a38b56e0 100644
--- a/mediaapi/routing/upload.go
+++ b/mediaapi/routing/upload.go
@@ -158,6 +158,12 @@ func (r *uploadRequest) doUpload(
}
}
+ // Check if temp file size exceeds max file size configuration
+ if bytesWritten > types.FileSizeBytes(*cfg.MaxFileSizeBytes) {
+ fileutils.RemoveDir(tmpDir, r.Logger) // delete temp file
+ return requestEntityTooLargeJSONResponse(*cfg.MaxFileSizeBytes)
+ }
+
// Look up the media by the file hash. If we already have the file but under a
// different media ID then we won't upload the file again - instead we'll just
// add a new metadata entry that refers to the same file.
@@ -219,19 +225,17 @@ func (r *uploadRequest) doUpload(
)
}
+func requestEntityTooLargeJSONResponse(maxFileSizeBytes config.FileSizeBytes) *util.JSONResponse {
+ return &util.JSONResponse{
+ Code: http.StatusRequestEntityTooLarge,
+ JSON: jsonerror.Unknown(fmt.Sprintf("HTTP Content-Length is greater than the maximum allowed upload size (%v).", maxFileSizeBytes)),
+ }
+}
+
// Validate validates the uploadRequest fields
func (r *uploadRequest) Validate(maxFileSizeBytes config.FileSizeBytes) *util.JSONResponse {
- if r.MediaMetadata.FileSizeBytes < 1 {
- return &util.JSONResponse{
- Code: http.StatusLengthRequired,
- JSON: jsonerror.Unknown("HTTP Content-Length request header must be greater than zero."),
- }
- }
if maxFileSizeBytes > 0 && r.MediaMetadata.FileSizeBytes > types.FileSizeBytes(maxFileSizeBytes) {
- return &util.JSONResponse{
- Code: http.StatusRequestEntityTooLarge,
- JSON: jsonerror.Unknown(fmt.Sprintf("HTTP Content-Length is greater than the maximum allowed upload size (%v).", maxFileSizeBytes)),
- }
+ return requestEntityTooLargeJSONResponse(maxFileSizeBytes)
}
// TODO: Check if the Content-Type is a valid type?
if r.MediaMetadata.ContentType == "" {