File manager and
multipart/form-data parser for Node.js Express applications.
npm install --save fi-fileman
const fileman = ;
This component should be configured before using it:
const db = ;const fileman = ;const app = ;const path = ;fileman;//...app;app;//...app;app;//...
Fi Fileman exposes multiple methods that might be used as functions or as an Express middleware.
This method should be called before using any other Fi Fileman's methods:
It only receives a parameter that must be an
Object with the following optional parameters:
tempdir: This can be a
Stringto the absolute path where the temporal uploaded files are saved. It defaults to:path
stordir: This can be a
Stringto the absolute path where the files are finally stored. It defaults to:path
That path might resolve to
C:\Users\<USER>\fileman-storagein Windows, to
/home/<USER>/fileman-storagein Linux and to
Using a configuration module
If you wish to configure it with a module then it should look like this:
const path = ;const os = ;moduleexports =stordir: pathtempdir: path;
And then in your application, assuming it's located in
<APP_DIR>/config/fileman.js and you're calling it from a script located in
const config = ;fileman;
This method returns an Express middelware that intercepts POST or PUT
multipart/form-data requests only. This will save the uploaded temporal files to the specified
tempdir and will add the
fileinfo objects to
req.files as an
Array, even if just one file was uploaded, and attach the parsed form data fields to
req.body as an object with field names as properties. The fields are parsed as JSON whenever possible.
IMPORTANT: If any of the upload fails, the module will remove all the uploaded files on that request.
next callback you'll receive the parameters as usual but
req will now have
files properties as an
So if the user makes a
/api/files, like the example above, then the
req object will contain the corresponding
req.files and fields
This method returns an Express middleware that will clean all the files inside the
Array once the
res has finished so where you declare it is not really relevant.
If you do not wish to remove the temporal uploaded files after the request has finished then just don't use this middleware.
Use this method to save a file to a path relative to the configured
stordir. Useful for storing temporal uploaded files into it's definitive location.
It must be called with three parameters:
fileinfo: This is an
Objectcontaining the file's information, not the binary content, composed of the following properties:
- path: This is required and must be a
Stringwith the full path pointing to the file to read.
- name: An optional
Stringwith the file's original name.
- path: This is required and must be a
destpath: This is an optional
Stringpointing to a folder relative to the
stordir. If not passed then it'll save the files directly in the
done: A callback
Functionthat will receive an
nullon success, and a
const folder = '/folder/relative/to/stordir';const file =name: 'my-file.txt'path: '/full/path/to/the/file/my-file.txt';tryconst fileinfo = await fileman;// Check file infocatch err// Error!
All paths are normalized, meaning that if you pass
'..' as the folder it will save the file into the
stordir's parent folder and so on.
Reads a file from it's path relative to the
stordir folder and returns it's read
It's a good practice to set the correct and recommended response headers with the corresponding information before sending it:
As you may have noticed, the
fileinfo is retrieved from your database, meaning that if the files are modified, accessed or deleted outside the application then this information will be incorrect. Make sure you update your database entries accordingly.
Use this method is to obtain the file's full path relative to the
stordir folder. This can be particularly useful when using Express'
It receives only one argument that can be a
String with the relative path to the
stordir or a
Object with a valid
Object has the following properties:
- name: A
Stringwith the original file's name or
- path: A
Stringwith the file's path relative to the
- md5: A
Stringwith the file's MD5 hash.
- encoding: A
Stringwith the detected encoding.
- type: A
Stringwith the detected mimetype.
- stats: An
Objectwith the results of Node's
fs.statson the file.
name: Stringpath: Stringmd5: Stringencoding: Stringtype: Stringstats:dev: Numbermode: Numbernlink: Numberuid: Numbergid: Numberrdev: Numberblksize: Numberino: Numbersize: Numberblocks: Numberatime: Datemtime: Datectime: Datebirthtime: Date
Object is the result of a NodeJS'
fs.stat on the file.
If you need to know more about
fs.stats read the Node.js documentation and [System stats wiki](https://en.wikipedia.org/wiki/Stat_(system_call).