Create upload service
In this guide, we will create a new collection with file field that will be saved in the public folder in the server, after you finish this guide you can change the storage in google-cloud or aws
1 - Generate a new service
feathers generate service
? What kind of service is it? Mongoose
? What is the name of the service? organizations-files
? Which path should the service be registered on? /organizations-files
? Does the service require authentication? No2 - Create new validators file
const {Joi, enums} = require('feathers-mongoose-casl');
const getJoiObject = function(withRequired) {
const required = withRequired ? 'required' : 'optional';
return Joi.object({
// Data from user
displayName: Joi.string()[required](),
// File will be the file url in the storage - the uploadMiddleware will handle this value
file: Joi.string().meta({ dashboard: { doc: {inputType: 'file'}, list: {type: 'link'} } }),
// Data from the file
originalName: Joi.string(),
// The user that uplaod the file
user: Joi.objectId()
.meta({ type: 'ObjectId', ref: 'users', displayKey: 'email' })
.meta({ dashboard: { doc: {readOnly: true} } }),
// Data from the upload service
fileId: Joi.string().meta({ dashboard: { hide: 1 }}),
storage: Joi.string().valid(
enums.STORAGE_TYPES['local-public'], // When file saved on local-storage
enums.STORAGE_TYPES['others'], // When user pass link to file
).meta({ dashboard: { hide: 1 }})
});
};
module.exports = getJoiObject;3. Add service middleware
4 - Update service model
5 - Update service hooks
6 - import upload service
7 - Validate config file
8 - Done!
Last updated
Was this helpful?