Daniel
Daniel

Reputation: 1205

Strapi - Media Library frontend side upload with user-specific access

I want to let authorized end users (non-admin) upload images to the media library straight from frontend (with no view restrictions - just public after upload).

How can I let the user see only his own folder during upload? Can I create a folder with ID of the user? How to create a folder programmatically? Is it even possible? Or I gotta use Cloudinary or S3?

Upvotes: 1

Views: 759

Answers (1)

Daniel
Daniel

Reputation: 1205

To extend functionality of Strapi plugin we need to copy the needed plugin from node_modules to src/extensions

In this particular example, we just want to modify `add() a little

src\extensions\upload\services\upload.js

async add(values, { user } = {}) {
  const fileValues = { ...values };

  if (user) {
    fileValues[UPDATED_BY_ATTRIBUTE] = user.id;
    fileValues[CREATED_BY_ATTRIBUTE] = user.id;
    fileValues.username = user.username; // <<< Here
  }
  sendMediaMetrics(fileValues);

  const res = await strapi.query(FILE_MODEL_UID).create({ data: fileValues });

  await this.emitEvent(MEDIA_CREATE, res);

  return res;
},

Maybe it won't be enough, I just don't remember all the steps, but I think this would do the trick. If not - just go through the chain of invocations and make sure user.username at this stage is not undefined and it'll work.

Upvotes: 1

Related Questions