Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static method or example to change asset link domains #397

Open
laststandjam opened this issue Nov 19, 2024 · 4 comments
Open

Static method or example to change asset link domains #397

laststandjam opened this issue Nov 19, 2024 · 4 comments

Comments

@laststandjam
Copy link

laststandjam commented Nov 19, 2024

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 (#381)
Issue (#311)

@xantari
Copy link

xantari commented Nov 19, 2024

Related: #193

@xantari
Copy link

xantari commented Nov 19, 2024

It appears that this might be as simple as allowing for string replacements from SDK configuration in this file:

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
           }
       }

@pokornyd
Copy link
Member

hello and thanks for your submission. you're correct, since the custom asset domains are not reflected directly in API response, the implementation relies upon string replacement at the SDK/app level. your proposed approach seems viable.

in practice, this would mean adjusting DeliveryOptions/DeliveryOptionsBuilder to allow specifying custom production/preview endpoints for assets and subsequently adjusting resolveAssetUrl method as you've suggested. feel free to submit your own pull request if you wish to participate on the SDK development, otherwise, I'll create an internal task for this and we'll handle it, though I can't promise this will be done before christmas.

@xantari
Copy link

xantari commented Nov 21, 2024

PR out there now: #398

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants