diff --git a/backend/file.js b/backend/file.js index 325a102d..1fcfddb5 100644 --- a/backend/file.js +++ b/backend/file.js @@ -91,8 +91,8 @@ const travels = (location, repo, basePath = null) => { })) } -exports.read = p => { - return fs.readFileSync(resolvePath(p)) +exports.read = (repo, p) => { + return fs.readFileSync(resolvePath(p, repo)) } exports.write = (p, content) => { diff --git a/backend/main.js b/backend/main.js index 799cc9bf..e01d1092 100644 --- a/backend/main.js +++ b/backend/main.js @@ -17,7 +17,7 @@ const result = (status = 'ok', message = '操作成功', data = null) => { const fileContent = async (ctx, next) => { if (ctx.path === '/api/file') { if (ctx.method === 'GET') { - ctx.body = result('ok', '获取成功', file.read(ctx.query.path).toString()) + 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)) { throw new Error('文件已经存在') @@ -85,6 +85,24 @@ const plantumlGen = async (ctx, next) => { } } +const searchFile = async (ctx, next) => { + if (ctx.path.startsWith('/api/search')) { + const search = ctx.query.str + + ctx.body = result('ok', '操作成功', file.search(search)) + } else { + await next() + } +} + +const repository = async (ctx, next) => { + if (ctx.path.startsWith('/api/repositories')) { + ctx.body = result('ok', '获取成功', dataRepository.list()) + } else { + await next() + } +} + const runCode = async (ctx, next) => { if (ctx.path.startsWith('/api/run')) { const rst = run.runCode(ctx.request.body.language, ctx.request.body.code) @@ -106,24 +124,6 @@ const convertFile = async (ctx, next) => { } } -const searchFile = async (ctx, next) => { - if (ctx.path.startsWith('/api/search')) { - const search = ctx.query.str - - ctx.body = result('ok', '操作成功', file.search(search)) - } else { - await next() - } -} - -const repository = async (ctx, next) => { - if (ctx.path.startsWith('/api/repositories')) { - ctx.body = result('ok', '获取成功', dataRepository.list()) - } else { - await next() - } -} - const app = new Koa() app.use(static( diff --git a/frontend/src/file.js b/frontend/src/file.js index 35ac5cf4..14877575 100644 --- a/frontend/src/file.js +++ b/frontend/src/file.js @@ -48,8 +48,8 @@ const decrypt = content => { } export default { - read: (path, call, ecall) => { - fetch(`/api/file?path=${encodeURIComponent(path)}`).then(response => { + read: (repo, path, call, ecall) => { + fetch(`/api/file?path=${encodeURIComponent(path)}&repo=${repo}`).then(response => { response.json().then(result => { if (result.status === 'ok') { try { diff --git a/frontend/src/pages/Home.vue b/frontend/src/pages/Home.vue index 53e8d755..ad99a269 100644 --- a/frontend/src/pages/Home.vue +++ b/frontend/src/pages/Home.vue @@ -144,7 +144,7 @@ export default { } if (f) { - File.read(f.path, data => { + File.read(f.repo, f.path, data => { this.lastSaveContent = data this.$refs.editor.setValue(data) this.status = '加载完毕'