Minio/Amazon S3 Storage Adapter
Minio adapter for receiving upstreams. Particularly useful for streaming multipart file uploads from the Skipper body parser.
Installation
$ npm install skipper-minio --save
If you're using this module outside of Sails (e.g. Express or a vanilla Node.js server), make sure you have skipper itself installed as your body parser.
Buyer Beware I probably would not recommend using this package in production applications just yet, and I'm sorry I didn't namespace the package from the beginning. That said it seems to do what it says on the box, but streams are still black magic as far as I'm concerned.
Usage
Using Minio
In the route(s) / controller action(s) where you want to accept file uploads, do something like:
req;
You can also use the nifty sails-hook-uploads for async/await-able upload processing in Sails v1.
Using Amazon S3
req;
Only allow files of type x
mmmagic is used to detect the MIME type of incoming upload streams.
You're able to restrict the types of files that will be accepted by passing an array of MIME types to the allowedFileTypes
setting.
Transforming an incoming upload
You're able to specify a transformer
method (or array of methods) to modify the incoming upload.
The following example accepts jpeg/png/gif uploads which it crops & resizes and stores as a jpeg using sharp.
const uuid = ;const sharp = ; req;
File extension normalisation
If you specify the runSaveAsAfterMimeDetection
boolean option then the saveAs
method will be called once again just before uploading the file to minio.
This could allow you to normalise the final filename of an upload. The use case that prompted this was
- A user uploads a jpeg file with a
.png
extension - A user uploads a jpeg file with a
.JPEG
extension, but other moving parts of the system are looking for and will only work on files with.jpg
file extension
If this option is true
then your saveAs
method will be run multiple times.
You probably shouldn't use this (it's more for my own reference when I come back in 12 months and wonder wtf is going on) but hey, check out our config/uploads.js
moduleexportsuploads = // ... allowedFileTypes: 'image/jpeg' 'image/png' 'image/gif' fileTypeMap: 'image/jpeg': '.jpg' 'image/png': '.png' 'image/gif': '.gif' runSaveAsAfterMimeDetection: true { const uuid = const extension = sailsconfiguploadsfileTypeMap__filemimeType || __filefilename || '.upload' // Because this saveAs method is gonna run probably twice, we attach the generated uuid to __file to save CPU cycles the next time we run let filename if __file__userlandFileName filename = __file__userlandFileName else filename = __file__userlandFileName = filename return }
TODO
Contribute
Please! I have no idea what I'm doing here 😉
To run the tests:
$ npm test
License
Based on work by Jarrod Linahan, Mike McNeil, Balderdash Design Co., Sails Co.
See LICENSE.md
.
This module is intended to be uesd with the Sails framework, and is free and open-source under the MIT License.