From 3d79c06f7169860f1418cf8db9f9dd0e4cc7b956 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Sat, 11 Mar 2023 19:55:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=86=E7=89=87=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/file.go | 16 +-- backend/app/api/v1/file_upload.go | 110 +++++++++++++++ backend/app/service/auth.go | 4 +- backend/app/service/file.go | 3 +- backend/i18n/i18n.go | 2 +- backend/router/ro_file.go | 2 +- cmd/server/conf/app.yaml | 1 + .../host/file-management/upload/index.vue | 133 ++++++++++-------- go.mod | 9 +- go.sum | 2 +- 10 files changed, 205 insertions(+), 77 deletions(-) create mode 100644 backend/app/api/v1/file_upload.go diff --git a/backend/app/api/v1/file.go b/backend/app/api/v1/file.go index 1bcc902b4..a289df02d 100644 --- a/backend/app/api/v1/file.go +++ b/backend/app/api/v1/file.go @@ -3,23 +3,21 @@ package v1 import ( "errors" "fmt" - "io/ioutil" - "net/http" - "os" - "path" - "strings" - + "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" + "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/app/dto/response" "github.com/1Panel-dev/1Panel/backend/buserr" - - "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" - "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" websocket2 "github.com/1Panel-dev/1Panel/backend/utils/websocket" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "io/ioutil" + "net/http" + "os" + "path" + "strings" ) // @Tags File diff --git a/backend/app/api/v1/file_upload.go b/backend/app/api/v1/file_upload.go new file mode 100644 index 000000000..4d39c2330 --- /dev/null +++ b/backend/app/api/v1/file_upload.go @@ -0,0 +1,110 @@ +package v1 + +import ( + "fmt" + "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" + "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/files" + "github.com/gin-gonic/gin" + "io/ioutil" + "os" + "path/filepath" + "strconv" +) + +func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int) error { + //fileInfoList, err := ioutil.ReadDir(fileDir) + //if err != nil { + // return err + //} + + targetFile, err := os.Create(filepath.Join(dstDir, fileName)) + if err != nil { + return err + } + defer targetFile.Close() + + for i := 0; i < chunkCount; i++ { + chunkPath := filepath.Join(fileDir, fmt.Sprintf("%s.%d", fileName, i)) + chunkData, err := ioutil.ReadFile(chunkPath) + if err != nil { + return err + } + _, err = targetFile.Write(chunkData) + if err != nil { + return err + } + } + + return files.NewFileOp().DeleteDir(fileDir) +} + +func (b *BaseApi) UploadChunkFiles(c *gin.Context) { + fileForm, err := c.FormFile("chunk") + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + uploadFile, err := fileForm.Open() + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + + chunkIndex, err := strconv.Atoi(c.PostForm("chunkIndex")) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + + chunkCount, err := strconv.Atoi(c.PostForm("chunkCount")) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + + fileOp := files.NewFileOp() + if err := fileOp.CreateDir("uploads", 0755); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + //fileID := uuid.New().String() + filename := c.PostForm("filename") + fileDir := filepath.Join(global.CONF.System.DataDir, "upload", filename) + + os.MkdirAll(fileDir, 0755) + filePath := filepath.Join(fileDir, filename) + + emptyFile, err := os.Create(filePath) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + emptyFile.Close() + + chunkData, err := ioutil.ReadAll(uploadFile) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + + chunkPath := filepath.Join(fileDir, fmt.Sprintf("%s.%d", filename, chunkIndex)) + err = ioutil.WriteFile(chunkPath, chunkData, 0644) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + + if chunkIndex+1 == chunkCount { + err = mergeChunks(filename, fileDir, c.PostForm("path"), chunkCount) + if err != nil { + fmt.Println(err.Error()) + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrAppDelete, err) + return + } + helper.SuccessWithData(c, true) + } else { + return + } +} diff --git a/backend/app/service/auth.go b/backend/app/service/auth.go index dc89b55e5..bc6e64110 100644 --- a/backend/app/service/auth.go +++ b/backend/app/service/auth.go @@ -12,8 +12,8 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/jwt" "github.com/1Panel-dev/1Panel/backend/utils/mfa" "github.com/gin-gonic/gin" + "github.com/google/uuid" "github.com/pkg/errors" - uuid "github.com/satori/go.uuid" ) type AuthService struct{} @@ -128,7 +128,7 @@ func (u *AuthService) generateSession(c *gin.Context, name, authMethod string) ( sID, _ := c.Cookie(constant.SessionName) sessionUser, err := global.SESSION.Get(sID) if err != nil { - sID = uuid.NewV4().String() + sID = uuid.New().String() c.SetCookie(constant.SessionName, sID, 604800, "", "", false, false) err := global.SESSION.Set(sID, sessionUser, lifeTime) if err != nil { diff --git a/backend/app/service/file.go b/backend/app/service/file.go index 0aa6e751d..5f392d98a 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -17,7 +17,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/pkg/errors" - uuid "github.com/satori/go.uuid" ) type FileService struct { @@ -181,7 +180,7 @@ func (f FileService) ChangeName(req request.FileRename) error { func (f FileService) Wget(w request.FileWget) (string, error) { fo := files.NewFileOp() - key := "file-wget-" + uuid.NewV4().String() + key := "file-wget-" + common.GetUuid() return key, fo.DownloadFileWithProcess(w.Url, filepath.Join(w.Path, w.Name), key) } diff --git a/backend/i18n/i18n.go b/backend/i18n/i18n.go index 261a2e1fb..7ff4c6acb 100644 --- a/backend/i18n/i18n.go +++ b/backend/i18n/i18n.go @@ -8,7 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) func GetMsgWithMap(msg string, maps map[string]interface{}) string { diff --git a/backend/router/ro_file.go b/backend/router/ro_file.go index 6e997da45..3b0a1dd80 100644 --- a/backend/router/ro_file.go +++ b/backend/router/ro_file.go @@ -26,7 +26,7 @@ func (f *FileRouter) InitFileRouter(Router *gin.RouterGroup) { fileRouter.POST("/content", baseApi.GetContent) fileRouter.POST("/save", baseApi.SaveContent) fileRouter.POST("/check", baseApi.CheckFile) - fileRouter.POST("/upload", baseApi.UploadFiles) + fileRouter.POST("/upload", baseApi.UploadChunkFiles) fileRouter.POST("/rename", baseApi.ChangeFileName) fileRouter.POST("/wget", baseApi.WgetFile) fileRouter.POST("/move", baseApi.MoveFile) diff --git a/cmd/server/conf/app.yaml b/cmd/server/conf/app.yaml index 228bf4248..b3c96dc79 100644 --- a/cmd/server/conf/app.yaml +++ b/cmd/server/conf/app.yaml @@ -4,6 +4,7 @@ system: mode: dev repo_url: https://resource.fit2cloud.com/1panel/package is_demo: false + port: 9999 log: level: debug diff --git a/frontend/src/views/host/file-management/upload/index.vue b/frontend/src/views/host/file-management/upload/index.vue index ba2d4c957..19a4bcb08 100644 --- a/frontend/src/views/host/file-management/upload/index.vue +++ b/frontend/src/views/host/file-management/upload/index.vue @@ -1,40 +1,39 @@