mirror of
https://github.com/purocean/yn.git
synced 2026-10-13 07:38:54 +08:00
文件新增保存支持仓库
This commit is contained in:
+4
-4
@@ -95,8 +95,8 @@ exports.read = (repo, p) => {
|
||||
return fs.readFileSync(resolvePath(p, repo))
|
||||
}
|
||||
|
||||
exports.write = (p, content) => {
|
||||
p = resolvePath(p)
|
||||
exports.write = (repo, p, content) => {
|
||||
p = resolvePath(p, repo)
|
||||
|
||||
mkdirPSync(path.dirname(p))
|
||||
|
||||
@@ -132,8 +132,8 @@ exports.mv = (oldPath, newPath) => {
|
||||
}
|
||||
}
|
||||
|
||||
exports.exists = p => {
|
||||
return fs.existsSync(resolvePath(p))
|
||||
exports.exists = (repo, p) => {
|
||||
return fs.existsSync(resolvePath(p, repo))
|
||||
}
|
||||
|
||||
exports.upload = (file, path) => {
|
||||
|
||||
+2
-2
@@ -19,11 +19,11 @@ const fileContent = async (ctx, next) => {
|
||||
if (ctx.method === 'GET') {
|
||||
ctx.body = result('ok', '获取成功', file.read(ctx.query.repo, ctx.query.path).toString())
|
||||
} else if (ctx.method === 'POST') {
|
||||
if (ctx.request.body.is_new && file.exists(ctx.request.body.path)) {
|
||||
if (ctx.request.body.is_new && file.exists(ctx.request.body.repo, ctx.request.body.path)) {
|
||||
throw new Error('文件已经存在')
|
||||
}
|
||||
|
||||
file.write(ctx.request.body.path, ctx.request.body.content)
|
||||
file.write(ctx.request.body.repo, ctx.request.body.path, ctx.request.body.content)
|
||||
ctx.body = result()
|
||||
} else if (ctx.method === 'DELETE') {
|
||||
file.rm(ctx.query.path)
|
||||
|
||||
@@ -30,6 +30,7 @@ export default {
|
||||
components: { TreeNode, XFilter },
|
||||
data () {
|
||||
return {
|
||||
repo: null,
|
||||
tree: null,
|
||||
file: null,
|
||||
showFilter: false
|
||||
@@ -46,13 +47,15 @@ export default {
|
||||
this.$bus.off('switch-repository', this.init)
|
||||
},
|
||||
methods: {
|
||||
init (repo) {
|
||||
init (repo = null) {
|
||||
if (repo) {
|
||||
this.repo = repo
|
||||
this.tree = null
|
||||
File.tree(repo, tree => {
|
||||
this.tree = tree
|
||||
})
|
||||
}
|
||||
|
||||
File.tree(this.repo, tree => {
|
||||
this.tree = tree
|
||||
})
|
||||
},
|
||||
onDelete (path) {
|
||||
// 删除了正在编辑的文件或者其父目录
|
||||
|
||||
@@ -83,7 +83,7 @@ export default {
|
||||
}
|
||||
|
||||
const path = this.item.path + '/' + filename
|
||||
File.write(path, `# ${filename.replace(/\.md$/i, '')}\n`, () => {
|
||||
File.write(this.item.repo, path, `# ${filename.replace(/\.md$/i, '')}\n`, () => {
|
||||
this.$emit('change', path)
|
||||
}, e => {
|
||||
alert(e.message)
|
||||
|
||||
@@ -72,7 +72,7 @@ export default {
|
||||
})
|
||||
})
|
||||
},
|
||||
write: (path, content, call, ecall, isNew = false) => {
|
||||
write: (repo, path, content, call, ecall, isNew = false) => {
|
||||
try {
|
||||
if (path.endsWith('.c.md')) {
|
||||
content = encrypt(content)
|
||||
@@ -88,7 +88,7 @@ export default {
|
||||
fetch('/api/file', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({path, content, is_new: isNew})
|
||||
body: JSON.stringify({repo, path, content, is_new: isNew})
|
||||
}).then(response => {
|
||||
response.json().then(result => {
|
||||
if (result.status === 'ok') {
|
||||
|
||||
@@ -103,7 +103,7 @@ export default {
|
||||
|
||||
const content = this.value
|
||||
this.lastSaveContent = content
|
||||
File.write(file.path, content, () => {
|
||||
File.write(file.repo, file.path, content, () => {
|
||||
this.status = '保存于:' + (new Date()).toLocaleString()
|
||||
}, e => {
|
||||
this.file = null
|
||||
|
||||
Reference in New Issue
Block a user