Copied to clipboard

Flag this post as spam?

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


  • Andrew Hawken 59 posts 116 karma points c-trib
    Sep 14, 2015 @ 18:56
    Andrew Hawken
    1

    How to get bytes of media and mimtype in Umbraco 7 backoffice

    I'm hooking into ContentServicePublished and I need to get at the actual media of a node - expressly to get the MimeType and read the bytes from disk/http to create a CouchDb Attachment).

    I can get 'close' with

    var media  = ApplicationContext.Current.Services.MediaService.GetById(mediaid);
    //or
    
    var media2 =  helper.Media(mediaid);
    //where helper is an  Umbraco.Web.UmbracoHelper 
    

    but this just gets me a Media object with bunch of properties - I can see the url

    ms.Properties["umbracoFile"]
    

    but that looks like a URL rather than I file. I guess I can cludge something together to read it from the filesystem by pretending its a path relative to AppDomain.CurrentDomain.BaseDirectory but that feels like a terrible mistake, especially if its configured to store it on Azure or Amazon!

    I could read the URL with webclient, but I don't know how to get the current hostname?

    I feel I'm missing something obvious: time to ask!

  • jivan thapa 194 posts 681 karma points
    Sep 14, 2015 @ 19:39
    jivan thapa
    0

    This may help you, try to look at /Settings/Media Types/Image and see the Generic properties.

    You can use alias "umbracoExtension" and "umbracoBytes".

    enter image description here

  • Andrew Hawken 59 posts 116 karma points c-trib
    Sep 14, 2015 @ 20:15
    Andrew Hawken
    0

    That's not quite helping. the umbracoBytes contains the length and umbracoFile gets me the filename (path in fact) and umbracoExtension gets the file extension (so I can use that to product a Mime Type) but I want to read the file itself. What is the best way to read the content of the file into memory, given all I have is a relative path?

    i.e. is there a public class/method that is used by the umbraco backend that is aware of things like UmbracoFileSystemProviders in order to get the file itself (whether it is stored locally or via https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure for instance.)

  • Andrew Hawken 59 posts 116 karma points c-trib
    Sep 14, 2015 @ 22:14
    Andrew Hawken
    0

    Having spent the evening reading some Umbraco source code, I think what I need is IFileSystem. There are probably enough clues in the Umbraco.Web.Editors.ImagesController to see what I need to do.

  • Andrew Hawken 59 posts 116 karma points c-trib
    Feb 05, 2017 @ 17:38
    Andrew Hawken
    2

    As (16 months later) I was looking for this (again) here the the answer I came up with ...

       public class SheetIOMediaReader
    {
        public byte[] Read(IMedia med)
        {
            if (med != null)
            {
                var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
                var imagePath = med.Properties["umbracoFile"].Value.ToString();
                var fullOrgPath = mediaFileSystem.GetFullPath(mediaFileSystem.GetRelativePath(imagePath));
                if (!mediaFileSystem.FileExists(fullOrgPath))
                {
                    return null;
                }
                byte[] res = null;
    
                using (var fileStream = mediaFileSystem.OpenFile(fullOrgPath))
                {
                    if (fileStream.CanSeek) fileStream.Seek(0, 0);
                    byte[] buffer = new byte[16*1024];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int read;
                        while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        res = ms.ToArray();
                    }
    
                }
                return res;
    
            }
            return null;
        }
    }
    
  • James Lau 37 posts 58 karma points
    Mar 27, 2018 @ 06:58
    James Lau
    0

    Hi,

    I am using Umbraco 7.5.13. The IMedia "umbracoFile" property value returned is a JSON string. It must be parsed.

                dynamic jsonValue = JObject.Parse(imagePath);
                string propertyValue = jsonValue.src.Value;
    
                var relativePath = mediaFileSystem.GetRelativePath(propertyValue);
                var fullOrgPath = mediaFileSystem.GetFullPath(relativePath);
    
  • 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