Skip to content

Commit f45e869

Browse files
committed
Better error handling for failed chunks, allocateChunk also returns error if given size is too big
1 parent dc93472 commit f45e869

File tree

4 files changed

+6
-1
lines changed

4 files changed

+6
-1
lines changed

internal/storage/FileServing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func NewFileFromChunk(chunkId string, fileHeader chunking.FileHeader, userId int
165165
defer file.Close()
166166
err = validateChunkInfo(file, fileHeader)
167167
if err != nil {
168+
_ = chunking.DeleteChunk(chunkId)
168169
return models.File{}, err
169170
}
170171

internal/storage/chunking/Chunking.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ func allocateFile(info ChunkInfo) error {
214214
if FileExists(info.UUID) {
215215
return nil
216216
}
217+
maxSizeBytes := int64(configuration.Get().MaxFileSizeMB) * 1024 * 1024
218+
if info.TotalFilesizeBytes > maxSizeBytes {
219+
return errors.New("declared file size exceeds the maximum allowed size")
220+
}
217221
enoughSpace, err := isEnoughSpace(info.TotalFilesizeBytes)
218222
if err != nil {
219223
return err

internal/webserver/api/Api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ func apiChunkComplete(w http.ResponseWriter, r requestParser, user models.User)
507507
func doBlockingPartCompleteChunk(w http.ResponseWriter, uuid string, fileHeader chunking.FileHeader, user models.User, uploadParameters models.UploadParameters) {
508508
file, err := fileupload.CompleteChunk(uuid, fileHeader, user.Id, uploadParameters)
509509
if err != nil {
510+
_ = chunking.DeleteChunk(uuid)
510511
sendError(w, http.StatusBadRequest, errorcodes.UnspecifiedError, err.Error())
511512
return
512513
}

internal/webserver/api/routing.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ type paramE2eStore struct {
584584
func (p *paramE2eStore) ProcessParameter(r *http.Request) error {
585585
const maxBodySize = 5 * 1024 * 1024 // 5MB in bytes
586586
bodyReader := http.MaxBytesReader(nil, r.Body, maxBodySize)
587-
defer bodyReader.Close()
588587

589588
type expectedInput struct {
590589
Content string `json:"content"`

0 commit comments

Comments
 (0)