The asset manager (am) is here to simplify your life concerning file in your applications.
npm install @gferrand/am-hook
npm i @gferrand/am-hook
`
... and that's it ! You can now import everything you need inside your project.
The basics
First, import the am.
`javascript
const AssetManager = require('@gferrand/am-hook');
`
Once it's imported you must initialize it inside your project, to do so, the am does have a method init() who take multiple parameters.
`javascript
AssetManager.init( $applicationName, $applicationVersion, $applicationPort, $applicationUrl, $masterUrl, $filePath, $consoleCbk );
`
They are all mandatory.
For the masterUrl, pass an empty string if you have none.
The filePath is the absolute path where the am will save your files.
The consoleCbk will return a message emitted by the library, it's your job to handle it, commons cases are just a console.log.
Next, let's see how it work.
The am expose differement methods :
`javascript
AssetManager.save($file);
`
`javascript
AssetManager.get($token);
`
`javascript
AssetManager.remove($token);
`
... and their plural counterparts :
`javascript
AssetManager.saves($arrayOfFiles);
`
`javascript
AssetManager.gets($arrayOfTokens);
`
`javascript
AssetManager.getsZipped($arrayOfTokens, $dictionnaryOfTokensAndFileNames);
`
`javascript
AssetManager.removes($arrayOfTokens);
`
All methods within the library return a promise.
Saving
To save files, either use one of the saves methods. This method only accept a file of this format :
`javascript
const file = {
name : yourFileName,
data : arrayBuffer
}
`
If your file is not an array buffer, look into the Buffer.from() method to convert it.
Once you're ready, call the am and pass the file object you created.
As it return a promise, your code must be prepare to handle asynchronous operation. You can either pick the '.then().catch()' way, or use the new sugar 'async/await'.
Either way, once the am is done saving your(s) file(s), it'll give you what we call a token, or an array of tokens.
What is a token ? A token is simply an auto generated id used by the library to find your files.
It's your responsability to save the token when you receive it, this is the only way to get your file back.
Getting
To get your files, either use one of the get methods.
They do accept a token, or an array of tokens. The tokens are the one provided straight after the save has been done.
Just like the saves methods, it'll return a promise, be sure to handle it properly.
The am will remove the file from the disk for you, you can now delete this token safely, it won't be reusable.
Getting zip
Return a zip of the files you claimed through the tokens.
The dictionnary must be an array of objects, and each object must respect the following structure :
`javascript
{
token : yourToken,
name : fileName
}
``