新增重命名文件加上限制

This commit is contained in:
purocean
2018-07-02 12:19:51 +08:00
parent 506605f201
commit 228e9b6891
4 changed files with 42 additions and 14 deletions
+4
View File
@@ -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))
}
+23 -6
View File
@@ -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)
+1 -6
View File
@@ -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)
+14 -2
View File
@@ -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)
}
})
})