emil-petras / strapi-blurhash

A plugin for Strapi CMS that generates blurhash for your uploaded images
https://www.npmjs.com/package/strapi-blurhash
20 stars 16 forks source link

Add check for where before checking for regenerateOnUpdate #8

Closed tirthbodawala closed 1 year ago

tirthbodawala commented 1 year ago

Solves: https://github.com/emil-petras/strapi-blurhash/issues/7

tirthbodawala commented 1 year ago

@emil-petras any updates on the above?

emil-petras commented 1 year ago

@tirthbodawala I confirmed that there is an issue. The query returns null because during image create, it doesn't yet exist. So ideally, there should be a check whether the event is 'beforeCreate'. The same situation doesn't happen in 'beforeUpdate'.

So just add 'eventType' parameter to the function. Add an 'if' that runs the blurhash update only if the event type equals to 'beforeCreate'.

And change the subscribe to this:

strapi.db.lifecycles.subscribe({ models: ['plugin::upload.file'], beforeCreate: (event) => generateBlurhash(event, 'beforeCreate'), beforeUpdate: (event) => generateBlurhash(event, 'beforeUpdate'), });