diff --git a/http/resource.go b/http/resource.go index 9fe4caa6..0a8da271 100644 --- a/http/resource.go +++ b/http/resource.go @@ -280,6 +280,12 @@ func writeFile(afs afero.Fs, dst string, in io.Reader, fileMode, dirMode fs.File return nil, err } + // Sync the file to ensure all data is written to storage. + // to prevent file corruption. + if err := file.Sync(); err != nil { + return nil, err + } + // Gets the info about the file. info, err := file.Stat() if err != nil { diff --git a/http/tus_handlers.go b/http/tus_handlers.go index d11920ae..498d776f 100644 --- a/http/tus_handlers.go +++ b/http/tus_handlers.go @@ -256,6 +256,12 @@ func tusPatchHandler() handleFunc { return http.StatusInternalServerError, fmt.Errorf("could not write to file: %w", err) } + // Sync the file to ensure all data is written to storage + // to prevent file corruption. + if err := openFile.Sync(); err != nil { + return http.StatusInternalServerError, fmt.Errorf("could not sync file: %w", err) + } + newOffset := uploadOffset + bytesWritten w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10))