Reputation: 1245
I have following tree structure in Sitecore 7
Content
Site 1
page 1
Site 2
page 1
Media Library
Site 1 media files
pdf
1.pdf
Site 2 media files
pdf
1.pdf
Media library is a shared library and looks like user can access site 2 media files from site 1 and site 1 media files from site 2, Can I stop this behavior?
I want if I reference site 2 files from site 1 pages, generated link should open them from site 2 domain.
for example currently If I am browsing page 1 of site 1 and that has a link to 1.pdf of site 2, following link is generated
http://site1/~media/site2/pdf/1.pdf
link is working properly but site 2 files is being served by site 1 domain I want url of the file should be as following
http://site2/~media/site2/pdf/1.pdf
Node:I have a custom link provider and that works very well for pages across the sites.
Upvotes: 1
Views: 290
Reputation: 27142
You can create your custom Media Provider and register it instead of default one:
<mediaLibrary>
<mediaProvider type="My.Assembly.Namespace.CustomMediaProvider, My.Assembly" />
</mediaLibrary>
public class CustomMediaProvider : MediaProvider
{
public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
{
string mediaUrl = base.GetMediaUrl(item, options);
if (mediaUrl == null || mediaUrl.StartsWith("http"))
{
return mediaUrl;
}
string[] parts = mediaUrl.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 2)
{
// skip "~" and "media" parts
string mediaLibraryTopFolder = parts[2];
// assuming that folder names are site1 and site2
SiteInfo siteInfo = SiteContextFactory.GetSiteInfo(mediaLibraryTopFolder);
if (siteInfo != null)
{
SiteContext siteContext = new SiteContext(siteInfo);
// change logic if you need https
mediaUrl = "http://" + siteContext.HostName + "/" + mediaUrl.TrimStart('/');
}
}
return mediaUrl;
}
}
This code is not tested, but if the media item link does not contain host name yet, it should get the top level folder from the media path, get the site with same name as folder name and add proper host name at the beginning of the path.
Remember that this will change all your media links so they will include http
and host name and that this will work only for links generated by the MediaManager
or MediaProvider
.
Upvotes: 0