feat: 增加重命名功能

This commit is contained in:
zhengkunwang223 2022-09-03 22:22:40 +08:00
parent 89b95e0d45
commit 04db6a8cf3
12 changed files with 160 additions and 11 deletions

View file

@ -154,3 +154,16 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
}
helper.SuccessWithMsg(c, fmt.Sprintf("%d files upload success", success))
}
func (b *BaseApi) ChangeName(c *gin.Context) {
var req dto.FileRename
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := fileService.ChangeName(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}

View file

@ -52,3 +52,8 @@ type FileEdit struct {
Path string
Content string
}
type FileRename struct {
OldName string
NewName string
}

View file

@ -115,6 +115,11 @@ func (f FileService) SaveContent(c dto.FileEdit) error {
return fo.WriteFile(c.Path, strings.NewReader(c.Content), info.FileMode)
}
func (f FileService) ChangeName(c dto.FileRename) error {
fo := files.NewFileOp()
return fo.Rename(c.OldName, c.NewName)
}
func getUuid() string {
b := make([]byte, 16)
io.ReadFull(rand.Reader, b)

View file

@ -25,6 +25,7 @@ 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)
}
}

View file

@ -77,6 +77,10 @@ func (f FileOp) Chmod(dst string, mode fs.FileMode) error {
return f.Fs.Chmod(dst, mode)
}
func (f FileOp) Rename(oldName string, newName string) error {
return f.Fs.Rename(oldName, newName)
}
type CompressType string
const (

View file

@ -64,4 +64,9 @@ export namespace File {
path: string;
content: string;
}
export interface FileRename {
oldName: string;
newName: string;
}
}

View file

@ -40,3 +40,7 @@ export const SaveFileContent = (params: File.FileEdit) => {
export const UploadFileData = (params: FormData) => {
return http.post<File.File>('files/upload', params);
};
export const RenameRile = (params: File.FileRename) => {
return http.post<File.File>('files/rename', params);
};

View file

@ -152,11 +152,6 @@ const changeMode = (val: String) => {
getRoleNum(val[3], form.value.public);
};
// onMounted(() => {
// form.value.mode = mode.value;
// changeMode(form.value.mode);
// });
onUpdated(() => {
form.value.mode = mode.value;
changeMode(form.value.mode);

View file

@ -32,6 +32,7 @@ export default {
sureLogOut: 'Are you sure you want to log out?',
createSuccess: 'Create Success',
updateSuccess: 'Update Success',
uploadSuccess: 'Update Success',
},
login: {
captchaHelper: 'Please enter the verification code',

View file

@ -32,6 +32,7 @@ export default {
sureLogOut: '您是否确认退出登录?',
createSuccess: '新建成功',
updateSuccess: '更新成功',
uploadSuccess: '上传成功',
},
login: {
captchaHelper: '请输入验证码',

View file

@ -0,0 +1,95 @@
<template>
<el-dialog
v-model="open"
:before-close="handleClose"
:title="$t('file.setRole')"
width="30%"
@open="onOpen"
v-loading="loading"
>
<el-form ref="fileForm" label-position="left" :model="addForm" label-width="100px" :rules="rules">
<el-form-item :label="$t('file.path')" prop="path">
<el-input v-model="props.path" disabled
/></el-form-item>
<el-form-item :label="$t('file.name')" prop="newName"> <el-input v-model="addForm.newName" /></el-form-item
></el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="handleClose">{{ $t('commons.button.cancel') }}</el-button>
<el-button type="primary" @click="submit(fileForm)">{{ $t('commons.button.confirm') }}</el-button>
</span>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
import { RenameRile } from '@/api/modules/files';
import { Rules } from '@/global/form-rues';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import { reactive, ref, toRefs } from 'vue';
import { File } from '@/api/interface/file';
import i18n from '@/lang';
const props = defineProps({
open: {
type: Boolean,
default: false,
},
oldName: {
type: String,
default: '',
},
path: {
type: String,
default: '',
},
});
const { open } = toRefs(props);
const fileForm = ref<FormInstance>();
const loading = ref(false);
const addForm = reactive({
newName: '',
path: '',
});
const rules = reactive<FormRules>({
newName: [Rules.requiredInput],
});
const em = defineEmits(['close']);
const handleClose = () => {
em('close', false);
};
const getPath = (path: string, name: string) => {
return path + '/' + name;
};
const submit = async (formEl: FormInstance | undefined) => {
if (!formEl) return;
await formEl.validate((valid) => {
if (!valid) {
return;
}
let addItem = {};
Object.assign(addItem, addForm);
addItem['oldName'] = getPath(props.path, props.oldName);
addItem['newName'] = getPath(props.path, addForm.newName);
loading.value = true;
RenameRile(addItem as File.FileRename)
.then(() => {
ElMessage.success(i18n.global.t('commons.msg.updateSuccess'));
handleClose();
})
.finally(() => {
loading.value = false;
});
});
};
const onOpen = () => {
addForm.newName = props.oldName;
};
</script>

View file

@ -124,6 +124,12 @@
@qsave="quickSave"
@save="saveContent"
></CodeEditor>
<FileRename
:open="renamePage.open"
:path="renamePage.path"
:oldName="renamePage.oldName"
@close="closeRename"
></FileRename>
<Upload :open="uploadPage.open" :path="uploadPage.path" @close="closeUpload"></Upload>
</el-row>
</LayoutContent>
@ -144,6 +150,7 @@ import ChangeRole from './change-role/index.vue';
import Compress from './compress/index.vue';
import Decompress from './decompress/index.vue';
import Upload from './upload/index.vue';
import FileRename from './file-rename/index.vue';
import { useDeleteData } from '@/hooks/use-delete-data';
import CodeEditor from './code-editor/index.vue';
import { ElMessage } from 'element-plus';
@ -157,13 +164,14 @@ let paths = ref<string[]>([]);
let fileTree = ref<File.FileTree[]>([]);
let expandKeys = ref<string[]>([]);
let filePage = reactive({ open: false, createForm: { path: '/', isDir: false, mode: 0o755 } });
let modePage = reactive({ open: false, modeForm: { path: '/', isDir: false, mode: 0o755 } });
let compressPage = reactive({ open: false, files: [''], name: '', dst: '' });
let deCompressPage = reactive({ open: false, path: '', name: '', dst: '', mimeType: '' });
let editorPage = reactive({ open: false, content: '', loading: false });
let codeReq = reactive({ path: '', expand: false });
const filePage = reactive({ open: false, createForm: { path: '/', isDir: false, mode: 0o755 } });
const modePage = reactive({ open: false, modeForm: { path: '/', isDir: false, mode: 0o755 } });
const compressPage = reactive({ open: false, files: [''], name: '', dst: '' });
const deCompressPage = reactive({ open: false, path: '', name: '', dst: '', mimeType: '' });
const editorPage = reactive({ open: false, content: '', loading: false });
const codeReq = reactive({ path: '', expand: false });
const uploadPage = reactive({ open: false, path: '' });
const renamePage = reactive({ open: false, path: '', oldName: '' });
const defaultProps = {
children: 'children',
@ -338,6 +346,17 @@ const closeUpload = () => {
search(req);
};
const openRename = (item: File.File) => {
renamePage.open = true;
renamePage.path = req.path;
renamePage.oldName = item.name;
};
const closeRename = () => {
renamePage.open = false;
search(req);
};
const saveContent = (content: string) => {
editorPage.loading = true;
SaveFileContent({ path: codeReq.path, content: content }).finally(() => {
@ -380,6 +399,7 @@ const buttons = [
},
{
label: i18n.global.t('file.rename'),
click: openRename,
},
{
label: i18n.global.t('commons.button.delete'),