kontent-ai / delivery-sdk-net

Kontent.ai Delivery .NET SDK
https://www.nuget.org/packages/Kontent.Ai.Delivery
MIT License
32 stars 42 forks source link

Static method or example to change asset link domains #397

Open laststandjam opened 2 hours ago

laststandjam commented 2 hours ago

Motivation

Dedicated support for custom asset domains has long been missing from this SDK.

Proposed solution

Without dedicated support customers lack guidance on best practice solutions for implementing a substitution themselves. If we had a code sample or even a static method that they could make use of it would be a big help. Not just for the custom domain use case but secure access as well, as a user has expressed interest in replacing the URL with their own endpoint, with a file handler, to make calls to our API with the needed keys.

Example: Bet way to turn "https://assets-us-01.kc-usercontent.com/778b7b68-e044-0006-e0eb-333eb9503651/45be1c3f-a3b9-428d-bbc6-cb3ea381405e/istockphoto-518322790-1024x1024.jpg" => "https://myassetdomain/778b7b68-e044-0006-e0eb-333eb9503651/45be1c3f-a3b9-428d-bbc6-cb3ea381405e/istockphoto-518322790-1024x1024.jpg"

Additional context

Issue (https://github.com/kontent-ai/delivery-sdk-net/issues/381) Issue (https://github.com/kontent-ai/delivery-sdk-net/issues/311)

xantari commented 2 hours ago

Related: #193

xantari commented 1 hour ago

It appears that this might be as simple as allowing for string replacements from SDK configuration in this file: https://github.com/kontent-ai/delivery-sdk-net/blob/9327b2c924d187e5f422072f6b7ce64f6de6ee46/Kontent.Ai.Delivery/ContentItems/AssetElementValueConverter.cs#L33

Such that ResolveAssetUrl function does a string substitution for the *.kc-usercontent.com URLS with a custom URL.

Example:

SDK Configuration:

  1. CustomAssetUrl: https://www.somewhere.com/assets

ResolveAssetUrl would then take CustomAssetUrl and do a replace on the url:

        private string ResolveAssetUrl(IAsset asset)
        {
            var url = ReplaceAssetUrlWIthCustomAssetUrl(asset.Url);
            var renditionPresetToBeApplied = Options.CurrentValue.DefaultRenditionPreset;
            if (renditionPresetToBeApplied == null || asset.Renditions == null)
                return url;

            return asset.Renditions.TryGetValue(renditionPresetToBeApplied, out var renditionToBeApplied)
                ? $"{url}?{renditionToBeApplied.Query}"
                : url;
        }

       private string ReplaceAssetUrlWIthCustomAssetUrl(url)
       {
           if (!string.IsNullOrEmpty(SDKConfig.CustomAssetUrl))
           {
               //Replace the beginning part of the asset URL such as https://assets-us-01.kc-usercontent.com or https://preview-assets-us-01.kc-usercontent.com with the SDKConfig.CustomAssetUrl

               //Example above would change something like this:
               //https://preview-assets-us-01.kc-usercontent.com/406ac8c6-58e8-00b3-e3c1-0c312965deb2/ba10a763-a225-41d9-86d0-16ffb31b3be3/name-change-form.pdf
               //To this: https://www.somewhere.com/assets/406ac8c6-58e8-00b3-e3c1-0c312965deb2/ba10a763-a225-41d9-86d0-16ffb31b3be3/name-change-form.pdf
           }
       }