Skip to content

Commit fffdc7f

Browse files
authored
fix: fix upload folder fail (#1614)
Signed-off-by: CorrectRoadH <[email protected]>
1 parent 29d16d1 commit fffdc7f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

route/v2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func InitV2Router() http.Handler {
120120
// jump validate when upload file
121121
// because file upload can't pass validate
122122
// issue: https://github.com/deepmap/oapi-codegen/issues/514
123-
return strings.Contains(c.Request().URL.Path, "file/upload")
123+
return strings.Contains(c.Request().Header[echo.HeaderContentType][0], "multipart/form-data")
124124
},
125125
Options: openapi3filter.Options{AuthenticationFunc: openapi3filter.NoopAuthenticationFunc},
126126
}))

service/file_upload.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"path/filepath"
99
"sync"
1010

11+
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
1112
"github.com/labstack/echo/v4"
13+
"go.uber.org/zap"
1214
)
1315

1416
type FileInfo struct {
@@ -76,7 +78,11 @@ func (s *FileUploadService) UploadFile(
7678
// uploaded file is folder
7779
folderPath := filepath.Dir(path + "/" + relativePath)
7880
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
79-
os.MkdirAll(folderPath, os.ModePerm)
81+
err := os.MkdirAll(folderPath, os.ModePerm)
82+
if err != nil {
83+
s.lock.Unlock()
84+
return err
85+
}
8086
}
8187
}
8288

@@ -124,8 +130,7 @@ func (s *FileUploadService) UploadFile(
124130
}
125131
defer src.Close()
126132

127-
buf := make([]byte, int(currentChunkSize))
128-
_, err = io.CopyBuffer(file, src, buf)
133+
_, err = io.Copy(file, src)
129134

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

143148
// handle file after write all chunk
144149
if fileInfo.uploadedChunkNum == totalChunks {
145-
file.Close()
146-
os.Rename(path+"/"+fileName+".tmp", path+"/"+fileName)
150+
err := file.Close()
151+
if err != nil {
152+
s.lock.Unlock()
153+
logger.Error("close file error: ", zap.Error(err))
154+
}
147155

156+
err = os.Rename(path+"/"+relativePath+".tmp", path+"/"+relativePath)
157+
if err != nil {
158+
s.lock.Unlock()
159+
logger.Error("rename file error: ", zap.Error(err))
160+
}
148161
// remove upload status info after upload complete
149162
s.uploadStatus.Delete(identifier)
150163
}

0 commit comments

Comments
 (0)