Displaying file size for your MediaData

Creating a document link as file.pdf(230kB ) may seem like a simple task…

Well now it is…

My Generic media

public class GenericMedia : MediaData
{
[CultureSpecific]
[Display( Name = "Title-text",
Description = "Ange title-text",
GroupName = SystemTabNames.Content, Order = 2)] 

public virtual String Description { get; set; }
public virtual string Copyright { get; set; }
[Editable(false)]
public virtual string FileSize { get; set; } }

Helper to get the file size


 public static string GetFileSize(GenericMedia media)
        {
            if (media != null)
            {
                using (var stream = media.BinaryData.OpenRead())
                {
                    return (stream.Length /1024)+ " kB";
                }
            }
            return string.Empty;
        }

And som magic sprinkle of events


namespace proj.Business.Initialization
{
    [InitializableModule]
    [ModuleDependency(typeof (EPiServer.Web.InitializationModule))]
    public class InitializationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var eventRegistry =
            ServiceLocator.Current.GetInstance<IContentEvents>();

            eventRegistry.CreatingContent += OnCreatingContent;
            eventRegistry.SavingContent += OnSavingContent;

        }

        public void Preload(string[] parameters)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
            var eventRegistry =
            ServiceLocator.Current.GetInstance<IContentEvents>();

            eventRegistry.CreatingContent -= OnCreatingContent;
            eventRegistry.SavingContent -= OnSavingContent;

       }

        private void OnSavingContent(object sender, ContentEventArgs e)
        {
            var content = e.Content as GenericMedia;
            if (content == null)
                return;
            var fs = ContentHelpers.GetFileSize(content);
            content.FileSize = fs;
        }

        private static void OnCreatingContent(object sender, ContentEventArgs e)
        {
            var content = e.Content as GenericMedia;
            if (content == null)
                return;
            var fs = ContentHelpers.GetFileSize(content);
            content.FileSize = fs;
        }
    }
}

 

Usage


<a title="@content.Description" href="@UrlResolver.Current.GetUrl(content.ContentLink)">
@content.Name (@content.FileSize)</a>

Enjoy!

EPiServer 7 MVC Top menu

First project using EPiServer 7 with MVC4
Couldn't find much information regarding an two leveled top menu so I created this one,
please feel free to give feedback. It feels that this can be made easier.
This code resides in an partial view called TopNav.cshtml


@using EPiServer
@using EPiServer.Core
@using EPiServer.Filters
@using EPiServer.Web.Mvc.Html
@using n.Helpers
@inherits n.Models.BasePage
<nav class="menu main col100" role="navigation">
 <div class="inner">
 <ul class="greyDark">
 @{

 PageDataCollection allChildren = DataFactory.Instance.GetChildren(PageReference.StartPage);
 PageDataCollection allGrandChildren = new PageDataCollection();
 allChildren.Insert(0, PageReference.StartPage.GetPage()); //Add Startpage to collection
 IEnumerable<PageData> filteredChilds = FilterForVisitor.Filter(allChildren).Where(p => p.IsVisibleOnSite() && p.VisibleInMenu);

//First level
 foreach (PageData item in filteredChilds)
 {
 if (item.HasChildren() && item.GetPageReference() != PageReference.StartPage)
 {
 allGrandChildren.Add(DataFactory.Instance.GetChildren(item.GetPageReference())); //add this childs children for second level
 }

var act = string.Empty;
 <li>
 @if (CurrentPage.LinkURL == item.LinkURL || item.GetPageReference() == CurrentPage.ParentLink && CurrentPage.ParentLink != PageReference.StartPage)
 {
 act = "active";
 }
 <a class="@act" href="/@item.URLSegment.ToString()">
 @item.Name
 </a>
 </li>
 }
 }
 </ul>
 </div>
</nav>

<nav class="menu section col100" role="navigation">
 <div class="inner">
 <ul class="greyDark">
 @{

 IEnumerable<PageData> filteredGrandChildren = FilterForVisitor.Filter(allGrandChildren).Where(p => p.IsVisibleOnSite() && p.VisibleInMenu);
 //Second level
 foreach (PageData item in filteredGrandChildren.Where(p => p.ParentLink == CurrentPage.GetPageReference() || p.ParentLink == CurrentPage.ParentLink))
 {
 var act = string.Empty;
 <li>
 @if (CurrentPage.LinkURL == item.LinkURL)
 {
 act = "active";
 }
 <a class="@act" href="@Url.PageUrl(item.StaticLinkURL)">
 @item.Name
 </a>
 </li>
 }
 }
 </ul>
 </div>
</nav>