Storage service
A dedicated service for managing files.
A service for the purpose of managing files may prove quite useful. The overall structure was inspired by this article. What we'll first do then is to generate a files module and a storage service.
Here, we'll adopt an ideia similar to the HashingService
. That is, to have an abstract class with the methods' signatures and a concrete class with their respective implementations. So, in the StorageService
, we should have signatures to:
Save a file
Create a directory
Get a file
Get the names of files inside a directory
Calculate the amount of files inside a directory
Delete a file or directory
Validate a path
Each method will be better detailed during its implementation. Therefore, let's proceed to implement the concrete class.
Node, by default, has the fs module for managing files. However, the fs-extra package has the same functionalities and more features and improvements. It's already installed, so let's just add its typing.
Now, we can create a concrete service that implements the methods of the StorageService
using fs-extra. After creating it, we should make it implement the StorageService
.
Let's also already adjust the providers
in the FilesModule
. Remember to add to the exports
, the StorageService
.
In file.constants, let's define a constant for the base directory where all the files will be stored, which will be the upload folder at the root of the project.
Also add this folder in the .gitignore file, as we won't publish files to the code repository.
Let's then, finally, implement each one of the methods in the FseService
.
saveFile()
- Receives thepath
and thefile
, and then saves it in thispath
prepended with theBASE_PATH
, with the file'soriginalname
, from itsbuffer
The join()
function combines many paths into one, in a safe manner.
createDir()
- The functionmkdirp()
allows to create folders and even nested ones
If the path already exists, simply doesn't do anything.
getFile()
- This function returns the file as aStreamableFile
, which allows for more flexibility if desired (further documentation)
getDirFilenames()
- Obtains the names of files inside a folder
getDirFilecount()
- Counts how many files are inside a folder using the previous method
Notice that the fullPath
is not obtained here: it is in the previous method.
delete()
- Deletes a file or folder
The remove()
function also allows for removing folders with contents.
If the path does not exist, simply doesn't do anything.
validatePath()
- Checks apath
, throwing an exception if it does not exist
And we're done! We now have a quite interesting set of util functions to aid us when managing files.
Commit - Implementing file storage functions
Last updated