From 83b81ed84f98bd64bffbfa5402a2ec5df5e30179 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Thu, 4 Feb 2021 12:57:11 -0600 Subject: [PATCH] Improve file box api - add delete endpoints, categories --- app/controllers/api/v1/FileBox.controller.js | 61 ++++++++++++++++++- app/models/api/files/FileBox.model.js | 13 ++++ app/routing/routers/api/v1/file-box.routes.js | 10 +++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/FileBox.controller.js b/app/controllers/api/v1/FileBox.controller.js index f413be5..d19b208 100644 --- a/app/controllers/api/v1/FileBox.controller.js +++ b/app/controllers/api/v1/FileBox.controller.js @@ -6,7 +6,7 @@ class FileBoxController extends Controller { } async create(req, res, next) { - const FileBox = this.models.get('models:files:FileBox') + const FileBox = this.models.get('api:files:FileBox') if ( !req.body.name ) { return res.message('Missing required field: name') @@ -60,6 +60,12 @@ class FileBoxController extends Controller { } await box.save() + + if ( !req.body.rootUUID && !box.rootUUID ) { + box.rootUUID = box.UUID + await box.save() + } + return res.api(await box.to_api()) } @@ -97,7 +103,7 @@ class FileBoxController extends Controller { const { file_box, file } = req.form if ( req.body.name ) { - file.name = req.body.name + file.original_name = req.body.name } if ( req.body.parentUUID && req.body.parentUUID !== file_box.UUID ) { @@ -123,6 +129,11 @@ class FileBoxController extends Controller { return res.api(this.file_to_api(file)) } + async download_file(req, res, next) { + const { file_box, file } = req.form + return file.send(res) + } + async upload_files(req, res, next) { const { file_box } = req.form @@ -143,6 +154,12 @@ class FileBoxController extends Controller { return res.api(await req.form.file_box.to_api()) } + async get_children(req, res, next) { + const { file_box } = req.form + const children = await file_box.children() + return res.api(await Promise.all(children.map(x => x.to_api()))); + } + async get_files(req, res, next) { const { file_box, page } = req.form const files = await file_box.files() @@ -165,13 +182,51 @@ class FileBoxController extends Controller { file_to_api(file) { return { - name: file.upload_name, + type: 'file', + title: file.original_name, mime: file.mime_type, uploaded: file.upload_date, + category: this.get_file_category(file.mime_type), id: file.id, _id: file._id, } } + + get_file_category(mime) { + if ( mime.includes('opendocument.text') || mime.includes('.document') ) { + return 'document'; + } + + if ( mime.includes('.spreadsheet') || mime.includes('.sheet') ) { + return 'spreadsheet'; + } + + if ( mime.includes('.presentation') ) { + return 'presentation'; + } + + if ( mime.includes('image/') ) { + return 'image'; + } + + if ( mime === 'application/pdf' ) { + return 'pdf'; + } + + if ( mime.includes('video/') ) { + return 'video'; + } + + if ( mime.includes('script') ) { + return 'code'; + } + + if ( mime.includes('text/') ) { + return 'text'; + } + + return 'other'; + } } module.exports = exports = FileBoxController diff --git a/app/models/api/files/FileBox.model.js b/app/models/api/files/FileBox.model.js index 8268697..d1abe93 100644 --- a/app/models/api/files/FileBox.model.js +++ b/app/models/api/files/FileBox.model.js @@ -3,6 +3,10 @@ const uuid = require('uuid').v4 const { ObjectId } = require('mongodb') class FileBoxModel extends Model { + static get services() { + return [...super.services, 'models'] + } + static get schema() { return { UUID: { type: String, default: uuid }, @@ -29,6 +33,13 @@ class FileBoxModel extends Model { }) } + async children() { + return this.constructor.find({ + parentUUID: this.UUID, + active: true, + }) + } + async files() { const File = this.models.get('upload::File') return await File.find({ @@ -40,8 +51,10 @@ class FileBoxModel extends Model { async to_api() { return { + type: 'folder', UUID: this.UUID, name: this.name, + title: this.name, pageId: this.pageId, parentUUID: this.parentUUID, rootUUID: this.rootUUID, diff --git a/app/routing/routers/api/v1/file-box.routes.js b/app/routing/routers/api/v1/file-box.routes.js index 895ab0f..1c92024 100644 --- a/app/routing/routers/api/v1/file-box.routes.js +++ b/app/routing/routers/api/v1/file-box.routes.js @@ -15,6 +15,16 @@ module.exports = exports = { ['middleware::api:DataInjection', { access_level: 'view' }], 'controller::api:v1:FileBox.get_files', ], + '/:PageId/:FileBoxId/files/:FileBoxFileId': [ + 'middleware::auth:ApiRoute', + ['middleware::api:DataInjection', { access_level: 'view' }], + 'controller::api:v1:FileBox.download_file', + ], + '/:PageId/:FileBoxId/children': [ + 'middleware::auth:ApiRoute', + ['middleware::api:DataInjection', { access_level: 'view' }], + 'controller::api:v1:FileBox.get_children', + ], }, post: {