Media library lifecycle hooks

Hey there!
I wanted to add beforeCreate() lifecycle hook to when I upload a media file (mostly .MP4).
Just needed a quick guide on where to write this code (including the path and the file name) . Also, the entire project is in Typescript.

This topic has been created from a Discord post (1219614502740426772) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

<@960590891200970812> any solution to this?

I have not done it yet, but you can you can checkout the docs here Models | Strapi Documentation you can register a subscriber and listen to events programmatically:

here is the way you would subscribe a life cycle hook to the upload plugin

  async bootstrap({ strapi }) {
    strapi.db.lifecycles.subscribe({
      models: ["plugin::upload.file"],

      beforeCreate(event) {

        const { data, where, select, populate } = event.params;

        console.log("before create fired with data: ", data);
        console.log(event);

        // event.state = "doStuffAfterWards";
      },
    });
  },
};

1 Like