/// <summary>
 /// Accesses the <see cref="MediaTypeAttribute"/> applied to a class to find
 /// the media type alias for that class
 /// </summary>
 /// <param name="input">The media type instance to get the alias for</param>
 /// <returns>the media type alias</returns>
 /// <exception cref="CodeFirstException">Thrown if the specified type does not have a <see cref="MediaTypeAttribute"/> attribute.</exception>
 public static string GetMediaTypeAlias(this MediaTypeBase input)
 {
     try
     {
         return(input.GetType().GetCodeFirstAttribute <MediaTypeAttribute>().Alias);
     }
     catch (Exception e)
     {
         throw new CodeFirstException(input.GetType().Name, e);
     }
 }
        public void ProjectModelToContent(MediaTypeBase model, IMedia content)
        {
            var type = model.GetType();
            MediaTypeRegistration reg;

            if (_mediaTypeModule.TryGetMediaType(type, out reg) && (reg.ClrType == type || reg.ClrType.Inherits(type)))
            {
                MapModelToContent(content, model, reg);
                if (model.MediaFile != null && content.HasProperty("umbracoFile"))
                {
                    content.SetValue("umbracoFile", System.IO.Path.GetFileNameWithoutExtension(model.MediaFile.Name), model.MediaFile);
                }
            }
        }
        /// <summary>
        /// <para>Creates an IContent populated with the current values of the model</para>
        /// </summary>
        private IMedia CreateContent(int parentId, MediaTypeBase model, ContentTypeRegistration registration)
        {
            //Get the type alias and create the content
            var typeAlias = registration.Alias;
            var node      = ApplicationContext.Current.Services.MediaService.CreateMedia(model.NodeDetails.Name, parentId, typeAlias);

            MapModelToContent(node, model, registration);
            if (model.MediaFile != null && node.HasProperty("umbracoFile"))
            {
                var fn = System.IO.Path.GetFileName(model.MediaFile.Name);
                node.SetValue("umbracoFile", fn, model.MediaFile);
            }
            return(node);
        }
        /// <summary>
        /// Updates an existing IContent item with the current values of the model
        /// </summary>
        /// <returns></returns>
        private IMedia UpdateContent(MediaTypeBase model, ContentTypeRegistration registration)
        {
            if (model.NodeDetails == null || model.NodeDetails.UmbracoId == -1)
            {
                throw new ArgumentException("Can't update content for a model with no ID. Try calling CreateContent instead. Check that the NodeDetails.UmbracoId property is set before calling UpdateContent.");
            }
            var node = ApplicationContext.Current.Services.MediaService.GetById(model.NodeDetails.UmbracoId);

            MapModelToContent(node, model, registration);
            if (model.MediaFile != null && node.HasProperty("umbracoFile"))
            {
                node.SetValue("umbracoFile", System.IO.Path.GetFileNameWithoutExtension(model.MediaFile.Name), model.MediaFile);
            }
            return(node);
        }
        public IMedia ConvertToContent(MediaTypeBase model, int parentId = -1)
        {
            var contentId = model.NodeDetails.UmbracoId;
            MediaTypeRegistration registration;

            _mediaTypeModule.TryGetMediaType(model.GetType(), out registration);
            if (registration == null)
            {
                throw new CodeFirstException("Media type not registered. Type: " + model.GetType());
            }

            //Create or update object
            if (contentId == -1)
            {
                return(CreateContent(parentId, model, registration));
            }
            else
            {
                return(UpdateContent(model, registration));
            }
        }