diff --git a/backend/file.js b/backend/file.js index 76cce783..e0c362fe 100644 --- a/backend/file.js +++ b/backend/file.js @@ -111,6 +111,10 @@ exports.mv = (oldPath, newPath) => { } } +exports.exists = p => { + return fs.existsSync(resolvePath(p)) +} + exports.upload = (file, path) => { exports.write(path, fs.readFileSync(file.path)) } diff --git a/backend/main.js b/backend/main.js index bca5631e..fb426341 100644 --- a/backend/main.js +++ b/backend/main.js @@ -18,12 +18,20 @@ const fileContent = async (ctx, next) => { if (ctx.method === 'GET') { ctx.body = result('ok', '获取成功', file.read(ctx.query.path).toString()) } else if (ctx.method === 'POST') { + if (ctx.request.body.is_new && file.exists(ctx.request.body.path)) { + throw new Error('文件已经存在') + } + file.write(ctx.request.body.path, ctx.request.body.content) ctx.body = result() } else if (ctx.method === 'DELETE') { file.rm(ctx.query.path) ctx.body = result() } else if (ctx.method === 'PATCH') { + if (file.exists(ctx.request.body.newPath)) { + throw new Error('文件已经存在') + } + file.mv(ctx.request.body.oldPath, ctx.request.body.newPath) ctx.body = result() } @@ -108,12 +116,21 @@ app.use(bodyParser({ jsonLimit: '20mb', textLimit: '20mb' })) -app.use(async (ctx, next) => await fileContent(ctx, next)) -app.use(async (ctx, next) => await attachment(ctx, next)) -app.use(async (ctx, next) => await plantumlGen(ctx, next)) -app.use(async (ctx, next) => await runCode(ctx, next)) -app.use(async (ctx, next) => await convertFile(ctx, next)) -app.use(async (ctx, next) => await searchFile(ctx, next)) + +const wrapper = async (ctx, next, fun) => { + try { + await fun(ctx, next) + } catch (error) { + ctx.body = result('error', error.message) + } +} + +app.use(async (ctx, next) => await wrapper(ctx, next, fileContent)) +app.use(async (ctx, next) => await wrapper(ctx, next, attachment)) +app.use(async (ctx, next) => await wrapper(ctx, next, plantumlGen)) +app.use(async (ctx, next) => await wrapper(ctx, next, runCode)) +app.use(async (ctx, next) => await wrapper(ctx, next, convertFile)) +app.use(async (ctx, next) => await wrapper(ctx, next, searchFile)) const port = 3000 app.listen(port) diff --git a/frontend/src/components/TreeNode.vue b/frontend/src/components/TreeNode.vue index 8f138c56..784c73b8 100644 --- a/frontend/src/components/TreeNode.vue +++ b/frontend/src/components/TreeNode.vue @@ -73,11 +73,6 @@ export default { return } - if (this.item.children.find(x => x.name === filename)) { - window.alert('目标目录有同名文件存在') - return - } - if (!filename.endsWith('.md')) { filename += '.md' } @@ -87,7 +82,7 @@ export default { this.$emit('change', path) }, e => { alert(e.message) - }) + }, true) }, renameFile () { let newPath = window.prompt(`新文件名`, this.item.path) diff --git a/frontend/src/file.js b/frontend/src/file.js index 69288e4f..60feb516 100644 --- a/frontend/src/file.js +++ b/frontend/src/file.js @@ -66,11 +66,13 @@ export default { throw e } } + } else { + alert(result.message) } }) }) }, - write: (path, content, call, ecall) => { + write: (path, content, call, ecall, isNew = false) => { try { if (path.endsWith('.c.md')) { content = encrypt(content) @@ -86,11 +88,13 @@ export default { fetch('/api/file', { method: 'POST', headers: {'Content-Type': 'application/json'}, - body: JSON.stringify({path, content}) + body: JSON.stringify({path, content, is_new: isNew}) }).then(response => { response.json().then(result => { if (result.status === 'ok') { call(result) + } else { + alert(result.message) } }) }) @@ -104,6 +108,8 @@ export default { response.json().then(result => { if (result.status === 'ok') { call(result) + } else { + alert(result.message) } }) }) @@ -113,6 +119,8 @@ export default { response.json().then(result => { if (result.status === 'ok') { call(result.data) + } else { + alert(result.message) } }) }) @@ -122,6 +130,8 @@ export default { response.json().then(result => { if (result.status === 'ok') { call(result) + } else { + alert(result.message) } }) }) @@ -144,6 +154,8 @@ export default { response.json().then(result => { if (result.status === 'ok') { call(result.data) + } else { + alert(result.message) } }) })