title: Validate and Rename Image file from loopback-component-storage
body: <p>I got the similar question as <a href="https://stackoverflow.com/questions/31048618/modify-image-obtained-from-loopback-component-storage">Modify image obtained from loopback-component-storage</a> but the answer is not satisfactory to me.</p>
  
  <p>Use the same example : </p>
  
  <blockquote>
    <p>At client side</p>
  </blockquote>
  
  <pre class="lang-js prettyprint-override"><code>Upload.upload(
  {
      url: '/api/containers/container_name/upload',
      file: file,
      fileName: "demoImage.jpg",
      //Additional data with file
      params:{
       username : username
      }
  });
  </code></pre>
  
  <p>Here is the requirement:</p>
  
  <ol>
  <li>Validate the file type of the file </li>
  <li>Modify the name saved in server</li>
  <li>Reject if invalid file or username</li>
  </ol>
  
  <p>Here is some reference that I used and encountered :  </p>
  
  <ul>
  <li><p>The username is required, so it should be a user's method. I added a remote method like it is in <a href="https://stackoverflow.com/a/31118294/5241407">https://stackoverflow.com/a/31118294/5241407</a> and changed the api into /api/usermodel/:id/upload. Some codes were add in remoteMethod to send username to <code>container.upload</code> 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 ?</p></li>
  <li><p>I used configure-storage.js in boot file as it is in <a href="https://stackoverflow.com/a/31059880/5241407">https://stackoverflow.com/a/31059880/5241407</a> 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?</p></li>
  <li><p>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: <code>curl -X POST --header "Content-Type: multipart/form-data" --header "Accept: application/json" "HOST/api/usermodel/userId/upload" -F "filename=@a.png"</code> This unexpected result may hinder the judgement of file type.</p></li>
  </ul>