mirror of
https://github.com/pocket-id/pocket-id.git
synced 2025-12-11 07:32:57 +03:00
[PR #372] [MERGED] refactor: use atomic renames for uploaded files #853
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/pocket-id/pocket-id/pull/372
Author: @ItalyPaleAle
Created: 3/21/2025
Status: ✅ Merged
Merged: 3/23/2025
Merged by: @stonith404
Base:
main← Head:fix-file-upload📝 Commits (8)
66eec1dfix: use atomic renames for uploaded files0f93c7cAvoid buffering encoded images in-memory8c51ac4Small lintsc5b7177Update backend/internal/service/user_service.go7864ec4Use crc6476be054Merge branch 'main' into fix-file-uploada3e7ad7Merge branch 'main' into fix-file-upload7376481Merge branch 'main' into fix-file-upload📊 Changes
4 files changed (+106 additions, -35 deletions)
View changed files
📝
backend/internal/model/app_config.go(+9 -0)📝
backend/internal/service/user_service.go(+12 -17)📝
backend/internal/utils/file_util.go(+66 -5)📝
backend/internal/utils/image/profile_picture.go(+19 -13)📄 Description
This PR fixes a bug in how uploaded files are handled, by using temporary files that are then atomically renamed to the final destination.
This ensures that in case of errors, there is no corrupted file saved on disk. For example, imagine scenarios such as pocket id crashing while writing the file, or a power failure, or more simply interrupted uploads... Because the stream was opened to the original file, only partial data would be written to the file.
The typical solution to this problem is to write to a temporary file first, and then perform a rename. In most OS's, renaming is an atomic operation.
Note that renaming must be done in the same filesystem, so the temporary file needs to be in the same directory. We can't use
/tmpbecause that's often on a separate filesystemPS: Also updates
CreateProfilePictureto avoid buffering the entire encoded image in-memory🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.