I got the similar question as Modify image obtained from loopback-component-storage but the answer is not satisfactory to me.
Use the same example :
At client side
Upload.upload(
{
url: '/api/containers/container_name/upload',
file: file,
fileName: "demoImage.jpg",
//Additional data with file
params:{
username : username
}
});
Here is the requirement:
- Validate the file type of the file
- Modify the name saved in server
- Reject if invalid file or username
Here is some reference that I used and encountered :
The username is required, so it should be a user's method. I added a remote method like it is in https://stackoverflow.com/a/31118294/5241407 and changed the api into /api/usermodel/:id/upload. Some codes were add in remoteMethod to send username to
container.upload
function. Is it possible that we valid the file type and change the file name in this phase so that I can return error if the it is invalid ?I used configure-storage.js in boot file as it is in https://stackoverflow.com/a/31059880/5241407 But this solution is inadequate. If the client send invalid filetype the server will throw error, which is not expected. It is better to reject the query with a error response instead of throw a system error. So how to return error without throw a error in system level?
Once I upload a png file and checked the file.type in configure-storage.js, only to find that the file type is application/octet-stream instead of image/png. This is the code I used to probe:
curl -X POST --header "Content-Type: multipart/form-data" --header "Accept: application/json" "HOST/api/usermodel/userId/upload" -F "filename=@a.png"
This unexpected result may hinder the judgement of file type.