Last active
February 8, 2018 08:40
-
-
Save javafun/5bd04caa6991db682469 to your computer and use it in GitHub Desktop.
EPiServer - ContentReference Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.ServiceLocation; | |
using EPiServer7App.EPiServerCore.ContentTypes.Media; | |
using EPiServer7App.EPiServerCore.ContentTypes.Pages; | |
using EPiServer.Framework; | |
using System.IO; | |
using EPiServer7App.EPiServerCore.Model; | |
namespace EPiServer7App.EPiServerCore.Extensions | |
{ | |
/// <summary> | |
/// Provides extension methods for content references | |
/// </summary> | |
public static class ContentReferenceExtensions | |
{ | |
/// <summary> | |
/// Gets the top page reference, i e page beneath the start page, starting at the specified page reference | |
/// </summary> | |
public static PageReference GetTopPage(this PageReference pageLink) | |
{ | |
if (PageReference.IsNullOrEmpty(pageLink)) | |
{ | |
throw new NotSupportedException("Current top page cannot be retrieved without a starting point, and the specified page link was empty"); | |
} | |
var page = pageLink.GetPage(); | |
while (!PageReference.IsNullOrEmpty(page.ParentLink) && !page.ParentLink.CompareToIgnoreWorkID(ContentReference.RootPage) && !page.ParentLink.CompareToIgnoreWorkID(ContentReference.StartPage)) | |
{ | |
page = page.ParentLink.GetPage(); | |
} | |
return page.PageLink; | |
} | |
/// <summary> | |
/// Shorthand for DataFactory.Instance.GetPage | |
/// </summary> | |
public static PageData GetPage(this PageReference pageLink) | |
{ | |
return DataFactory.Instance.GetPage(pageLink); | |
} | |
public static T GetPage<T>(this ContentReference contentLink) where T : SitePageData | |
{ | |
return ServiceLocator.Current.GetInstance<IContentLoader>().Get<T>(contentLink); | |
} | |
/// <summary> | |
/// Convert <see cref="ContentReference"/> internal link to external | |
/// </summary> | |
public static string GetFriendlyUrl(this ContentReference contentLink) | |
{ | |
if (contentLink == null) | |
return string.Empty; | |
SitePageData pageData = GetPage<SitePageData>(contentLink); | |
if (pageData != null) | |
{ | |
UrlBuilder url = new UrlBuilder(pageData.LinkURL); | |
EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, pageData.PageLink, System.Text.UTF8Encoding.UTF8); | |
return url.ToString(); | |
} | |
return string.Empty; | |
} | |
/// <summary> | |
/// Get generic page shorthand | |
/// </summary> | |
public static T GetPage<T>(this PageReference pageLink, ILanguageSelector languageSelector = null) where T : PageData | |
{ | |
if (pageLink.CompareToIgnoreWorkID(ContentReference.RootPage)) | |
{ | |
throw new NotSupportedException("The root page cannot be converted to type " + typeof(T).Name); | |
} | |
var repository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
if (languageSelector != null) | |
return repository.Get<T>(pageLink, languageSelector); | |
return repository.Get<T>(pageLink); | |
} | |
public static IEnumerable<IContent> GetGetAncestors(this PageReference pageLink) | |
{ | |
var repository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
return repository.GetAncestors(pageLink); | |
} | |
/// <summary> | |
/// Get alt text from image data | |
/// </summary> | |
/// <param name="contentReference">ContentREference</param> | |
/// <returns>Alternative text otherwise empty string</returns> | |
public static string GetImageAlt(this ContentReference contentReference) | |
{ | |
if (contentReference == null) | |
return string.Empty; | |
IContentData contentData = ServiceLocator.Current.GetInstance<IContentRepository>().Get<IContent>(contentReference); | |
// Try to cast content data to image | |
ImageFile imgFile = contentData as ImageFile; | |
// Check whether the content is image file | |
if (imgFile != null) | |
return imgFile.AlternativeText; | |
return string.Empty; | |
} | |
/// <summary> | |
/// Get media file information | |
/// </summary> | |
/// <param name="contentReference">Current content reference</param> | |
/// <returns>An instance of <see cref="MediaInfo"/></returns> | |
public static MediaInfo GetMediaInfo(this ContentReference contentReference) | |
{ | |
if (contentReference == null) | |
return new EmptyMediaInfo(); | |
IContentData contentData = ServiceLocator.Current.GetInstance<IContentRepository>().Get<IContent>(contentReference); | |
MediaData data = contentData as MediaData; | |
// Check whether the content is a blob content | |
if (data == null) | |
return new EmptyMediaInfo(); | |
MediaInfo mediaInfo = new MediaInfo(); | |
mediaInfo.FileName = Path.GetFileNameWithoutExtension(data.BinaryData.ToString()); | |
mediaInfo.FileExt = Path.GetExtension(data.BinaryData.ToString()).TrimStart('.'); | |
mediaInfo.InternalFileSize = new Lazy<string>(() => Orchard.Utility.Format.FormatFileSize(data.BinaryData.OpenRead().Length, 1)); | |
return mediaInfo; | |
} | |
/** | |
* | |
* | |
/// <summary> | |
/// Shorthand for DataFactory.Instance.GetPage | |
/// </summary> | |
public static T GetPage<T>(this PageReference pageLink) where T : PageData | |
{ | |
if (pageLink.CompareToIgnoreWorkID(PageReference.RootPage)) | |
{ | |
throw new NotSupportedException("The root page cannot be converted to type " + typeof(T).Name); | |
} | |
return (T)DataFactory.Instance.GetPage(pageLink); | |
} | |
/// <summary> | |
/// Shorthand for DataFactory.Instance.GetPage | |
/// </summary> | |
public static PageData GetPage(this PageReference pageLink, ILanguageSelector languageSelector) | |
{ | |
return DataFactory.Instance.GetPage(pageLink, languageSelector); | |
} | |
**/ | |
} | |
} | |
using System; | |
namespace EPiServer7App.EPiServerCore.Model | |
{ | |
public class MediaInfo | |
{ | |
public virtual string FileName { get; set; } | |
public virtual string FileExt { get; set; } | |
public virtual string FileSize | |
{ | |
get | |
{ | |
if (InternalFileSize == null) | |
return string.Empty; | |
return InternalFileSize.Value; | |
} | |
} | |
internal Lazy<string> InternalFileSize { get; set; } | |
} | |
} | |
namespace EPiServer7App.EPiServerCore.Model | |
{ | |
public class EmptyMediaInfo : MediaInfo | |
{ | |
public EmptyMediaInfo() | |
{ | |
FileName = string.Empty; | |
FileExt = string.Empty; | |
InternalFileSize = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment