2018-02-08 12:01:06 +01:00
'use strict' ;
/ * *
* Upload . js controller
*
* @ description : A set of functions called "actions" of the ` upload ` plugin .
* /
2018-02-21 14:06:57 +01:00
const _ = require ( 'lodash' ) ;
2018-02-08 12:01:06 +01:00
module . exports = {
2018-03-07 16:25:56 +01:00
upload : async ( ctx ) => {
2018-02-27 16:53:06 +01:00
// Retrieve provider configuration.
2018-02-20 17:10:25 +01:00
const config = await strapi . store ( {
environment : strapi . config . environment ,
type : 'plugin' ,
name : 'upload'
2018-03-06 15:49:11 +01:00
} ) . get ( { key : 'provider' } ) ;
2018-02-20 17:10:25 +01:00
2018-02-27 16:53:06 +01:00
// Verify if the file upload is enable.
if ( config . enabled === false ) {
2018-03-06 16:51:00 +01:00
strapi . log . error ( 'File upload is disabled' ) ;
return ctx . badRequest ( null , ctx . request . admin ? [ { messages : [ { id : 'Upload.status.disabled' } ] } ] : 'File upload is disabled' ) ;
2018-02-20 17:10:25 +01:00
}
2018-02-27 16:53:06 +01:00
// Extract optional relational data.
const { refId , ref , source , field } = ctx . request . body . fields ;
2018-03-07 17:48:40 +01:00
const { files = { } } = ctx . request . body . files ;
if ( _ . isEmpty ( files ) ) {
return ctx . send ( true ) ;
}
2018-02-27 16:53:06 +01:00
// Transform stream files to buffer
const buffers = await strapi . plugins . upload . services . upload . bufferize ( ctx . request . body . files . files ) ;
2018-03-07 17:48:40 +01:00
const enhancedFiles = buffers . map ( file => {
2018-04-30 18:00:01 +02:00
if ( file . size > config . sizeLimit ) {
return ctx . badRequest ( null , ctx . request . admin ? [ { messages : [ { id : 'Upload.status.sizeLimit' , values : { file : file . name } } ] } ] : ` ${ file . name } file is bigger than limit size! ` ) ;
}
// Add details to the file to be able to create the relationships.
if ( refId && ref && field ) {
Object . assign ( file , {
related : [ {
refId ,
ref ,
source ,
field
} ]
} ) ;
}
return file ;
} ) ;
2018-02-27 16:53:06 +01:00
// Something is wrong (size limit)...
if ( ctx . status === 400 ) {
return ;
2018-02-21 14:46:10 +01:00
}
2018-03-07 17:48:40 +01:00
const uploadedFiles = await strapi . plugins . upload . services . upload . upload ( enhancedFiles , config ) ;
2018-02-08 12:01:06 +01:00
// Send 200 `ok`
2018-02-27 16:53:06 +01:00
ctx . send ( uploadedFiles . map ( ( file ) => {
// If is local server upload, add backend host as prefix
if ( file . url && file . url [ 0 ] === '/' ) {
2018-02-21 14:06:57 +01:00
file . url = strapi . config . url + file . url ;
}
2018-02-19 15:41:26 +01:00
2018-02-27 16:53:06 +01:00
if ( _ . isArray ( file . related ) ) {
file . related = file . related . map ( obj => obj . ref || obj ) ;
}
2018-02-19 15:41:26 +01:00
return file ;
2018-02-19 14:26:20 +01:00
} ) ) ;
2018-02-19 15:41:26 +01:00
} ,
2018-02-27 17:45:12 +01:00
getEnvironments : async ( ctx ) => {
const environments = _ . map ( _ . keys ( strapi . config . environments ) , environment => {
return {
name : environment ,
active : ( strapi . config . environment === environment )
} ;
} ) ;
ctx . send ( { environments } ) ;
} ,
2018-02-20 17:10:25 +01:00
getSettings : async ( ctx ) => {
const config = await strapi . store ( {
environment : ctx . params . environment ,
type : 'plugin' ,
name : 'upload'
} ) . get ( { key : 'provider' } ) ;
ctx . send ( {
providers : strapi . plugins . upload . config . providers ,
config
} ) ;
} ,
updateSettings : async ( ctx ) => {
await strapi . store ( {
environment : ctx . params . environment ,
type : 'plugin' ,
name : 'upload'
2018-02-21 14:06:57 +01:00
} ) . set ( { key : 'provider' , value : ctx . request . body } ) ;
2018-02-20 17:10:25 +01:00
ctx . send ( { ok : true } ) ;
} ,
2018-02-19 15:41:26 +01:00
find : async ( ctx ) => {
const data = await strapi . plugins [ 'upload' ] . services . upload . fetchAll ( ctx . query ) ;
// Send 200 `ok`
2018-02-19 16:00:37 +01:00
ctx . send ( data . map ( ( file ) => {
2018-02-21 17:18:33 +01:00
// if is local server upload, add backend host as prefix
2018-02-27 16:53:06 +01:00
if ( file . url [ 0 ] === '/' ) {
2018-02-21 14:06:57 +01:00
file . url = strapi . config . url + file . url ;
}
2018-02-19 16:00:37 +01:00
return file ;
} ) ) ;
2018-02-19 15:41:26 +01:00
} ,
2018-02-19 16:00:37 +01:00
2018-04-09 19:31:49 +02:00
findOne : async ( ctx ) => {
const data = await strapi . plugins [ 'upload' ] . services . upload . fetch ( ctx . params ) ;
data . url = strapi . config . url + data . url ;
// Send 200 `ok`
ctx . send ( data ) ;
} ,
2018-04-30 18:00:01 +02:00
count : async ( ctx ) => {
2018-02-19 19:54:45 +01:00
const data = await strapi . plugins [ 'upload' ] . services . upload . count ( ctx . query ) ;
// Send 200 `ok`
ctx . send ( {
count : data
} ) ;
} ,
2018-04-30 18:00:01 +02:00
destroy : async ( ctx ) => {
2018-02-20 17:10:25 +01:00
const config = await strapi . store ( {
environment : strapi . config . environment ,
type : 'plugin' ,
name : 'upload'
} ) . get ( { key : 'provider' } ) ;
const data = await strapi . plugins [ 'upload' ] . services . upload . remove ( ctx . params , config ) ;
2018-02-19 16:00:37 +01:00
// Send 200 `ok`
ctx . send ( data ) ;
2018-02-22 17:12:03 +01:00
} ,
search : async ( ctx ) => {
const data = await strapi . query ( 'file' , 'upload' ) . search ( ctx . params ) ;
ctx . send ( data ) ;
} ,
2018-02-08 12:01:06 +01:00
} ;