cancel
Showing results for 
Search instead for 
Did you mean: 

Question on how to customize media url generation

Former Member
4,189

Hi there,

I am trying to use a CDN for media items. The goal is to customize the generation of the URL field of Media items. I know that I have to use MediaURLStrategy to achieve this but unfortunately it does not work.

Here is what I did:

  • Implemented MediaURLStrategy interface in my custom url strategy

  • Configured a media folder in local.properties to use this custom url strategy

  • The custom url strategy does get called when I open a media item in the backoffice

  • Unfortunately only the downloadURL is being set via the strategy. The URL of the media item stays empty.

Now my question: How do I make sure that media items in the given folder get assigned the correct URL from the custom strategy?

I did not create a custom MediaStorageStrategy as I did not see why I would need one.

Thanks in advance!

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Ultimately I've found out that HMC and Backoffice seem to use the internalUrl field of the media items directly. The storefront however correctly makes use of the strategy with the given configuration. For the backoffice/hmc issue my workaround consisted of setting the internalUrl field of the media items to the actual url which makes the images visible there.

Answers (7)

Answers (7)

Former Member

As a workaround, you may try to override the DefaultMediaService and implement custom methods for "getUrlForMedia" and "getUrlForMediaVersion".

To elaborate on this: If you store all your media items in a special folder, you can use the folder qualifier to filter out your own media items. For these you can return your custom URL and for all other just call the super method and return the result.

Former Member

This is my relevant local.properties content:

 media.folder.cdn_folder.url.strategy=cloudinaryMediaURLStrategy
 media.folder.cdn_folder.url.template=http://res.cloudinary.com/censored/image/upload/%%asset-id%%.jpg

This is my bean definition:

 <bean id="cloudinaryMediaURLStrategy" class="de.censored.core.cdn.CloudinaryMediaURLStrategy" />

I am creating the media items from an xml import, where I first use modelService.create to create a MediaModel, then I set the folder for which the strategy is configured as well as the location property of the media model. Then I save it:

 final MediaModel media = getModelService().create(MediaModel.class);
                 media.setLocation(asset.getCode())
         media.setMediaFormat(mediaFormat);
         media.setFolder(censoredImportService.getMediaService().getFolder(ASSET_CDN_FOLDER_ID));
         media.setCode(mediaCode);
         media.setCatalogVersion(getTargetCatalogVersion());
                 modelService.save(media);
Former Member

This is my MediaUrlStrategy.

 package de.censored.core.cdn;
 
 import de.hybris.platform.media.MediaSource;
 import de.hybris.platform.media.storage.MediaStorageConfigService.MediaFolderConfig;
 import de.hybris.platform.media.url.MediaURLStrategy;
 
 import java.util.Objects;
 
 import org.apache.log4j.Logger;
 
 public class CloudinaryMediaURLStrategy implements MediaURLStrategy {
     private static final String CONFIG_URL_TEMPLATE_KEY = "url.template";
 
     private static final String DEFAULT_URL_TEMPLATE = "http://localhost/media/%%asset-id%%";
 
     private static final String ASSET_ID = "%%asset-id%%";
 
     private static final Logger LOG = Logger.getLogger(CloudinaryMediaURLStrategy.class);
 
     @Override
     public String getUrlForMedia(final MediaFolderConfig paramMediaFolderConfig, final MediaSource media) {
         Objects.requireNonNull(media, "Media is null.");
 
         return getUrlInternal(paramMediaFolderConfig, media);
     }
 
     private String getUrlInternal(final MediaFolderConfig paramMediaFolderConfig, final MediaSource mediaSource) {
         final String assetId = mediaSource.getLocation();
 
         Objects.requireNonNull(assetId, "AssetId is null");
 
         return getUrlPattern(paramMediaFolderConfig).replace(ASSET_ID, assetId);
     }
 
     private String getUrlPattern(final MediaFolderConfig config) {
         return config.getParameter(CONFIG_URL_TEMPLATE_KEY, String.class, DEFAULT_URL_TEMPLATE);
     }
 
 }
 
Former Member

Hi ,

Media Storage Strategy and Media URL Strategy are two different things.
MediaStorageStrategy: Performs actions on media data streams, like store, read and delete.
MediaURLStrategy: Renders public web URLs.

If You want to set custom Media URL Strategy, You have to:

  • Implement MediaURLStrategy and override getUrlFormMedia method.

  • Define bean of created Media Url strategy in *-spring.xml file.

  • Configure a media folder folder in local.properties:

media.folder.YourMediaFolderQualifierName.url.strategy=yourCustomMediaWebUrlStrategy

  • Change media folder e.g. from HMC->Multimedia->Media


If everything is set correctly after saving You can check Url address in:

  • General tab => properies = > URL

  • Metadata tab= > Metadata = >Download URL

  • Administration tab=> Unbound => URL

  • On Storefront page by checking media URL address (don't forget to clean browser's cache files)

Regards,
Artur

Former Member
0 Kudos

I am pretty new to hybris and not sure if it pacifies my requirements. I am looking forward to creating a custom field in product-cockpit. The field is a URL and I don't want to persist it. I just want to render the image. So, I have created a new item

    <attribute qualifier="imageServerUrl" type="Media">
                         <description>This is the Scene7 server URL</description>
                         <persistence type="dynamic" attributeHandler="productImageServerUrlAttributeHandler"/>
                         <modifiers read="true" write="false" optional="true" unique="false"/>
                     </attribute>


I have created a DynamicAttributeHandler which returns a MediaModel and have set all the URL class fields to the one I want

Also, added <property qualifier="product.imageServerUrl"/> in the editorArea_Product.xml , but it doesn't show up anything. Where does the Media URL strategy fit in?

0 Kudos

I am also facing the same issue.Updated URL is showing in HMC but not in the storefront.I have cleared the browser cache but still no luck.

Former Member
0 Kudos

See my reply below.

Former Member
0 Kudos

Unfortunately this is still unresolved. I apologize to the friendly case handler from SAP who called me. I was a little confused as I did not consider opening a thread in here as being equivalent to "opening a ticket". Of course it was me who created this "ticket".

Former Member
0 Kudos

Hello Artur,

Thank you for your reply!

I did exactly the things you mentioned for setting a custom media url strategy and like I said before it does work fine for the downloadURL. But unfortunately the "normal" url field of the media item does not get set and stays empty. Only the "downloadURL" field gets populated.

I am using Hybris 6 btw.

If anyone has any other ideas, please let me know.

Former Member
0 Kudos

Hi ,

On hybris 6.0 works the same.

Could You share Your version of Media Url Strategy?

Regards,
Artur

Former Member
0 Kudos

Sure, I added it as a reply to the main question.

Former Member
0 Kudos

I checked it and works well for me. Could You share strategy configuration from local.properties and bean definition?

Former Member
0 Kudos

Thanks again, I added the requested information as another reply.