Hello,
Is there any way to access asset content via asset scripts? What I need is to create metadata with width and height for svg images.
One way to do it is to use getJSON function to use external api to determine width and height and then put it to metadata. Is any other option to do it?
Greetings.
I just added it to the docs: https://docs.squidex.io/id-02-documentation/developer-guides/scripting#helper-methods
I would use the getAssetText
method and a little bit of regex.
1 Like
Hello,
getAssetText always returns ânoAssetâ string, Iâve tested it with create asset script. When using id of asset that is already uploaded (and smaller than 4MB) also getting the same response.
Can you post your script?
Of course:
var id = ctx.assetId
// or var id = some_existing_svg_asset_id
if (ctx.asset.mimeType === 'image/svg+xml') {
getAssetText(id, function(result) {
ctx.command.metadata['content'] = result
ctx.command.metadata['id'] = id
replace();
});
}
Sorry, the documentation was wrong here.
It should be getAssetText(ctx.asset)
1 Like
I think this would be general extension, that would be a useful general extension to support metadata for svgs. If you want you can provide a pull request. You just have to implement this interface: https://github.com/Squidex/squidex/blob/master/backend/src/Squidex.Domain.Apps.Entities/Assets/IAssetMetadataSource.cs
1 Like
Now It works almostâŚ
I canât write metadata:
ctx.command.metadata[âpixelWidthâ] = width;
got error âFieldAccessExeptionâ, Iâm assuming that pixelWidth/Height is reserved metadata name and I canât write to it? Is any way to do it?
WHich version are you using?
{âversionâ:â7.12.0.0â}
Okay. I will have a look. Could be a limitation with the script engine.
1 Like
Thanks, I think itâs the case - writing metadata with name âpixelwidthâ works ok âpixelWidthâ returns an error.
I fixed the problem and added automatic SVG metadata extraction.