feat: 优化文件创建 (#4440)

This commit is contained in:
zhengkunwang 2024-04-09 15:34:08 +08:00 committed by GitHub
parent db4973cfde
commit 0751326b46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 11 deletions

View File

@ -22,7 +22,7 @@ type FileCreate struct {
Path string `json:"path" validate:"required"`
Content string `json:"content"`
IsDir bool `json:"isDir"`
Mode int64 `json:"mode" validate:"required"`
Mode int64 `json:"mode"`
IsLink bool `json:"isLink"`
IsSymlink bool `json:"isSymlink"`
LinkPath string `json:"linkPath"`

View File

@ -120,18 +120,25 @@ func (f *FileService) Create(op request.FileCreate) error {
if fo.Stat(op.Path) {
return buserr.New(constant.ErrFileIsExit)
}
if op.IsDir {
return fo.CreateDir(op.Path, fs.FileMode(op.Mode))
} else {
if op.IsLink {
if !fo.Stat(op.LinkPath) {
return buserr.New(constant.ErrLinkPathNotFound)
}
return fo.LinkFile(op.LinkPath, op.Path, op.IsSymlink)
mode := op.Mode
if mode == 0 {
fileInfo, err := os.Stat(filepath.Dir(op.Path))
if err == nil {
mode = int64(fileInfo.Mode().Perm())
} else {
return fo.CreateFile(op.Path)
mode = 0755
}
}
if op.IsDir {
return fo.CreateDir(op.Path, fs.FileMode(mode))
}
if op.IsLink {
if !fo.Stat(op.LinkPath) {
return buserr.New(constant.ErrLinkPathNotFound)
}
return fo.LinkFile(op.LinkPath, op.Path, op.IsSymlink)
}
return fo.CreateFileWithMode(op.Path, fs.FileMode(mode))
}
func (f *FileService) Delete(op request.FileDelete) error {

View File

@ -64,6 +64,14 @@ func (f FileOp) CreateFile(dst string) error {
return nil
}
func (f FileOp) CreateFileWithMode(dst string, mode fs.FileMode) error {
file, err := f.Fs.OpenFile(dst, os.O_CREATE, mode)
if err != nil {
return err
}
return file.Close()
}
func (f FileOp) LinkFile(source string, dst string, isSymlink bool) error {
if isSymlink {
osFs := afero.OsFs{}

View File

@ -55,7 +55,7 @@ export namespace File {
export interface FileCreate {
path: string;
isDir: boolean;
mode: number;
mode?: number;
isLink?: boolean;
isSymlink?: boolean;
linkPath?: boolean;

View File

@ -125,6 +125,9 @@ const submit = async (formEl: FormInstance | undefined) => {
Object.assign(addItem, addForm);
addItem['path'] = getPath.value;
loading.value = true;
if (!setRole.value) {
addItem['mode'] = undefined;
}
CreateFile(addItem as File.FileCreate)
.then(() => {
MsgSuccess(i18n.global.t('commons.msg.createSuccess'));