Copied to clipboard

Flag this post as spam?

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


  • Robin Hansen 135 posts 368 karma points
    Sep 01, 2015 @ 14:04
    Robin Hansen
    0

    Update 'umbracoFile' property on existing Media

    I'm struggling to edit a custom mediaType's property ("umbracoFile"). The property is an image cropper. I've tried this snippet with no luck: mediaItem.SetValue("umbracoFile", "/media/1059/uploader-test.jpg");

    Plz take notice that I'm NOT creating a new Image - but trying to update an allready existing one... - who kan tell med how to acess this property...? :-|

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Sep 02, 2015 @ 05:24
    Sebastiaan Janssen
    1

    You need the MediaService to save the item:

    var mediaService = Services.MediaService;
    mediaItem.SetValue("umbracoFile", "/media/1059/uploader-test.jpg");
    mediaService.Save(mediaItem);
    
  • Robin Hansen 135 posts 368 karma points
    Sep 02, 2015 @ 10:34
    Robin Hansen
    0

    Yup - that did the trick... - how could I miss that... ;-)

  • Robin Hansen 135 posts 368 karma points
    Nov 02, 2015 @ 14:20
    Robin Hansen
    0

    Hm - since upgrade to ver. 7.3.0/7.3.1 it seems like the mediaService is not acting correctly... :-|

    enter image description here

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Nov 02, 2015 @ 14:23
    Sebastiaan Janssen
    0

    @Robin If you can show more of your code (preferably in text, not in a screenshot) we can help you better.

  • Robin Hansen 135 posts 368 karma points
    Nov 03, 2015 @ 11:34
    Robin Hansen
    0

    Absolutly - this is a little script I'm experimenting with, a custom mediaType to take screenshots with - worked like a charm in ver. 7.2.8:

    using Newtonsoft.Json.Linq;
    using System;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Threading;
    using umbraco.cms.businesslogic.web;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace ScreenDumbDataType.App_Start
    {
    public class ScreenShot : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            MediaService.Saved += MediaServiceSaved;
        }
    
        void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
        {
            foreach (var mediaItem in e.SavedEntities)
            {
                if (mediaItem.ContentType.Alias == "Screencapture")
                {
                    int imgId = mediaItem.Id;
                    string Name = mediaItem.Name;
                    string imgName = Name.ToLower()
                        .Replace(" ", "-")
                        .Replace(",", "")
                        .Replace(".", "-")
                        .Replace(":", "")
                        .Replace("æ", "ae")
                        .Replace("ø", "oe")
                        .Replace("å", "aa")
                        .Replace("ä", "ae")
                        .Replace("ö", "oe")
                        .Replace("ü", "ue")
                        .Replace("ß", "ss")
                        .Replace("Ä", "ae")
                        .Replace("Ö", "oe").Trim();
                    string imgUrl = mediaItem.GetValue<string>("targetUrl").Trim(); //This Property is crucial for this MediaType
    
                    Thread thread = new Thread(delegate()
                        {
                            using (System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser())
                            {
                                browser.ScrollBarsEnabled = false;
                                browser.AllowNavigation = true;
                                browser.Navigate(imgUrl);
                                //browser.Width = 1920;   //Modify theese as you whant
                                //browser.Height = 1080;   //Modify theese as you whant
                                browser.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
                                browser.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
                                browser.Name = imgName;
                                browser.Tag = Convert.ToInt32(imgId); //Placeholder to pass on the NodeId
                                browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
                                while (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
                                {
                                    System.Windows.Forms.Application.DoEvents();
                                }
                            }
                        });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                }
            }
        }
    
    
        private void DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            System.Windows.Forms.WebBrowser browser = sender as System.Windows.Forms.WebBrowser;
            using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
            {
                browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
                using (MemoryStream stream = new MemoryStream())
                {
                    int mediaId = Convert.ToInt32(browser.Tag);
                    var mediaService = ApplicationContext.Current.Services.MediaService;
                    var mediaItem = mediaService.GetById(mediaId);
    
                    string propertySrc = mediaItem.GetValue<string>("umbracoFile");
    
                    dynamic jsonValue = JObject.Parse(propertySrc);
                    string propertyValue = jsonValue.src.Value;
    
                    string root = System.Web.Hosting.HostingEnvironment.MapPath("/media");
                    string createImg = browser.Name + ".jpg";
    
                    if (propertyValue  == String.Empty)
                    {
                        string newFolder = "";
                        foreach (string folder in Directory.GetDirectories(root).Where(file => Regex.IsMatch(Path.GetFileName(file), "^[0-9]+")).OrderByDescending(x => x).Take(1))
                        {
                            string folderId = Path.GetFileName(folder);
                            int number = int.Parse(folderId);
                            newFolder = (number + 1).ToString();
                        }
                        string createFolder = ((newFolder == String.Empty) ? "1001" : newFolder);
                        string createPath = root + "\\" + createFolder;
    
                        Directory.CreateDirectory(createPath);
                        bitmap.Save(createPath + "\\" + createImg, System.Drawing.Imaging.ImageFormat.Jpeg);
                        mediaItem.SetValue("umbracoFile", "/media/" + createFolder + "/" + createImg);
                        mediaService.Save(mediaItem);
                    }
                }
            }
        }
    }
    }
    
  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Nov 03, 2015 @ 12:44
    Sebastiaan Janssen
    0

    Hmmm.. from the code I can only conclude that mediaItem is actually null, have you checked that?

  • 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