Media Library lifecycles

System Information
  • Strapi Version: 3.5.4
  • Operating System: macOS Catalina 10.15.7
  • Database: MongoDB v4.4.1
  • Node Version: v14.15.1
  • NPM Version: 6.14.8
  • Yarn Version: 1.22.10

Hi there :wave:,

Currently I’m trying to implement a library like Color Thief to find the dominant color of an image. I would like to hook into the Media Library when an image has been uploaded. This way Color Thief can get the dominant color and generate a new field for the image with the color hex.

I’ve tried adding the beforeCreate lifecycle at a new file located here: extensions/strapi-plugin-upload/models/File.js, but nothing is being triggerd when I upload a file.

module.exports = {
  lifecycles: {
    async beforeCreate(data) {
    async afterCreate(result, data) {

Any tips on how I can call the beforeCreate lifecycle hook after uploading an image? Thank you for all the help! :purple_heart:


Made a post way to quick!

For anyone who might stumble upon this post, I solved it by renaming strapi-plugin-upload to upload. That means the path is extensions/upload/models/File.js instead of extensions/strapi-plugin-upload/models/File.js

1 Like

Does this approach work with Strapi v4? I’m looking to do something similar but I think there have been quite a few changes to how plugin lifecycle hooks work in v4.

In v4 you can use this snippet to subscribe to lifecycle methods of any model.
You could put this snippet in your bootstrap function.

  models: [modalName, modalName2],
  async afterCreate(event) {
    // afterCreate lifeclcyle

  async afterCreateMany(event) {
    // afterCreateMany lifeclcyle

  async afterUpdate(event) {
    // afterUpdate lifeclcyle

  async afterUpdateMany(event) {
    // afterUpdateMany lifeclcyle

  async afterDelete(event) {
    // afterDelete lifeclcyle

  async afterDeleteMany(event) {
    // afterDeleteMany lifeclcyle

change your folder name

works fine