From 8b3d84d667dd55f1936d52f5feacf6ab9bbd1bc0 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Mon, 13 Mar 2023 16:44:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=8F=B3=E4=B8=8B?= =?UTF-8?q?=E8=A7=92=E6=A3=80=E6=9F=A5=E6=9B=B4=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/file.go | 102 +++++++++++++++++ .../components/app-layout/footer/index.vue | 6 +- .../src/components/system-upgrade/index.vue | 92 +++++++++++++++ frontend/src/views/setting/about/index.vue | 105 ++++-------------- 4 files changed, 223 insertions(+), 82 deletions(-) create mode 100644 frontend/src/components/system-upgrade/index.vue diff --git a/backend/app/api/v1/file.go b/backend/app/api/v1/file.go index a289df02d..e46a62271 100644 --- a/backend/app/api/v1/file.go +++ b/backend/app/api/v1/file.go @@ -10,6 +10,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/files" websocket2 "github.com/1Panel-dev/1Panel/backend/utils/websocket" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" @@ -17,6 +18,8 @@ import ( "net/http" "os" "path" + "path/filepath" + "strconv" "strings" ) @@ -492,6 +495,105 @@ func (b *BaseApi) LoadFromFile(c *gin.Context) { helper.SuccessWithData(c, string(content)) } +func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int) error { + 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) +} + +// @Tags File +// @Summary ChunkUpload file +// @Description 分片上传文件 +// @Param file formData file true "request" +// @Success 200 +// @Security ApiKeyAuth +// @Router /files/chunkupload [post] +// @x-panel-log {"bodyKeys":["path"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"上传文件 [path]","formatEN":"Upload file [path]"} +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 + } +} + var wsUpgrade = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true diff --git a/frontend/src/components/app-layout/footer/index.vue b/frontend/src/components/app-layout/footer/index.vue index 76c5e73d6..f60ac7ac6 100644 --- a/frontend/src/components/app-layout/footer/index.vue +++ b/frontend/src/components/app-layout/footer/index.vue @@ -7,11 +7,15 @@ 杭州飞致云信息科技有限公司 + +