Skip to content

Commit

Permalink
fix: fix upload folder fail (#1614)
Browse files Browse the repository at this point in the history
Signed-off-by: CorrectRoadH <[email protected]>
  • Loading branch information
CorrectRoadH committed Jan 18, 2024
1 parent 29d16d1 commit fffdc7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion route/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func InitV2Router() http.Handler {
// jump validate when upload file
// because file upload can't pass validate
// issue: https://github.com/deepmap/oapi-codegen/issues/514
return strings.Contains(c.Request().URL.Path, "file/upload")
return strings.Contains(c.Request().Header[echo.HeaderContentType][0], "multipart/form-data")
},
Options: openapi3filter.Options{AuthenticationFunc: openapi3filter.NoopAuthenticationFunc},
}))
Expand Down
23 changes: 18 additions & 5 deletions service/file_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"path/filepath"
"sync"

"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)

type FileInfo struct {
Expand Down Expand Up @@ -76,7 +78,11 @@ func (s *FileUploadService) UploadFile(
// uploaded file is folder
folderPath := filepath.Dir(path + "/" + relativePath)
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
os.MkdirAll(folderPath, os.ModePerm)
err := os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
s.lock.Unlock()
return err
}
}
}

Expand Down Expand Up @@ -124,8 +130,7 @@ func (s *FileUploadService) UploadFile(
}
defer src.Close()

buf := make([]byte, int(currentChunkSize))
_, err = io.CopyBuffer(file, src, buf)
_, err = io.Copy(file, src)

if err != nil {
fmt.Println(err)
Expand All @@ -142,9 +147,17 @@ func (s *FileUploadService) UploadFile(

// handle file after write all chunk
if fileInfo.uploadedChunkNum == totalChunks {
file.Close()
os.Rename(path+"/"+fileName+".tmp", path+"/"+fileName)
err := file.Close()
if err != nil {
s.lock.Unlock()
logger.Error("close file error: ", zap.Error(err))
}

err = os.Rename(path+"/"+relativePath+".tmp", path+"/"+relativePath)
if err != nil {
s.lock.Unlock()
logger.Error("rename file error: ", zap.Error(err))
}
// remove upload status info after upload complete
s.uploadStatus.Delete(identifier)
}
Expand Down

0 comments on commit fffdc7f

Please sign in to comment.