Skip to content

Commit

Permalink
filestore: Close files correctly when concatenating uploads (#1239)
Browse files Browse the repository at this point in the history
* filestore: Add a method to ensure merged chunk files are properly closed

* Remove unused `ctx` argument

* Handle errors from `src.Close`

---------

Co-authored-by: Marius Kleidl <[email protected]>
  • Loading branch information
nervtech and Acconut authored Jan 16, 2025
1 parent d69efe0 commit 7178845
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pkg/filestore/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,28 @@ func (upload *fileUpload) ConcatUploads(ctx context.Context, uploads []handler.U
}()

for _, partialUpload := range uploads {
fileUpload := partialUpload.(*fileUpload)

src, err := os.Open(fileUpload.binPath)
if err != nil {
return err
}

if _, err := io.Copy(file, src); err != nil {
if err := partialUpload.(*fileUpload).appendTo(file); err != nil {
return err
}
}

return
}

func (upload *fileUpload) appendTo(file *os.File) error {
src, err := os.Open(upload.binPath)
if err != nil {
return err
}

if _, err := io.Copy(file, src); err != nil {
src.Close()
return err
}

return src.Close()
}

func (upload *fileUpload) DeclareLength(ctx context.Context, length int64) error {
upload.info.Size = length
upload.info.SizeIsDeferred = false
Expand Down

0 comments on commit 7178845

Please sign in to comment.