Copied to clipboard

Flag this post as spam?

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


  • Luis Pedro 14 posts 135 karma points
    1 week ago
    Luis Pedro
    0

    Creating media on the fly

    Hey,

    I've extended my 'onsave' button to, depending on the document type and if a property it's filed, grab some online data from a JSON API (getting among other stuff, a URL to a live image).

    How can I create one media image on the fly, when saving the document (from an IUserComposer extension) ?


    (I just need help for the Umbraco Media parts, using what I guess it is the Media Service in V8)

    TIA /LPP

  • Sebastiaan Janssen 4805 posts 13980 karma points MVP admin hq
    1 week ago
    Sebastiaan Janssen
    0

    Here's a bit of code for when I imported my old blog content into v8, should help, mainly the last part I guess - make sure to get a stream from the file and then you can indeed use the MediaService to save it:

        public GuidUdi CreateMediaImageFromMediaId(int mediaId, IEnumerable<MediaItem> mediaItems, string nodeName)
        {
            var mediaFile = mediaItems.FirstOrDefault(x => x.Id == mediaId);
            if (mediaFile == null)
                return null;
    
            var url = $"https://cultiv.nl/{mediaFile.UmbracoFile}";
    
            return CreateMediaImageFromUrl(url, nodeName);
        }
    
        private GuidUdi CreateMediaImageFromUrl(string url, string nodeName)
        {
            var downloadPath = IOHelper.MapPath("~/App_Data/TEMP/DownloadedFiles");
            if (Directory.Exists(downloadPath) == false)
                Directory.CreateDirectory(downloadPath);
    
            var fileName = url.Substring(url.LastIndexOf('/') + 1);
            if (fileName.Contains("?"))
                fileName = fileName.Substring(0, fileName.IndexOf("?"));
    
            var filePath = $"{downloadPath}/{fileName}";
            if (File.Exists(filePath) == false)
                using (var client = new WebClient())
                    try
                    {
                        client.DownloadFile(url, filePath);
                    }
                    catch (Exception e)
                    {
    
                    }
    
            if (File.Exists(filePath) == false)
                return null;
    
            var blogPostsRoot = _helper.MediaAtRoot().FirstOrDefault(x => x.Name == "Blog Posts");
            Guid blogPostsRootFolderId;
            if (blogPostsRoot == null)
            {
                var blogPostsRootFolder = _mediaService.CreateMedia("Blog Posts", -1, "Folder");
                _mediaService.Save(blogPostsRootFolder, userId: -1);
                blogPostsRootFolderId = blogPostsRootFolder.Key;
            }
            else
            {
                blogPostsRootFolderId = blogPostsRoot.Key;
            }
    
            var thisBlogPostRoot = _helper.Media(blogPostsRootFolderId).Children.FirstOrDefault(x => x.Name == nodeName);
            Guid thisBlogPostsRootFolderId;
            if (thisBlogPostRoot == null)
            {
                var thisBlogPostsRootFolder = _mediaService.CreateMedia(nodeName, blogPostsRootFolderId, "Folder");
                _mediaService.Save(thisBlogPostsRootFolder, userId: -1);
                thisBlogPostsRootFolderId = thisBlogPostsRootFolder.Key;
            }
            else
            {
                thisBlogPostsRootFolderId = thisBlogPostRoot.Key;
            }
    
            using (var imageStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                // Stream the file into a new umbraco media item
                var imageMedia = _mediaService.CreateMedia(fileName, thisBlogPostsRootFolderId, "Image");
                imageMedia.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", fileName, imageStream);
                _mediaService.Save(imageMedia, -1);
                return new GuidUdi("media", imageMedia.Key);
            }
        }
    
  • Luis Pedro 14 posts 135 karma points
    1 week ago
    Luis Pedro
    0

    Thanks Sebastiaan

    Having a difficulty crossing this 'lines'.

    _mediaService and _contentTypeBaseServiceProvider declaration from y ContentService.IUserComposer extension

    enter image description here

  • Sebastiaan Janssen 4805 posts 13980 karma points MVP admin hq
    1 week ago
    Sebastiaan Janssen
    100

    Use dependency injection. :-)

    So if your class is named Importer, create a new constructor public Importer() { .. } and ask for the things you need:

    public class Importer
    {
        private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
        private readonly IMediaService _mediaService;
    
        public Importer(
            IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
            IMediaService mediaService)
        {
    
            _contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
            _mediaService = mediaService;;
        }
    

    I have more of a detailed explanation here: https://our.umbraco.com/forum/umbraco-8/96125-custom-application-started-events#comment-304012

  • Luis Pedro 14 posts 135 karma points
    1 week ago
    Luis Pedro
    0

    Awesome.

    Sebastiaan, you're the man.

    Thanks a lot :)

Please Sign in or register to post replies

Write your reply to:

Draft