Copied to clipboard

Flag this post as spam?

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


  • Dan White 206 posts 510 karma points c-trib
    Jun 08, 2013 @ 04:26
    Dan White
    0

    How to use UmbracoEntity & ThumbnailProvidersResolver?

    Hi,

    I'm trying to customize some things in the media Folder Browser datatype. 

    In order to do so I'm having to duplicate FolderBrowserService and modify it for my needs. The problem I'm having is that ThumbnailProvidersResolver is a internal sealed class, so obviously I can't access it. 

    Currently, I'm using the FolderBrowserService source from 6.1, but I can see that it's being rewritten for 6.1.2. With the 6.1.2 source I'm having issues with the UmbracoEntity type. 

    Looking for suggestions, and I prefer not to have to download and modify the core directly. 

    Thanks,

    Dan

    Working code with the ThumbnailProvidersResolver part commented out.

    using Pike.Web.Trees.CustomMediaTree;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Script.Serialization;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.Tags;
    using Umbraco.Core.IO;
    using Umbraco.Core.Media;
    using Umbraco.Core.ObjectResolution;
    using Umbraco.Web.BaseRest;
    
    namespace Pike.Web.WebServices
    {
        //TODO: Can we convert this to MVC please instead of /base?
    
        [RestExtension("CustomFolderBrowserService")]
        public class CustomFolderBrowserService
        {
            [RestExtensionMethod(ReturnXml = false)]
            public static string GetChildren(int parentId)
            {
                var parentMedia = new global::umbraco.cms.businesslogic.media.Media(parentId);
                var currentUser = User.GetCurrent();
                var data = new List<object>();
    
                // Check user is logged in
                if (currentUser == null)
                    throw new UnauthorizedAccessException("You must be logged in to use this service");
    
                // Check user is allowed to access selected media item
                if (!("," + parentMedia.Path + ",").Contains("," + currentUser.StartMediaId + ","))
                    throw new UnauthorizedAccessException("You do not have access to this Media node");
    
                // Get children and filter
                //TODO: Only fetch files, not containers
                //TODO: Cache responses to speed up susequent searches
                List<global::umbraco.cms.businesslogic.media.Media> children;
                if (parentId == -1 && Config.Instance.ConfigEntries.Any(a => a.Users.Contains(currentUser.Id)))
                {
                    var allowedNodes = Config.Instance.ConfigEntries.First(a => a.Users.Contains(currentUser.Id)).Nodes.Keys;
                    children = new List<global::umbraco.cms.businesslogic.media.Media>();
                    foreach (var node in allowedNodes)
                    {
                        children.Add(new global::umbraco.cms.businesslogic.media.Media(node));
                    }
                
                }else{
                    children = parentMedia.Children.ToList();
                }         
    
                foreach (var child in children)
                {
                    var fileProp = child.getProperty("umbracoFile") ??
                        child.GenericProperties.FirstOrDefault(x =>
                            x.PropertyType.DataTypeDefinition.DataType.Id == new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"));
    
                    var fileUrl = fileProp != null ? fileProp.Value.ToString() : "";
                    // ARGG!!! MAYBE LOOK AT WHAT DAMP IS DOING FOR THE DIALOG IMAGES
                    var thumbUrl = ""; // ThumbnailProvidersResolver.Current.GetThumbnailUrl(fileUrl);
                    var item = new
                    {
                        Id = child.Id,
                        Path = child.Path,
                        Name = child.Text,
                        Tags = string.Join(",", Tag.GetTags(child.Id).Select(x => x.TagCaption)),
                        MediaTypeAlias = child.ContentType.Alias,
                        EditUrl = string.Format("editMedia.aspx?id={0}", child.Id),
                        FileUrl = fileUrl,
                        ThumbnailUrl = !string.IsNullOrEmpty(thumbUrl)
                            ? thumbUrl
                            : IOHelper.ResolveUrl(SystemDirectories.Umbraco + "/images/thumbnails/" + child.ContentType.Thumbnail)
                    };
    
                    data.Add(item);
                }
    
                return new JavaScriptSerializer().Serialize(data);
            }
    
            [RestExtensionMethod(ReturnXml = false)]
            public static string Delete(string nodeIds)
            {
                var nodeIdParts = nodeIds.Split(',');
    
                foreach (var nodeIdPart in nodeIdParts.Where(x => !string.IsNullOrEmpty(x)))
                {
                    var nodeId = 0;
                    if (!Int32.TryParse(nodeIdPart, out nodeId))
                        continue;
    
                    var node = new global::umbraco.cms.businesslogic.media.Media(nodeId);
                    node.delete(("," + node.Path + ",").Contains(",-21,"));
                }
    
                return new JavaScriptSerializer().Serialize(new
                {
                    success = true
                });
            }
        }
    

  • Dan White 206 posts 510 karma points c-trib
    Jun 12, 2013 @ 18:54
    Dan White
    100

    Got it working, but not pretty. Switched out EntityService for MediaService and ThumbnailProvidersResolver for TryGetThumbnailUrl (copied over the code).

Please Sign in or register to post replies

Write your reply to:

Draft