files@1.0.0

files@1.0.0

Mantra component to manage files repositories

By @mantradev

Install with:

$ mantrad download-component files@1.0.0

Or install last version with:

$ mantrad download-component files

README.md

file Mantra component

This component defines files repositories to store from simple files repositories up to massive ones.

Each file is identified by and ID and is stored with a manifesto with the information of the file and optional metadata.

The component generates three files respositories:

  • Main files repository: for long term persistance files
  • Temp files repository: for temporal files that we'll be removed automatically periodically when "system.cleanup" event is raised by Mantra.
  • Generated files repository: for files that are generated when the application is running. It is emptied when the application starts.

file configuration values

The component needs the following configuration values:

{
    "filesRepositoryLocation": "<relative or absolute path for main repository, like ./filesrepository/files>",
    "filesTempRepositoryLocation": "<relative or absolute path to temporal repository, like ./filesrepository/tmp>",
    "filesGeneratedRepositoryLocation": "<relative or absolute path to generated repository, like ./filesrepository/generated>",
    "removetemponapp": "<name of the application in charge or removing temporal files>"
}

The component defines a number of API methods.

file API

async Mantra.api.files.addfilefromlocal ( Mantra, fullPathToFile )

Adds a file to the repository given its full local path.

Returns the file ID of the new file added.

async Mantra.api.files.appendtotempgeneratedfile( Mantra, params )

Appends a file to a temporal generated file where params is an object with these values:

{
    "fileId": <id of the file to append to>,
    "fullPathToFile": <full path to the the file to file id file>,
    "separation": <string to add between original and appended file (optional)>
}

async Mantra.api.files.existsfile( Mantra, fileId )

Returns true if a file exists given its id.

async Mantra.api.files.existstempgeneratedfile( Mantra, temFileId )

Returns true if a temporal generated file exists given its id.

async Mantra.api.files.getnewtempfile( Mantra, extension )

Creates a new temporal file defition and returns its manifesto given its extension.

Returns its json manifesto object:

{
    "fileId": "<ID of the file>",
    "length": -1,
    "extension": "<extension of the file indicated in parameter>",
    "created": <current date>,
    "location": "<full path to the file in the repository>"
}

async Mantra.api.files.getfilelocation( Mantra, fileId )

Returns the location of a file or "" if file doesn't exist. For performance reasons, the method doesn't check if file exists in files repository previously with the right method.

async Mantra.api.files.getfilemanifesto( Mantra, tempFileId )

Returns the manifesto for the temp file id indicated as parameter

async Mantra.api.files.getlinktofile( Mantra, fileId )

Returns a link to a file given its ID.

async Mantra.api.files.getnewtempgeneratedfile( Mantra, params )

Creates a new temporal generated file defition and returns its manifesto where params has these values:

{
    "extension": "<extension of the file (like 'jpg', 'png', etc.)>",
    "fileId": "<file id for the new file generated (optional)>"
}

Return the manifesto of the file from files-repo component

{
    "fileId": "<ID of the file>",
    "length": -1,
    "extension": "<extension of the file indicated in parameter>",
    "created": <current date>,
    "location": "<full path to the file in the repository>"
}

async Mantra.api.files.gettempfilelocation( Mantra, fileId )

Returns the location of a temp file or "" if file doesn't exist. For performance reasons, the method doesn't check if file exists in files repository previously with the right method.

async Mantra.api.files.gettempfilemanifesto( Mantra, fileId )

Returns the manifesto for the temp file id indicated as parameter.

async Mantra.api.files.movefromtemp( Mantra, fileTempId )

Moves a temporal file from temporal files repository to files repository.

Param:

  • fileTempId: "<id of the file in temp repository>"

Returns the new file ID of the file in repository.

async Mantra.api.files.removefile( Mantra, fileId )

Removes a file given its ID from repository.