From 125bb7e2e55bc4b2900ffa2af0cfb88dd18ca355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lie=20Delhaie?= Date: Sat, 4 Jun 2022 13:04:15 +0200 Subject: [PATCH] Fix upload failed when using docker. Fix weird folder name --- upload/upload.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/upload/upload.go b/upload/upload.go index b37307a..bba616f 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -2,6 +2,7 @@ package upload import ( "errors" + "fmt" "github.com/google/uuid" "io" "mime/multipart" @@ -9,6 +10,7 @@ import ( "opensavecloudserver/database" "os" "path" + "strconv" "sync" "time" ) @@ -34,6 +36,31 @@ func init() { }() } +func MoveFile(sourcePath, destPath string) error { + inputFile, err := os.Open(sourcePath) + if err != nil { + return fmt.Errorf("couldn't open source file: %s", err) + } + outputFile, err := os.Create(destPath) + if err != nil { + inputFile.Close() + return fmt.Errorf("couldn't open dest file: %s", err) + } + defer outputFile.Close() + _, err = io.Copy(outputFile, inputFile) + if err != nil { + inputFile.Close() + return fmt.Errorf("writing to output file failed: %s", err) + } + inputFile.Close() + // The copy was successful, so now delete the original file + err = os.Remove(sourcePath) + if err != nil { + return fmt.Errorf("failed removing original file: %s", err) + } + return nil +} + // AskForUpload Create a lock for upload a new revision of a game func AskForUpload(userId, gameId int) (*GameUploadToken, error) { mu.Lock() @@ -66,7 +93,7 @@ func CheckUploadToken(uploadToken string) (int, bool) { } func UploadSave(file multipart.File, game *database.Game) error { - filePath := path.Join(config.Path().Cache, string(rune(game.UserId))) + filePath := path.Join(config.Path().Cache, strconv.Itoa(game.UserId)) if _, err := os.Stat(filePath); err != nil { err = os.Mkdir(filePath, 0766) if err != nil { @@ -91,7 +118,7 @@ func UploadSave(file multipart.File, game *database.Game) error { } func moveToStorage(cachePath string, game *database.Game) error { - filePath := path.Join(config.Path().Storage, string(rune(game.UserId))) + filePath := path.Join(config.Path().Storage, strconv.Itoa(game.UserId)) if _, err := os.Stat(filePath); err != nil { err = os.Mkdir(filePath, 0766) if err != nil { @@ -104,7 +131,7 @@ func moveToStorage(cachePath string, game *database.Game) error { return err } } - if err := os.Rename(cachePath, filePath); err != nil { + if err := MoveFile(cachePath, filePath); err != nil { return err } return nil