Skip to content

Commit

Permalink
fix: ensure thumbnail is written into disk
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Dec 3, 2024
1 parent ce3d9c1 commit f3d91f8
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions services/thumbnails/pkg/thumbnail/storage/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,28 @@ func (s FileSystem) Put(key string, img []byte) error {
if err != nil {
return errors.Wrapf(err, "could not create temporary file for \"%s\"", key)
}

_, writeErr := f.Write(img) // write the thumbnail in the temporary file
f.Close() // close the file regardless of the error
defer f.Close()

// if there was a problem writing, remove the temporary file
if writeErr != nil {
if _, writeErr := f.Write(img); writeErr != nil {
if remErr := os.Remove(f.Name()); remErr != nil {
return errors.Wrapf(remErr, "could not cleanup temporary file for \"%s\"", key)
}
return errors.Wrapf(writeErr, "could not write to temporary file for \"%s\"", key)
}

// if there wasn't a problem, ensure the data is written into disk
if synErr := f.Sync(); synErr != nil {
return errors.Wrapf(synErr, "could not sync temporary file data into the disk for \"%s\"", key)
}

// rename the temporary file to the final file
if renErr := os.Rename(f.Name(), imgPath); renErr != nil {
// if we couldn't rename, remove the temporary file
if remErr := os.Remove(f.Name()); remErr != nil {
return errors.Wrapf(remErr, "rename failed and could not cleanup temporary file for \"%s\"", key)
}
return errors.Wrapf(err, "could not rename temporary file to \"%s\"", key)
return errors.Wrapf(renErr, "could not rename temporary file to \"%s\"", key)
}
}

Expand Down

0 comments on commit f3d91f8

Please sign in to comment.