file-manager
This simple module is designed to be an abstraction for file storage. By default, it saves files to disk in a specified
directory, but it can easily be overridden to save files to anywhere you want, e.g. a database.
API
Constructor(storageDirectory)
storageDirectory
- The directory on the local disk where files will be stored. Usenull
to disable local disk storage.
isEnabled()
Returnstrue
if either a storage directory is set, or if save and read handlers have been registered.
saveFile(filename, contents)
filename
- Obvious
contents
- Either aBuffer
, or some other value that will have.toString()
called on it, then it will be converted to aBuffer
by interpreting the string as UTF-8
Promise
that will be fulfilled once the file is saved, or rejected if there's an error.
Alias: writeFile
saveFiles(files)
files
- An object where keys are filenames and values are file contents
Promise
that will be fulfilled once all files are saved, or rejected if there's an error saving any file
Alias: writeFiles
readFile(filename)
filename
- Obvious
Promise
that fulfills to its content, as a Buffer
. Rejects if there's an error or the file doesn't exist.
readFiles(filenames)
filenames
- Array of filenames
Promise
that fulfills to an array containing objects of this structure:
filename
- The name of this file
contents
- The content of this file, if reading succeeded
error
- AnError
object, if there was an error reading this file (e.g. it doesn't exist)
Events
You can register your own save/read handlers by attaching event listeners.read
filename
- The filename of the file we want to read
callback
- A function you should call once this file's contents are available, or an error occurred
- `err` - An `Error` object if there was an error reading this file (e.g. it doesn't exist). `null` if no error.
- `contents` - A `Buffer` containing the file's content, if reading succeeded
Emitted when a file is requested to be read.
save
filename
- The filename of the file we want to save
contents
- The content we want to save to this file, as aBuffer
callback
- A function you should call once this file has been saved, or an error occurred
- `err` - An `Error` object if there was an error saving this file. `null` if no error.
Emitted when a file is requested to be saved.
Properties
directory
The local disk storage directory location can be changed at any time by assigning to thedirectory
property.