From 9d6e232d0fd766a05df2e2a538423831ea730bc0 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Tue, 6 Sep 2022 17:48:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=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 | 24 +++- backend/app/dto/file.go | 8 +- backend/app/service/file.go | 47 +++++--- backend/router/ro_file.go | 5 +- frontend/src/api/index.ts | 3 + frontend/src/api/interface/file.ts | 8 +- frontend/src/api/modules/files.ts | 8 +- frontend/src/utils/util.ts | 11 ++ .../views/file-management/download/index.vue | 110 +++++++++++------- frontend/src/views/file-management/index.vue | 99 +++++++++++----- .../src/views/file-management/wget/index.vue | 101 ++++++++++++++++ 11 files changed, 325 insertions(+), 99 deletions(-) create mode 100644 frontend/src/views/file-management/wget/index.vue diff --git a/backend/app/api/v1/file.go b/backend/app/api/v1/file.go index 2f9ffa9f1..ad178dca6 100644 --- a/backend/app/api/v1/file.go +++ b/backend/app/api/v1/file.go @@ -155,7 +155,7 @@ func (b *BaseApi) UploadFiles(c *gin.Context) { helper.SuccessWithMsg(c, fmt.Sprintf("%d files upload success", success)) } -func (b *BaseApi) ChangeName(c *gin.Context) { +func (b *BaseApi) ChangeFileName(c *gin.Context) { var req dto.FileRename if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) @@ -168,20 +168,20 @@ func (b *BaseApi) ChangeName(c *gin.Context) { helper.SuccessWithData(c, nil) } -func (b *BaseApi) Download(c *gin.Context) { - var req dto.FileDownload +func (b *BaseApi) WgetFile(c *gin.Context) { + var req dto.FileWget if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - if err := fileService.Download(req); err != nil { + if err := fileService.Wget(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } helper.SuccessWithData(c, nil) } -func (b *BaseApi) Move(c *gin.Context) { +func (b *BaseApi) MoveFile(c *gin.Context) { var req dto.FileMove if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) @@ -193,3 +193,17 @@ func (b *BaseApi) Move(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +func (b *BaseApi) Download(c *gin.Context) { + var req dto.FileDownload + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + filePath, err := fileService.FileDownload(req) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + c.File(filePath) +} diff --git a/backend/app/dto/file.go b/backend/app/dto/file.go index a72414e28..9c2f1e5a9 100644 --- a/backend/app/dto/file.go +++ b/backend/app/dto/file.go @@ -58,7 +58,7 @@ type FileRename struct { NewName string } -type FileDownload struct { +type FileWget struct { Url string `json:"url" validate:"required"` Path string `json:"path" validate:"required"` Name string `json:"name" validate:"required"` @@ -69,3 +69,9 @@ type FileMove struct { OldPaths []string `json:"oldPaths" validate:"required"` NewPath string `json:"newPath" validate:"required"` } + +type FileDownload struct { + Paths []string `json:"paths" validate:"required"` + Type string `json:"type" validate:"required"` + Name string `json:"name" validate:"required"` +} diff --git a/backend/app/service/file.go b/backend/app/service/file.go index 44f8eca2e..a601b356c 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -9,8 +9,10 @@ import ( "github.com/pkg/errors" "io" "io/fs" + "os" "path/filepath" "strings" + "time" ) type FileService struct { @@ -94,18 +96,18 @@ func (f FileService) DeCompress(c dto.FileDeCompress) error { return fo.Decompress(c.Path, c.Dst, files.CompressType(c.Type)) } -func (f FileService) GetContent(c dto.FileOption) (dto.FileInfo, error) { - info, err := files.NewFileInfo(c.FileOption) +func (f FileService) GetContent(op dto.FileOption) (dto.FileInfo, error) { + info, err := files.NewFileInfo(op.FileOption) if err != nil { return dto.FileInfo{}, err } return dto.FileInfo{*info}, nil } -func (f FileService) SaveContent(c dto.FileEdit) error { +func (f FileService) SaveContent(edit dto.FileEdit) error { info, err := files.NewFileInfo(files.FileOption{ - Path: c.Path, + Path: edit.Path, Expand: false, }) if err != nil { @@ -113,30 +115,30 @@ func (f FileService) SaveContent(c dto.FileEdit) error { } fo := files.NewFileOp() - return fo.WriteFile(c.Path, strings.NewReader(c.Content), info.FileMode) + return fo.WriteFile(edit.Path, strings.NewReader(edit.Content), info.FileMode) } -func (f FileService) ChangeName(c dto.FileRename) error { +func (f FileService) ChangeName(re dto.FileRename) error { fo := files.NewFileOp() - return fo.Rename(c.OldName, c.NewName) + return fo.Rename(re.OldName, re.NewName) } -func (f FileService) Download(c dto.FileDownload) error { +func (f FileService) Wget(w dto.FileWget) error { fo := files.NewFileOp() - return fo.DownloadFile(c.Url, filepath.Join(c.Path, c.Name)) + return fo.DownloadFile(w.Url, filepath.Join(w.Path, w.Name)) } -func (f FileService) MvFile(c dto.FileMove) error { +func (f FileService) MvFile(m dto.FileMove) error { fo := files.NewFileOp() - if c.Type == "cut" { - return fo.Cut(c.OldPaths, c.NewPath) + if m.Type == "cut" { + return fo.Cut(m.OldPaths, m.NewPath) } var errs []error - if c.Type == "copy" { - for _, src := range c.OldPaths { - if err := fo.Copy(src, c.NewPath); err != nil { + if m.Type == "copy" { + for _, src := range m.OldPaths { + if err := fo.Copy(src, m.NewPath); err != nil { errs = append(errs, err) - global.LOG.Errorf("copy file [%s] to [%s] failed, err: %s", src, c.NewPath, err.Error()) + global.LOG.Errorf("copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error()) } } } @@ -152,6 +154,19 @@ func (f FileService) MvFile(c dto.FileMove) error { return nil } +func (f FileService) FileDownload(d dto.FileDownload) (string, error) { + tempPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano())) + if err := os.MkdirAll(tempPath, os.ModePerm); err != nil { + return "", err + } + fo := files.NewFileOp() + if err := fo.Compress(d.Paths, tempPath, d.Name, files.CompressType(d.Type)); err != nil { + return "", err + } + filePath := filepath.Join(tempPath, d.Name) + return filePath, nil +} + func getUuid() string { b := make([]byte, 16) io.ReadFull(rand.Reader, b) diff --git a/backend/router/ro_file.go b/backend/router/ro_file.go index e7a0f5adf..f88c19470 100644 --- a/backend/router/ro_file.go +++ b/backend/router/ro_file.go @@ -25,9 +25,10 @@ func (f *FileRouter) InitFileRouter(Router *gin.RouterGroup) { fileRouter.POST("/content", baseApi.GetContent) fileRouter.POST("/save", baseApi.SaveContent) fileRouter.POST("/upload", baseApi.UploadFiles) - fileRouter.POST("/rename", baseApi.ChangeName) + fileRouter.POST("/rename", baseApi.ChangeFileName) + fileRouter.POST("/wget", baseApi.WgetFile) + fileRouter.POST("/move", baseApi.MoveFile) fileRouter.POST("/download", baseApi.Download) - fileRouter.POST("/move", baseApi.Move) } } diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index 72ecf6222..538a4438a 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -78,6 +78,9 @@ class RequestHttp { delete(url: string, params?: any, _object = {}): Promise> { return this.service.delete(url, { params, ..._object }); } + download(url: string, params?: object, _object = {}): Promise { + return this.service.post(url, params, _object); + } } export default new RequestHttp(config); diff --git a/frontend/src/api/interface/file.ts b/frontend/src/api/interface/file.ts index fc7c5545b..980116848 100644 --- a/frontend/src/api/interface/file.ts +++ b/frontend/src/api/interface/file.ts @@ -71,7 +71,7 @@ export namespace File { newName: string; } - export interface FileDownload { + export interface FileWget { path: string; name: string; url: string; @@ -82,4 +82,10 @@ export namespace File { newPath: string; type: string; } + + export interface FileDownload { + paths: string[]; + name: string; + url: string; + } } diff --git a/frontend/src/api/modules/files.ts b/frontend/src/api/modules/files.ts index 1cffa8e8f..c57c2a6d6 100644 --- a/frontend/src/api/modules/files.ts +++ b/frontend/src/api/modules/files.ts @@ -45,10 +45,14 @@ export const RenameRile = (params: File.FileRename) => { return http.post('files/rename', params); }; -export const DownloadFile = (params: File.FileDownload) => { - return http.post('files/download', params); +export const WgetFile = (params: File.FileWget) => { + return http.post('files/wget', params); }; export const MoveFile = (params: File.FileMove) => { return http.post('files/move', params); }; + +export const DownloadFile = (params: File.FileDownload) => { + return http.download('files/download', params, { responseType: 'blob' }); +}; diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts index f7fe5ba11..50aa07aa5 100644 --- a/frontend/src/utils/util.ts +++ b/frontend/src/utils/util.ts @@ -48,3 +48,14 @@ export function dateFromat(row: number, col: number, dataStr: any) { second = second < 10 ? `0${String(second)}` : second; return `${String(y)}-${String(m)}-${String(d)} ${String(h)}:${String(minute)}:${String(second)}`; } + +export function getRandomStr(e: number): string { + const t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; + const a = t.length; + let n = ''; + + for (let i = 0; i < e; i++) { + n += t.charAt(Math.floor(Math.random() * a)); + } + return n; +} diff --git a/frontend/src/views/file-management/download/index.vue b/frontend/src/views/file-management/download/index.vue index 70bbd5f97..4b53f38dc 100644 --- a/frontend/src/views/file-management/download/index.vue +++ b/frontend/src/views/file-management/download/index.vue @@ -1,71 +1,69 @@ -