Improve file box api - add delete endpoints, categories
This commit is contained in:
parent
1545505c22
commit
83b81ed84f
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user