Customize controller for uploading profile picture in user-permission

I am trying to create a customized controller to update the user’s profile picture. I send PUT request with form-data that contains the picture. The request goes 200 but the picture cannot be uploaded to the database. How to make it run correctly?

This is the controller:

  async updatepfp(ctx){
    const { id } = ctx.params;

    let entity;
    if (ctx.is('multipart')) {
      const { files, data } = parseMultipartData(ctx);
      entity = await strapi.query('user', 'users-permissions').update({ id: id }, data,{
        files,
      });

    } else {
      entity = await strapi.query('user', 'users-permissions').update({ id: id }, ctx.request.body);
    }

    entity = sanitizeUser(entity);
    return entity
  }

The route:

    {
      "method": "PUT",
      "path": "/users/updatepfp/:id",
      "handler": "User.updatepfp",
      "config": {
        "policies": [],
        "prefix": "",
        "description": "Update an existing user's pfp",
        "tag": {
          "plugin": "users-permissions",
          "name": "User",
          "actionType": "update"
        }
      }
    },

The request in Postman

On the other hand, I am also thinking to override the existed User.update controller for user-permission, but I can’t know the location of the controller. Where is it?

System Information
  • Strapi Version: 3.6.6
  • Operating System: Mac OS Big Sur 11.2.3
  • Database: MySQL
  • Node Version: v14.17.5
  • NPM Version: 6.14.14
  • Yarn Version: 1.22.17

Try this in the controller:

  async updatepfp(ctx){
    let entity;
    const id = ctx.state.user.id;

    if (ctx.is('multipart')) {
      const { data, files } = parseMultipartData(ctx);
      entity = await strapi.services.user.updateUser(id, {}, {files});
    } else {
      entity = await strapi.services.user.updateUser(id, ctx.request.body);
    }
    return sanitizeEntity(entity, { model: strapi.models.user });
  }

Then add a custom function in ./extensions/users-permissions/services/Users.js:

'use strict';

module.exports = {

  updateUser: async (id, data) => {
    return await strapi.query('user', 'users-permissions').update({id}, data)
  },

};

Thank you for your advice. But it gets a 500 error