private void GetBlurHash(ScriptExecutionContext context, JsValue input, Action <JsValue> callback, JsValue?componentX, JsValue?componentY)
        {
            Guard.NotNull(callback);

            context.Schedule(async(scheduler, ct) =>
            {
                if (input is not ObjectWrapper objectWrapper)
                {
                    scheduler.Run(callback, JsValue.FromObject(context.Engine, "ErrorNoAsset"));
                    return;
                }

                async Task ResolveHashAsync(AssetRef asset)
                {
                    if (asset.FileSize > 512_000 || asset.Type != AssetType.Image)
                    {
                        scheduler.Run(callback, JsValue.Null);
                        return;
                    }

                    var options = new BlurOptions();

                    if (componentX?.IsNumber() == true)
                    {
                        options.ComponentX = (int)componentX.AsNumber();
                    }

                    if (componentY?.IsNumber() == true)
                    {
                        options.ComponentX = (int)componentX.AsNumber();
                    }

                    var assetThumbnailGenerator = serviceProvider.GetRequiredService <IAssetThumbnailGenerator>();
                    var assetFileStore          = serviceProvider.GetRequiredService <IAssetFileStore>();
                    try
                    {
                        var hash = await asset.GetBlurHashAsync(options, assetFileStore, assetThumbnailGenerator, ct);

                        scheduler.Run(callback, JsValue.FromObject(context.Engine, hash));
                    }
                    catch
                    {
                        scheduler.Run(callback, JsValue.Null);
                    }
                }

                switch (objectWrapper.Target)
                {
                case IAssetEntity asset:
                    await ResolveHashAsync(asset.ToRef());
                    break;

                case EnrichedAssetEvent @event:
                    await ResolveHashAsync(@event.ToRef());
                    break;

                default:
                    scheduler.Run(callback, JsValue.FromObject(context.Engine, "ErrorNoAsset"));
                    break;
                }
            });
        }
Exemple #2
0
        private void AddAssetTextFilter()
        {
            TemplateContext.GlobalFilters.AddAsyncFilter("assetText", async(input, arguments, context) =>
            {
                if (input is not ObjectValue objectValue)
                {
                    return(ErrorNoAsset);
                }

                async Task <FluidValue> ResolveAssetTextAsync(AssetRef asset)
                {
                    if (asset.FileSize > 256_000)
                    {
                        return(ErrorTooBig);
                    }

                    var assetFileStore = serviceProvider.GetRequiredService <IAssetFileStore>();

                    var encoding = arguments.At(0).ToStringValue()?.ToUpperInvariant();
                    var encoded  = await asset.GetTextAsync(encoding, assetFileStore, default);

                    return(new StringValue(encoded));
                }

                switch (objectValue.ToObjectValue())
                {
                case IAssetEntity asset:
                    return(await ResolveAssetTextAsync(asset.ToRef()));

                case EnrichedAssetEvent @event:
                    return(await ResolveAssetTextAsync(@event.ToRef()));
                }

                return(ErrorNoAsset);
            });

            TemplateContext.GlobalFilters.AddAsyncFilter("assetBlurHash", async(input, arguments, context) =>
            {
                if (input is not ObjectValue objectValue)
                {
                    return(ErrorNoAsset);
                }

                async Task <FluidValue> ResolveAssetHashAsync(AssetRef asset)
                {
                    if (asset.FileSize > 512_000)
                    {
                        return(ErrorTooBig);
                    }

                    if (asset.Type != AssetType.Image)
                    {
                        return(ErrorNoImage);
                    }

                    var options = new BlurOptions();

                    var arg0 = arguments.At(0);
                    var arg1 = arguments.At(1);

                    if (arg0.Type == FluidValues.Number)
                    {
                        options.ComponentX = (int)arg0.ToNumberValue();
                    }

                    if (arg1.Type == FluidValues.Number)
                    {
                        options.ComponentX = (int)arg1.ToNumberValue();
                    }

                    var assetFileStore          = serviceProvider.GetRequiredService <IAssetFileStore>();
                    var assetThumbnailGenerator = serviceProvider.GetRequiredService <IAssetThumbnailGenerator>();

                    var blur = await asset.GetBlurHashAsync(options, assetFileStore, assetThumbnailGenerator, default);

                    return(new StringValue(blur));
                }

                switch (objectValue.ToObjectValue())
                {
                case IAssetEntity asset:
                    return(await ResolveAssetHashAsync(asset.ToRef()));

                case EnrichedAssetEvent @event:
                    return(await ResolveAssetHashAsync(@event.ToRef()));
                }

                return(ErrorNoAsset);
            });
        }