getAssetBlurHash asset script example

I have…

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [ ] Bug report
  • [ ] Performance issue
  • [x] Documentation issue or request

Current behavior

I am looking for an example for how to update a metadata property on an asset with its BlurHash when an asset is created or updated.

Expected behavior

It would be nice to have this example in the docs or in the BlurHash announcement. Ideally a blurHash property of metadata could be set.

Minimal reproduction of the problem

This code does not work:

getAssetBlurHash(ctx.asset, (res)=>{
    ctx.asset.metadata.blurHash = res;    
}, 5, 5);

Environment

App Name:

  • [ ] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [x] Cloud version

Version: [VERSION]

Browser:

  • [x] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [ ] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [ ] Edge

Others:
That is all.

Sure, I can add something. But your use case is wrong. The idea is to put the blur hash in the content that references the asset. Because then you can show the blur hash after the content is loaded and before the asset is loaded,

e.g. in your schema update script:

getAssets(data.assets.iv, function (assets) {
    getAssetBlurHash(assets[0], function (hash) {
         ctx.data.blurHash.iv = hash;
         replace();
    });
});

Thanks @Sebastian. I am storing alt text for images in metadata and am doing additional calls to get that data which is why setting a metadata value would work in this case for me. I was hoping to offload this to the asset script instead of schema, but the code at least gets me thinking about my options.

I have a lot of schemas that reference assets. Those return only ids for the assets being referenced. I architected my site around this before fully understanding how to fill out the content being returned using a script.

The problem is that getAssetBlurHash requires an asset as input. There are a lot of reasons. But the asset script is triggered before the asset is actually created. So either we have to introduce new script triggers or rule triggers or something like this or we have to rebuilt the whole asset creation flow. The second option is not so realistic.

Would it work for the asset update script since by then the asset exists? Or, am I wrong in that the asset update script runs when an image itself is updated/replaced and not the metadata of the image?

Yes, you are referring to annotate event. And what happens with the first version?

Is it possible to update an asset via scripts or is that only possible for content? It would be nice to be able to add a rule when an asset is updated to retain a metadata value from the previous version. Setting a metadata value for blurHash would be ideal so I don’t have to modify my content schema. Especially when a content schema references multiple images.

Only for content so far. There are several alternatives how to implement that:

  1. Just calculate the blur hash by default
  2. Add a new scripting method to update the blur hash in a rule.
  3. Try to get the getAssetBlurHash working in the normal asset scripts.

I’ve tried 2 and 3 with no success. I can’t seem to debug the scripts in Squidex Cloud. Perhaps I’m missing something.

I am talking about implementation from my side :wink:

1 Like

I almost have a fix working for that and also a updateAsset action.

So you will be able to do …

  1. Calculate the blur hash in the asset script:
if (ctx.asset.type === 'Image') {
    getAssetBlurHash(ctx.asset, function (hash) {
        ctx.command.metadata['blurHash'] = hash;
    });
}

or

  1. Calculate the blur hash asynchronously in a rule
getAssetBlurHash(ctx.asset, function (hash) {
    var metadata = { ...event.metadata, hash };

    updateAsset(event, metadata);
});

e.g. with a filter

event.type === 'Asset'