Upload Files
How to upload files to the system.
We know how important it is to be able to upload files to the system. Our functions can help you implement upload files with ease.
Storage files outside local
For now VitNode only supports local storage for files. We'll be working on supporting other storage options like S3, Google Cloud Storage, etc. in the feature.
Create Controller
Create a controller to handle the file upload. Use the @UploadFilesMethod
decorator to handle the file upload and pass all files name in the fields
array.
This decorator will automatically change consumers to multipart/form-data
and handle the files upload.
Get files form body
To get the files from the body, you can use the @UploadedFiles
decorator with FilesValidationPipe
to validate the files. As an example below, we validate the icon
field with a maximum size of 1MB, accept only image files, and allow only one file.
Below you can see other options for the FilesValidationPipe
:
Handle upload files
Now you can handle the upload files in your service. As an example we will add files and also body data.
Below you can see the service example:
We omit the icon
field from the body
because any file doesn't exist in the body. We need to handle the file separately.
Upload file
Now you can upload the files to the storage by using the FilesHelperService
and the upload
method.
Secure file
As default each files will save as public and will be visible for everyone. If you want to save the file as secure, you can pass the secure
option as true
.
Secure files will be saved in a private
folder inside uploads
folder in
backend
and will be accessible only by the system. You need to handle the
access to the file by yourself.
Handle the response
filesHelper.upload
will return an object with the following properties:
dir_folder
- The folder where the file is saved.extension
- The file extension.file_name
- The file name.file_name_original
- The original file name.file_size
- The file size in bytes.height
- The height of the image file.mimetype
- The file mimetype.secure
- If the file is secure.width
- The width of the image file.
Default URL for the public
file will be: {backend_url}/public/{dir_folder}/{file_name}
.
VitNode will not save any information about the file. You need to handle the file information by yourself for example in the database.
Delete file
To remove the file, you can use the FilesHelperService
and the delete
method.
Delete secure file
If you want to delete a secure file, you need to pass the secure
option as true
.