From 1dc20914ac7e8004477a5884799bace9fe1f1a1a Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 25 Aug 2022 18:48:03 +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=A0=E9=99=A4=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 | 14 +++++++ backend/app/dto/file.go | 5 +++ backend/app/service/file.go | 9 +++++ backend/constant/errs.go | 2 +- backend/router/ro_file.go | 1 + backend/utils/files/file_op.go | 8 ++++ frontend/src/api/interface/file.ts | 5 +++ frontend/src/api/modules/files.ts | 4 ++ frontend/src/views/file-management/index.vue | 39 ++++++++++++++++---- 9 files changed, 79 insertions(+), 8 deletions(-) diff --git a/backend/app/api/v1/file.go b/backend/app/api/v1/file.go index c16f64c3a..42ae62e09 100644 --- a/backend/app/api/v1/file.go +++ b/backend/app/api/v1/file.go @@ -48,3 +48,17 @@ func (b *BaseApi) CreateFile(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +func (b *BaseApi) DeleteFile(c *gin.Context) { + var req dto.FileDelete + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + err := fileService.Delete(req) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, nil) +} diff --git a/backend/app/dto/file.go b/backend/app/dto/file.go index 2cd7b3d4b..befadbc6b 100644 --- a/backend/app/dto/file.go +++ b/backend/app/dto/file.go @@ -25,3 +25,8 @@ type FileCreate struct { IsDir bool Mode int64 } + +type FileDelete struct { + Path string + IsDir bool +} diff --git a/backend/app/service/file.go b/backend/app/service/file.go index f15d6530b..0e6231650 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -55,6 +55,15 @@ func (f FileService) Create(op dto.FileCreate) error { return nil } +func (f FileService) Delete(op dto.FileDelete) error { + fo := files.NewFileOp() + if op.IsDir { + return fo.DeleteDir(op.Path) + } else { + return fo.DeleteFile(op.Path) + } +} + func getUuid() string { b := make([]byte, 16) io.ReadFull(rand.Reader, b) diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 83ef571ec..73e23ad6e 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -32,5 +32,5 @@ var ( ErrTypeInvalidParams = "ErrInvalidParams" ErrTypeToken = "ErrToken" ErrTypeTokenTimeOut = "ErrTokenTimeOut" - ErrTypeNotLogin = "ErrNotLogin" + ErrTypeNotLogin = "ErrTypeNotLogin" ) diff --git a/backend/router/ro_file.go b/backend/router/ro_file.go index 27ea97518..71ed048f0 100644 --- a/backend/router/ro_file.go +++ b/backend/router/ro_file.go @@ -18,6 +18,7 @@ func (f *FileRouter) InitFileRouter(Router *gin.RouterGroup) { fileRouter.POST("/search", baseApi.ListFiles) fileRouter.POST("/tree", baseApi.GetFileTree) fileRouter.POST("", baseApi.CreateFile) + fileRouter.POST("/del", baseApi.DeleteFile) } } diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 4d743a5ec..1f10b5b93 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -22,6 +22,14 @@ func (f FileOp) CreateDir(dst string, mode fs.FileMode) error { return f.Fs.MkdirAll(dst, mode) } +func (f FileOp) DeleteDir(dst string) error { + return f.Fs.RemoveAll(dst) +} + +func (f FileOp) DeleteFile(dst string) error { + return f.Fs.Remove(dst) +} + func (f FileOp) WriteFile(dst string, in io.Reader, mode fs.FileMode) error { dir, _ := path.Split(dst) if err := f.Fs.MkdirAll(dir, mode); err != nil { diff --git a/frontend/src/api/interface/file.ts b/frontend/src/api/interface/file.ts index 89f6cfae6..15af96bc6 100644 --- a/frontend/src/api/interface/file.ts +++ b/frontend/src/api/interface/file.ts @@ -35,4 +35,9 @@ export namespace File { isDir: Boolean; mode: number; } + + export interface FileDelete { + path: string; + isDir: Boolean; + } } diff --git a/frontend/src/api/modules/files.ts b/frontend/src/api/modules/files.ts index 357140f5b..4042c5095 100644 --- a/frontend/src/api/modules/files.ts +++ b/frontend/src/api/modules/files.ts @@ -12,3 +12,7 @@ export const GetFilesTree = (params: File.ReqFile) => { export const CreateFile = (form: File.FileCreate) => { return http.post('files', form); }; + +export const DeleteFile = (form: File.FileDelete) => { + return http.post('files/del', form); +}; diff --git a/frontend/src/views/file-management/index.vue b/frontend/src/views/file-management/index.vue index 50f141dc1..02fb036c5 100644 --- a/frontend/src/views/file-management/index.vue +++ b/frontend/src/views/file-management/index.vue @@ -11,6 +11,7 @@ v-loading="treeLoading" node-key="id" :default-expanded-keys="expandKeys" + @node-click="clickNode" > - +