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!
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.)
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.
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;
}
}
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
but this just gets me a Media object with bunch of properties - I can see the url
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!
This may help you, try to look at /Settings/Media Types/Image and see the Generic properties.
You can use alias "umbracoExtension" and "umbracoBytes".
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.)
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.
As (16 months later) I was looking for this (again) here the the answer I came up with ...
Hi,
I am using Umbraco 7.5.13. The IMedia "umbracoFile" property value returned is a JSON string. It must be parsed.
is working on a reply...