We have a requirement to provide thumbnail images in the price agent feeds instead of regular images.
We have a custom Price Agent Item Factory (and Price Agent) in place already, but the Base Price Agent Item Factory gives us pre-calculated “regular image” URLs and additional images URLs without any clear way to change this behavior.
Calculating our own image URLs using GetUrlToImage for each price agent item feels like it would slow down the price agent feed refresh way too much (~30000 variants). A refresh only takes ~20 seconds currently. Haven’t tested this though.
There are already thumbnails generated for each variant since thumbnails are used in the storefront.
We have considered using our own image links (e.g. /api/image/thumbnail/<variant_id>), and emit those in the feed instead, and the link would then fetch and render the image. But then we have to recreate functionality that’s already in place in the price agent item factory, i.e. finding the main image and the additional images and generating links to them. I’m not even sure how additional images are calculated.
What would be the best way to go about this? Ideally, we would go from an image URL to “same URL but for different size” without hitting the database or generating a thumbnail, since there are already URLs generated.
The GetUrlToImage method will not generate the image in the requested size but providing a path that contains the information that the system need to generate the image up on first request, if the requested size already exists it’s reused.
Creating an separate endpoint that generating the images is something that should be avoided to not complicate the evict caching rules.
The solution we opted for was in the PriceAgentItemFactory to generate the thumbnail URL using
_fileService
.Get(variantImageSystemId) // from VariantService and the Images field
.MapTo<ImageModel>()
.GetUrlToImage(
new Size(...),
new Size(...)
).Url;
And then add that URL to the feed in the actual IPriceAgent.
For performance optimization it will use less resources if you going directly from Guid to ImageModel as
variantImageSystemId // from VariantService and the Images field
.MapTo<ImageModel>()
.GetUrlToImage(
new Size(...),
new Size(...)
).Url
The reason is that both of the methods using an internal object named MediaLookupItem that contains the needed metadata for the image, this metadata is loaded into a cache during first access of any url generation for a file to avoid loading the File object into memory. Otherwise when using the File and loading one by one and iterating over the products the overhead to fetch the File object can be visible in the loading time if the item not already is cached.