From 54a115ae8925d8f20c286aab84d780f6cfed46bd Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Wed, 17 Jan 2024 17:32:19 +0800 Subject: [PATCH] fix: fix upload folder is wrong (#1611) --- route/v2/file.go | 2 ++ service/file_upload.go | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/route/v2/file.go b/route/v2/file.go index a5cb6df..c89f15e 100644 --- a/route/v2/file.go +++ b/route/v2/file.go @@ -59,6 +59,7 @@ func (c *CasaOS) PostUploadFile(ctx echo.Context) error { identifier := ctx.FormValue("identifier") fileName := ctx.FormValue("filename") + relativePath := ctx.FormValue("relativePath") bin, err := ctx.FormFile("file") if err != nil { @@ -74,6 +75,7 @@ func (c *CasaOS) PostUploadFile(ctx echo.Context) error { totalChunks, totalSize, identifier, + relativePath, fileName, bin, ) diff --git a/service/file_upload.go b/service/file_upload.go index 3e5acb6..7654c2c 100644 --- a/service/file_upload.go +++ b/service/file_upload.go @@ -5,6 +5,7 @@ import ( "io" "mime/multipart" "os" + "path/filepath" "sync" "github.com/labstack/echo/v4" @@ -63,6 +64,7 @@ func (s *FileUploadService) UploadFile( totalChunks int64, totalSize int64, identifier string, + relativePath string, fileName string, bin *multipart.FileHeader, ) error { @@ -70,7 +72,15 @@ func (s *FileUploadService) UploadFile( fileInfoTemp, ok := s.uploadStatus.Load(identifier) var fileInfo *FileInfo - file, err := os.OpenFile(path+"/"+fileName+".tmp", os.O_WRONLY|os.O_CREATE, 0644) + if relativePath != fileName { + // uploaded file is folder + folderPath := filepath.Dir(path + "/" + relativePath) + if _, err := os.Stat(folderPath); os.IsNotExist(err) { + os.MkdirAll(folderPath, os.ModePerm) + } + } + + file, err := os.OpenFile(path+"/"+relativePath+".tmp", os.O_WRONLY|os.O_CREATE, 0644) if err != nil { s.lock.Unlock() return err