From ccc18d716f16a7ef1775d30982e2ba7b5ff159a6 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Sun, 6 Apr 2025 15:08:06 +0200 Subject: [PATCH] fix: use UUID for temporary file names --- backend/internal/utils/file_util.go | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/backend/internal/utils/file_util.go b/backend/internal/utils/file_util.go index 04ab8547..94221de1 100644 --- a/backend/internal/utils/file_util.go +++ b/backend/internal/utils/file_util.go @@ -3,14 +3,12 @@ package utils import ( "errors" "fmt" - "hash/crc64" "io" "mime/multipart" "os" "path/filepath" - "strconv" - "time" + "github.com/google/uuid" "github.com/pocket-id/pocket-id/backend/resources" ) @@ -80,22 +78,7 @@ func SaveFile(file *multipart.FileHeader, dst string) error { // SaveFileStream saves a stream to a file. func SaveFileStream(r io.Reader, dstFileName string) error { // Our strategy is to save to a separate file and then rename it to override the original file - // First, get a temp file name that doesn't exist already - var tmpFileName string - var i int64 - for { - seed := strconv.FormatInt(time.Now().UnixNano()+i, 10) - suffix := crc64.Checksum([]byte(dstFileName+seed), crc64.MakeTable(crc64.ISO)) - tmpFileName = dstFileName + "." + strconv.FormatUint(suffix, 10) - exists, err := FileExists(tmpFileName) - if err != nil { - return fmt.Errorf("failed to check if file '%s' exists: %w", tmpFileName, err) - } - if !exists { - break - } - i++ - } + tmpFileName := dstFileName + "." + uuid.NewString() + "-tmp" // Write to the temporary file tmpFile, err := os.Create(tmpFileName)