Service logic
Implementing the actual logic for managing the products' images.
Before entering the ProductsService
, we should add the FilesModule
to the imports
of the ProductsModule
. This way, the StorageService
will be available for injection in its context.
Now, let's actually implement these methods, starting with uploadImages()
. Let's begin by simply searching for the entity, but not doing anything with it. This is done just to check its existence.
We'll use certain path fragments many times, making it useful to have them as constants. So, let's create them in file.constants.
After that, back in the service, let's create the path
where the images will be saved.
Then, as we can save many images, let's ensure that the amount of images that arrived, combined with the amount already present in the directory, don't exceed the limit. However, we should first verify that the folder actually exists, lest an error will be thrown.
We checked everything and have the path
in hand. Let's then create the directory where the files will be stored, in case it does not exist yet.
Finally, let's save the images in parallel using Promise.all()
.
Here, map()
is used instead of forEach()
due to the latter's unstable behavior with promises.
Now, the downloadImage()
method. The step of searching for the entity is identical. However, the path
also includes the name of the file
to be downloaded.
Then, it is checked if the file
actually exists.
Finally, the file
is returned.
The last method is deleteImage()
, which is practically identical to downloadImage()
. The only difference is the last line, where the file is deleted.
One final improvement: let's make that, when removing a product
, its files-related folder is too. First, create an auxiliary method that performs this exclusion.
Then, invoke it in the remove()
method. However, so that both can happen in atomicity, we should wrap this in a transaction.
Ensuring that both database and file system operations are in sync can be challenging, but as the nature of our operations is very simple (database operation followed by file system operation), a transaction is enough.
We have finished implementing the file-related methods in the service.
Commit - Implementing file related methods in service
Last updated