The upload plugin empowers your thorin application to handle file uploads. In order for it to work, your application must use a http transport. Behind the scenes, the plugin uses busboy to handle file uploading. You also must specify the store solution to use for uploads. We currently only offer the AWS S3 one.
# install the upload plugin npm i --save thorin-plugin-upload@1.x # install the aws upload storage npm i --save thorin-plugin-upload-aws@1.x
'use strict'; // app.js entry file const thorin = require('thorin'); // Register the plugins thorin .addPlugin(require('thorin-plugin-upload')) .addPlugin(require('thorin.plugin-upload-aws')); // Make the upload plugin use the upload-aws for storage. thorin.on(thorin.EVENT.RUN, 'plugin.upload', (pluginObj) => { pluginObj.registerStorage('aws', thorin.config('aws')); }); thorin.run((err) => {});
# update config/app.js with your configuration
The upload storage configuration for AWS should be placed under the aws config key in your application configuration.
'use strict';
// File: config/app.js
module.exports = {
"plugin.upload": {
"limits": {
fileSize: 30 * 1000000 // make it 30MB
}
},
"aws": {
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
bucket: 'YOUR_BUCKET',
region: 'eu-central-1'
}
};
FileUpload
class used while uploading a file
thorin.Action
but with a few other functionalities. This will return a UploadHandler
instance.
registerStorage(type, instanceName, config)
registerStorage(type, config)
registerStorage(storageInstanceObj)
UploadHandler
classlimits
options above.
storage(storageInstanceName=string)
storage(fn) => storageInstanceName = fn(intentObj, fileObj)
storage(fn) => storageInstanceObj = fn(intentObj, fileObj);
fn(fileObj, next)
FileUpload
classurl, size
properties.
pluginObj.IStorage
storage interfaceThis is the storage interface that has to be implemented for custom storage options.
UploadFile
object, that contains a stream.
It is supposed to return a promise that will attach the full URL to the
UploadFile instance and the file size if possible.
'use strict';
const uploadObj = thorin.plugin('upload'),
logger = thorin.logger('upload');
uploadObj
.addHandler('upload.image', '/upload/image') // the POST /upload/image alias
.template('session.account') // inherit authorization from a session.account action template
.enableCors() // enable CORS for the file uploading
.limit('fileSize', '2mb') // limit to 2MB
.mimeType('image/*') // accept only images
.extension('jpg', 'jpeg', 'png') // of type jpeg and png
.storage('aws') // use the AWS storage class
.upload((intentObj, fileObj, next) => {
const account = intentObj.session.account;
fileObj.setFolder(`account/${account.id}`); // change the folder of the image
next();
})
.use((intentObj) => {
const fileObj = intentObj.data('file');
intentObj.result(fileObj).send(); // return the url of the file.
logger.trace('Uploaded image for account ' + intentObj.session.account.id);
});
// The final URL of the image would be https://bucket.eu-west-1.amazonaws.com/account/2/randomKeyRightH3r3.jpg
You can always create a new issue on GitHub or contact one of the core founders by chat.