Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Søren Kottal 530 posts 3521 karma points MVP 2x c-trib
    Oct 01, 2014 @ 15:27
    Søren Kottal
    0

    Create custom MediaFactory

    Not sure if i'm in the right forum, but maybe you can help me.

     

    I have created two custom media types (one for pdf/doc/docx/etc. and one for mp4/ogg/flv/etc), and would like to have the standard upload fuctionality to map uploads to these media types.

     

    Googling led me to create a custom mediafactory, but how do I get it to work?

     

    I tried making a .cs file in the App_Code directory, but all that resulted in was a YSOD telling me that 'FileSystem' doesn't exist.

     

    Heres my code

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web;
    using umbraco.BusinessLogic;
    using Umbraco.Core;
    using Umbraco.Core.IO;
    
    
    namespace umbraco.cms.businesslogic.media
    {
        public class UmbracoDocumentMediaFactory : UmbracoMediaFactory
        {
            public override string MediaTypeAlias
            {
                get { return "Document"; }
            }
    
            public override List Extensions
            {
                get { return new List { "pdf", "doc", "docx", "rtf", "xls", "xlsx", "ppt", "pptx" }; }
            }
    
            public override void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user)
            {
                // Get umbracoFile property
                var propertyId = media.getProperty(Constants.Conventions.Media.File).Id;
    
                // Get paths
                var destFilePath = FileSystem.GetRelativePath(propertyId, uploadedFile.FileName);
                var ext = Path.GetExtension(destFilePath).Substring(1);
    
                //var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
                //var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
    
                // Set media properties
                media.getProperty(Constants.Conventions.Media.File).Value = FileSystem.GetUrl(destFilePath);
                media.getProperty(Constants.Conventions.Media.Bytes).Value = uploadedFile.ContentLength;
    
                if (media.getProperty(Constants.Conventions.Media.Extension) != null)
                    media.getProperty(Constants.Conventions.Media.Extension).Value = ext;
    
                // Legacy: The 'extensio' typo applied to MySQL (bug in install script, prior to v4.6.x)
                if (media.getProperty("umbracoExtensio") != null)
                    media.getProperty("umbracoExtensio").Value = ext;
    
                FileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting);
    
                // Save media
                media.Save();
            }
        }
        public class UmbracoVideoMediaFactory : UmbracoMediaFactory
        {
            public override string MediaTypeAlias
            {
                get { return "Video"; }
            }
    
            public override List Extensions
            {
                get { return new List { "mp4", "ogg", "webm", "flv" }; }
            }
    
            public override void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user)
            {
                // Get umbracoFile property
                var propertyId = media.getProperty(Constants.Conventions.Media.File).Id;
    
                // Get paths
                var destFilePath = FileSystem.GetRelativePath(propertyId, uploadedFile.FileName);
                var ext = Path.GetExtension(destFilePath).Substring(1);
    
                //var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
                //var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
    
                // Set media properties
                media.getProperty(ext).Value = FileSystem.GetUrl(destFilePath);
                //media.getProperty(Constants.Conventions.Media.Bytes).Value = uploadedFile.ContentLength;
    
                if (media.getProperty(Constants.Conventions.Media.Extension) != null)
                    media.getProperty(Constants.Conventions.Media.Extension).Value = ext;
    
                // Legacy: The 'extensio' typo applied to MySQL (bug in install script, prior to v4.6.x)
                if (media.getProperty("umbracoExtensio") != null)
                    media.getProperty("umbracoExtensio").Value = ext;
    
                FileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting);
    
                // Save media
                media.Save();
            }
        }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies