diff --git a/src/models/DirectoryModel.js b/src/models/DirectoryModel.js index 4fb82f9c1..947005993 100644 --- a/src/models/DirectoryModel.js +++ b/src/models/DirectoryModel.js @@ -33,15 +33,20 @@ export function createDirectoryModel(origin, accessToken, api) { .on('progress', progressHandler); }, async remove(filePath) { - const [error] = await safe(superagent.del(`${origin}/api/v1/${api}/files/${filePath}`) - .query({ access_token: accessToken })); - if (error) throw error; + await superagent.del(`${origin}/api/v1/${api}/files/${filePath}`) + .query({ access_token: accessToken }); }, async rename(fromFilePath, toFilePath) { - const [error] = await safe(superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`) + await superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`) .send({ action: 'rename', newFilePath: sanitize(toFilePath) }) - .query({ access_token: accessToken })); - if (error) throw error; + .query({ access_token: accessToken }); + }, + async save(filePath, content) { + const file = new File([content], 'file'); + await superagent.post(`${origin}/api/v1/${api}/files/${filePath}`) + .query({ access_token: accessToken }) + .attach('file', file) + .field('overwrite', 'true'); }, async getFile(path) { const [error, result] = await safe(fetch(`${origin}/api/v1/${api}/files/${path}?access_token=${accessToken}`)); diff --git a/src/views/Viewer.vue b/src/views/Viewer.vue index 4fd3fdde9..ec988357e 100644 --- a/src/views/Viewer.vue +++ b/src/views/Viewer.vue @@ -1,6 +1,10 @@ @@ -30,15 +34,18 @@ export default { methods: { onClose() { this.$router.go(-1); + }, + async saveHandler(item, content) { + await this.directoryModel.save(this.filePath, content); } }, async mounted() { this.resourceId = this.$route.params.resourceId; this.resourceType = this.$route.params.type; - const filePath = this.$route.params.filePath.join('/'); + this.filePath = this.$route.params.filePath.join('/'); const fileName = this.$route.params.filePath[this.$route.params.filePath.length-1]; - const parentDirectoryPath = sanitize(filePath.split('/').slice(0, -1).join('/')); + const parentDirectoryPath = sanitize(this.filePath.split('/').slice(0, -1).join('/')); this.directoryModel = createDirectoryModel(BASE_URL, localStorage.accessToken, (this.resourceType === 'volume' ? 'volumes/' : 'apps/') + this.resourceId); const files = await this.directoryModel.listFiles(parentDirectoryPath); @@ -46,19 +53,19 @@ export default { this.item = files.find(i => i.fileName === fileName); if (!this.item) { - console.log('File not found', filePath); + console.log('File not found', this.filePath); return; } if (this.$refs.imageViewer.canHandle(this.item)) { - this.$refs.imageViewer.open(this.item, this.directoryModel.getFileUrl(filePath)); + this.$refs.imageViewer.open(this.item, this.directoryModel.getFileUrl(this.filePath)); this.active = 'imageViewer'; } else if (this.$refs.textEditor.canHandle(this.item)) { - const content = await this.directoryModel.getFile(filePath); + const content = await this.directoryModel.getFile(this.filePath); this.$refs.textEditor.open(this.item, content); this.active = 'textEditor'; } else { - console.warn(`no editor or viewer for ${this.item.mimeType}`, this.item); + console.warn(`no editor or viewer found for ${this.item.mimeType}`, this.item); this.active = ''; } }