Copied to clipboard

Flag this post as spam?

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


  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Mar 31, 2019 @ 20:11
    Damiaan
    0

    Upload a new media item from code in v8

    I am trying to download a file and then upload it to the media section. It worked with v7.

    But I am not sure on how to fix this in v8.

    // add media item var newmedia = _mediaService.CreateMediaWithIdentity(photo.Title, -1, "image"); var destinationFolder = HostingEnvironment.MapPath("~/App_Data/FlickrTemp/"); System.IO.Directory.CreateDirectory(destinationFolder); var newFileName = Path.Combine(destinationFolder, photo.PhotoId + ".jpg"); // download the file new WebClient().DownloadFile(photo.LargeUrl, newFileName); var s = new FileStream(newFileName, FileMode.Open); // next line won't compile newmedia.SetValue("umbracoFile", Path.GetFileName(newFileName), s); // PLEASE HELP :-) s.Close(); _mediaService.Save(newmedia);

    I need a ContentTypeBaseServiceProvider. I don't know what it is and how to get it...

  • Tarik 196 posts 840 karma points c-trib
    Mar 31, 2019 @ 22:10
    Tarik
    100

    Damiaan, below is a class I used in UPillarv8 package.

    using Newtonsoft.Json.Linq;
    using System;
    using System.IO;
    using System.Web;
    using System.Xml;
    using System.Xml.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Models;
    using Umbraco.Core.PackageActions;
    using Umbraco.Core.Services;
    
    namespace Kacebee.ClassLibrary.PackageActions
    {
        public class AddMediaHandler : IPackageAction
        {
            IMediaService _mediaService = Current.Services.MediaService;
    
            public string Alias()
            {
                return "AddMediaHandler";
            }
    
            public bool Execute(string packageName, XElement xmlData)
            {
                var contentService = Current.Services.ContentService;
                var mediaTypeService = Current.Services.MediaTypeService;
                var mediaService = Current.Services.MediaService;
                var dataTypeService = Current.Services.DataTypeService;
                var fileService = Current.Services.FileService;
    
                try
                {
                    Current.Logger.Info(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "Executing CreateMediaHandler");
                    return AddMediaItems();
                }
                catch (Exception ex)
                {
                    Current.Logger.Error(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "INSTALL Package Error", ex);
                    return false;
                }
            }
    
            public bool AddMediaItems()
            {
                var contentService = Current.Services.ContentService;
                var mediaTypeService = Current.Services.MediaTypeService;
                var mediaService = Current.Services.MediaService;
                var dataTypeService = Current.Services.DataTypeService;
                var fileService = Current.Services.FileService;
                var contentTypeBaseServiceProvider = Current.Services.ContentTypeBaseServices;
    
                try
                {
                    int parentId = -1;
                    IMedia profileImage = AddMediaItem(parentId, "Profile.png", contentTypeBaseServiceProvider);
    
                    Guid homeId = new Guid("777f5bc3-8f24-4dcb-b727-3b2e4473927e");
                    IContent homePage = contentService.GetById(homeId);
    
                    homePage.SetValue("siteLogo", profileImage.GetUdi().ToString());
                    contentService.SaveAndPublish(homePage);
    
                    if (homePage != null)
                    {
                        contentService.SaveAndPublishBranch(homePage, true);
                    }
    
                    Current.Logger.Info(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "Add media items");
                    return true;
                }
                catch (Exception ex)
                {
                    Current.Logger.Error(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "Error when adding media items.", ex);
                    return false;
                }
            }
    
            private IMedia AddMediaItem(int parentId, string fileName, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider)
            {
                IMedia newFile = _mediaService.CreateMedia(fileName, parentId, "Image");
                try
                {
                    string filePath = HttpContext.Current.Server.MapPath("~/img/" + fileName);
                    using (FileStream stream = System.IO.File.Open(filePath, FileMode.Open))
                    {
                        newFile.SetValue(contentTypeBaseServiceProvider, "umbracoFile", fileName, stream);
                    }
                    _mediaService.Save(newFile);
                    Current.Logger.Info(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "Add image");
                    return newFile;
                }
                catch (Exception ex)
                {
                    Current.Logger.Error(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "Error when adding image", ex);
                    return newFile;
                }
            }
    
            public XmlNode SampleXml()
            {
                const string sample = "<Action runat=\"install\" undo=\"true\" alias=\"AddMediaHandler\"></Action>";
                return ParseStringToXmlNode(sample);
            }
    
            private static XmlNode ParseStringToXmlNode(string value)
            {
                var xmlDocument = new XmlDocument();
                var xmlNode = AddTextNode(xmlDocument, "error", "");
    
                try
                {
                    xmlDocument.LoadXml(value);
                    return xmlDocument.SelectSingleNode(".");
                }
                catch
                {
                    return xmlNode;
                }
            }
    
            private static XmlNode AddTextNode(XmlDocument xmlDocument, string name, string value)
            {
                var node = xmlDocument.CreateNode(XmlNodeType.Element, name, "");
                node.AppendChild(xmlDocument.CreateTextNode(value));
                return node;
            }
    
            public bool Undo(string packageName, XElement xmlData)
            {
                return true;
            }
        }
    }
    
  • Rabea 34 posts 186 karma points
    May 05, 2019 @ 20:09
    Rabea
    0

    Hi @Tarik, I have used the method you have suggested but i end up with a blank file uploaded and the path set as following: enter image description here

    Any idea what i might be doing wrong?

  • Tarik 196 posts 840 karma points c-trib
    May 06, 2019 @ 12:29
    Tarik
    0

    Rabea, peace be upon those who follow guidance.

    Did you create an img folder and added the picture there and used the siteLogo alias ?

    What part of code you utilize ?

  • Rabea 34 posts 186 karma points
    May 06, 2019 @ 12:32
Please Sign in or register to post replies

Write your reply to:

Draft