脚本管理支持新增文件夹

This commit is contained in:
whyour 2022-09-16 00:14:10 +08:00
parent d23fcfaa5a
commit 967071ad4e
2 changed files with 42 additions and 16 deletions

View File

@ -77,12 +77,14 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
let { filename, path, content, originFilename } = req.body as {
filename: string;
path: string;
content: string;
originFilename: string;
};
let { filename, path, content, originFilename, directory } =
req.body as {
filename: string;
path: string;
content: string;
originFilename: string;
directory: string;
};
if (!path) {
path = config.scriptPath;
@ -105,6 +107,11 @@ export default (app: Router) => {
return res.send({ code: 200 });
}
if (directory) {
fs.mkdirSync(join(path, directory), { recursive: true });
return res.send({ code: 200 });
}
if (!originFilename) {
originFilename = filename;
}

View File

@ -32,25 +32,26 @@ const EditScriptNameModal = ({
const [loading, setLoading] = useState(false);
const [dirs, setDirs] = useState<any[]>([]);
const [file, setFile] = useState<File>();
const [type, setType] = useState<'blank' | 'upload'>('blank');
const [type, setType] = useState<'blank' | 'upload' | 'directory'>('blank');
const handleOk = async (values: any) => {
setLoading(true);
values.path = values.path || '';
const { path = '', filename: inputFilename, directory } = values;
const formData = new FormData();
formData.append('file', file as any);
formData.append('filename', values.filename);
formData.append('path', values.path);
formData.append('filename', inputFilename);
formData.append('path', path);
formData.append('content', '');
formData.append('directory', directory);
request
.post(`${config.apiPrefix}scripts`, {
data: formData,
})
.then(({ code, data }) => {
if (code === 200) {
message.success('新建文件成功');
const key = values.path ? `${values.path}/` : '';
const filename = file ? file.name : values.filename;
message.success(directory ? '新建文件夹成功' : '新建文件成功');
const key = path ? `${values.path}/` : '';
const filename = file ? file.name : inputFilename;
handleCancel({
filename,
path: values.path,
@ -99,7 +100,7 @@ const EditScriptNameModal = ({
return (
<Modal
title="新建脚本"
title="新建"
visible={visible}
forceRender
centered
@ -126,18 +127,36 @@ const EditScriptNameModal = ({
>
<Radio.Group onChange={typeChange}>
<Radio value="blank"></Radio>
<Radio value="upload"></Radio>
<Radio value="upload"></Radio>
<Radio value="directory"></Radio>
</Radio.Group>
</Form.Item>
{type === 'blank' && (
<Form.Item
name="filename"
label="文件名"
rules={[{ required: true, message: '请输入文件名' }]}
rules={[
{ required: true, message: '请输入文件名' },
{
validator: (_, value) =>
value.includes('/')
? Promise.reject(new Error('文件名不能包含斜杠'))
: Promise.resolve(),
},
]}
>
<Input placeholder="请输入文件名" />
</Form.Item>
)}
{type === 'directory' && (
<Form.Item
name="directory"
label="文件夹名"
rules={[{ required: true, message: '请输入文件夹名' }]}
>
<Input placeholder="请输入文件夹名" />
</Form.Item>
)}
<Form.Item label="父目录" name="path">
<TreeSelect
allowClear