From 1b0660f4a35eb01d8795c8fceca32e9bde1514dd Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 31 Aug 2022 16:00:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=BD=AF=E9=93=BE=E6=8E=A5=E6=96=87=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/file.go | 11 ++-- backend/app/service/file.go | 10 +-- backend/utils/files/file_op.go | 16 +++++ backend/utils/files/fileinfo.go | 7 +++ backend/utils/files/utils.go | 8 +++ frontend/src/api/interface/file.ts | 4 ++ frontend/src/lang/modules/zh.ts | 4 ++ .../views/file-management/create/index.vue | 62 +++++++++++++------ frontend/src/views/file-management/index.vue | 3 +- 9 files changed, 98 insertions(+), 27 deletions(-) diff --git a/backend/app/dto/file.go b/backend/app/dto/file.go index aefebed12..b211eb912 100644 --- a/backend/app/dto/file.go +++ b/backend/app/dto/file.go @@ -20,10 +20,13 @@ type FileTree struct { } type FileCreate struct { - Path string - Content string - IsDir bool - Mode int64 + Path string + Content string + IsDir bool + Mode int64 + IsLink bool + IsSymlink bool + LinkPath string } type FileDelete struct { diff --git a/backend/app/service/file.go b/backend/app/service/file.go index 1afae647a..eb4bc5387 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -50,16 +50,18 @@ func (f FileService) GetFileTree(op dto.FileOption) ([]dto.FileTree, error) { func (f FileService) Create(op dto.FileCreate) error { fo := files.NewFileOp() - if fo.Stat(op.Path) { return errors.New("file is exist") } - if op.IsDir { return fo.CreateDir(op.Path, fs.FileMode(op.Mode)) + } else { + if op.IsLink { + return fo.LinkFile(op.LinkPath, op.Path, op.IsSymlink) + } else { + return fo.CreateFile(op.Path) + } } - - return nil } func (f FileService) Delete(op dto.FileDelete) error { diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 4c62349d3..40d6fd431 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -25,6 +25,22 @@ func (f FileOp) CreateDir(dst string, mode fs.FileMode) error { return f.Fs.MkdirAll(dst, mode) } +func (f FileOp) CreateFile(dst string) error { + if _, err := f.Fs.Create(dst); err != nil { + return err + } + return nil +} + +func (f FileOp) LinkFile(source string, dst string, isSymlink bool) error { + if isSymlink { + osFs := afero.OsFs{} + return osFs.SymlinkIfPossible(source, dst) + } else { + return os.Link(source, dst) + } +} + func (f FileOp) DeleteDir(dst string) error { return f.Fs.RemoveAll(dst) } diff --git a/backend/utils/files/fileinfo.go b/backend/utils/files/fileinfo.go index e8448af2d..75e293a88 100644 --- a/backend/utils/files/fileinfo.go +++ b/backend/utils/files/fileinfo.go @@ -21,6 +21,7 @@ type FileInfo struct { Size int64 `json:"size"` IsDir bool `json:"isDir"` IsSymlink bool `json:"isSymlink"` + LinkPath string `json:"linkPath"` Type string `json:"type"` Mode string `json:"mode"` MimeType string `json:"mimeType"` @@ -59,6 +60,9 @@ func NewFileInfo(op FileOption) (*FileInfo, error) { Group: GetGroup(info.Sys().(*syscall.Stat_t).Gid), MimeType: GetMimeType(op.Path), } + if file.IsSymlink { + file.LinkPath = GetSymlink(op.Path) + } if op.Expand { if file.IsDir { if err := file.listChildren(); err != nil { @@ -111,6 +115,9 @@ func (f *FileInfo) listChildren() error { Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid), MimeType: GetMimeType(fPath), } + if isSymlink { + file.LinkPath = GetSymlink(fPath) + } if isInvalidLink { file.Type = "invalid_link" diff --git a/backend/utils/files/utils.go b/backend/utils/files/utils.go index 29793cb36..a1d8c22ea 100644 --- a/backend/utils/files/utils.go +++ b/backend/utils/files/utils.go @@ -34,3 +34,11 @@ func GetMimeType(path string) string { } return mime.String() } + +func GetSymlink(path string) string { + linkPath, err := os.Readlink(path) + if err != nil { + return "" + } + return linkPath +} diff --git a/frontend/src/api/interface/file.ts b/frontend/src/api/interface/file.ts index d2f2c5245..fe1b71d12 100644 --- a/frontend/src/api/interface/file.ts +++ b/frontend/src/api/interface/file.ts @@ -9,6 +9,7 @@ export namespace File { size: number; isDir: boolean; isSymlink: boolean; + linkPath: boolean; type: string; updateTime: string; modTime: string; @@ -35,6 +36,9 @@ export namespace File { path: string; isDir: boolean; mode: number; + isLink?: boolean; + isSymlink?: boolean; + linkPath?: boolean; } export interface FileDelete { diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index a779099ad..f68cbe19e 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -192,5 +192,9 @@ export default { compressSuccess: '压缩成功', deCompressSuccess: '解压成功', deCompressDst: '解压路径', + linkType: '链接类型', + softLink: '软链接', + hardLink: '硬链接', + linkPath: '链接路径', }, }; diff --git a/frontend/src/views/file-management/create/index.vue b/frontend/src/views/file-management/create/index.vue index 037532536..d4dae2230 100644 --- a/frontend/src/views/file-management/create/index.vue +++ b/frontend/src/views/file-management/create/index.vue @@ -7,13 +7,29 @@ @open="onOpen" v-loading="loading" > - + - - + + + + + + {{ $t('file.softLink') }} + {{ $t('file.hardLink') }} + + + + + + + + + + - - + - +