Read SVG File From Media Center (Azure Blob Storage Enabled)
I've updated the post a couple of times. If someone is willing to spend a little extra time helping me reach the optimal solution for rendering the contents of an Umbraco media picker (an SVG File in the Media Center of Umbraco) from Azure Blob Cache, from a razor partial....
I'm offering to donate $50 to a charity of your choice. (Sorry I can't extend this to more than 1 person, community will have to decide who helped more.)
Thanks!
I'm trying to print the contents of an SVG file. This file is obviously uploaded to the Media Center in Umbraco. The only quirk is we are using ImageProcessor and the Azure Blob Cache.
If it were a local file (and I welcome all criticism) I'd try to do something like this:
public static class ImageExtensions
{
public static string PrintSVG(this Image image)
{
var filePath = HttpContext.Current.Server.MapPath(image.Url());
var contents = System.IO.File.ReadAllText(filePath);
return contents;
}
}
Would I be able to somehow reference the already existing UmbracoFileSystemProviders.Azure? And using this, translate the Umbraco path /media/abc/def.svg to the correct path needed so OpenFile(string path) works?
I know very little about C# programming, dependency injection, good backend architecture. Hopefully someone can just help steer me in the right direction. Thanks!
So I stumbled upon GetFullPath(string path) which returned to me the full URI for the file hosted in Azure.
So I pass in the Umbraco image URI and get back what I need to then call OpenFile() and get the stream.... to then read the stream ReadToEnd() and return the SVG contents...
Everything is now working, but could someone who knows their way around this stuff tell me the best way to access AzureBlobFileSystem from a static class?
I'm writing an extension to Image like this:
public static class ImageExtensions
{
public static string PrintSVG(this Image image)
{
/*
How to access AzureBlobFileSystem
without initializing it every time?
*/
return "";
}
}
Or if there is a better recommendation I'm all ears. But I want to easily, from Razor, inject the contents of an SVG File.
I had a couple of sites fail for this exact reason (I was rendering SVGs by loading them from disk) when the Blob Storage update hit.
With help from Kenn Jacobsen and Dave Woestenborghs, I've been able to use this chunk of code to fix it:
public static string LoadFile(string sourceImagePath) {
try {
using (var stream = FileSystemProviderManager.Current.MediaFileSystem.OpenFile(sourceImagePath)) {
using (var ms = new MemoryStream()) {
stream.CopyTo(ms);
var bytes = ms.ToArray();
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
}
} catch (Exception) {
return string.Empty;
}
}
I hope you can figure out the missing using statements (it's part of a Helper file I use) - otherwise let me know and I'll see if I can provide the missing pieces.
Slightly embarassing but I'm finally coming back to solve this. Chriztian this helped me a lot. I was currently repeating code to access Azure Blob Cache on my own and was able to simply use Current.MediaFileSystem. IDK if it's an Umbraco 8 thing, but could not reference the FileSystemProviderManager. That's the only thing I had to remove actually.
I'd be happy to donate a few dollars to a charity of your choice mate.
Just started digging into this very same issue and lo and behold, you wonderful people have saved me potentially hours of digging so thank you very much.
Read SVG File From Media Center (Azure Blob Storage Enabled)
I'm trying to print the contents of an SVG file. This file is obviously uploaded to the Media Center in Umbraco. The only quirk is we are using ImageProcessor and the Azure Blob Cache.
If it were a local file (and I welcome all criticism) I'd try to do something like this:
Then I could use it my razor views:
And I'd get something like this:
Can someone point me in the right direction here?
Would I be able to somehow reference the already existing
UmbracoFileSystemProviders.Azure
? And using this, translate the Umbraco path /media/abc/def.svg to the correct path needed soOpenFile(string path)
works?I know very little about C# programming, dependency injection, good backend architecture. Hopefully someone can just help steer me in the right direction. Thanks!
So I stumbled upon
GetFullPath(string path)
which returned to me the full URI for the file hosted in Azure.So I pass in the Umbraco image URI and get back what I need to then call
OpenFile()
and get the stream.... to then read the streamReadToEnd()
and return the SVG contents...Everything is now working, but could someone who knows their way around this stuff tell me the best way to access
AzureBlobFileSystem
from a static class?I'm writing an extension to
Image
like this:Or if there is a better recommendation I'm all ears. But I want to easily, from Razor, inject the contents of an SVG File.
Hi Mark,
I had a couple of sites fail for this exact reason (I was rendering SVGs by loading them from disk) when the Blob Storage update hit.
With help from Kenn Jacobsen and Dave Woestenborghs, I've been able to use this chunk of code to fix it:
I hope you can figure out the missing
using
statements (it's part of a Helper file I use) - otherwise let me know and I'll see if I can provide the missing pieces.Hope this helps,
/Chriztian
Slightly embarassing but I'm finally coming back to solve this. Chriztian this helped me a lot. I was currently repeating code to access Azure Blob Cache on my own and was able to simply use Current.MediaFileSystem. IDK if it's an Umbraco 8 thing, but could not reference the FileSystemProviderManager. That's the only thing I had to remove actually.
I'd be happy to donate a few dollars to a charity of your choice mate.
Thank you, and happy and safe holidays mate!
Thanks so much for this, it was a life saver for getting SVGs rendered inline on a new Umbraco project :3
I think I owe everyone in this thread a drink!
Just started digging into this very same issue and lo and behold, you wonderful people have saved me potentially hours of digging so thank you very much.
h5yr
is working on a reply...