Copied to clipboard

Flag this post as spam?

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


  • James Jackson-South 489 posts 1747 karma points c-trib
    Nov 18, 2016 @ 05:22
    James Jackson-South
    0

    Unable to save media to blob storage via media service.

    Umbraco 7.5.3

    I'm attempting to save media via the content service to blob storage and getting a very odd result.

    The media service method runs accordingly and performs a save, however the resultant blob has a content length of zero! The odd thing is that the thumbnail blob is present and correct.

    The crux of my code is simple enough, I'm matching a string value from within an old umbraco.config file from a site i'm importing from, creating a web request for that image and saving the resultant response to the media service. Nothing crazy as far as I can see.

    Match inlineImageMatch = urlRegex.Match(inlineImage);
    if (inlineImageMatch.Success)
    {
        inlineImage = string.Join(string.Empty, rootUrl.TrimEnd("/"), inlineImageMatch.Value);
    
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(inlineImage);
        WebResponse response = null;
        try
        {
            using (response = request.GetResponse())
            {
                IMedia image = mediaService.CreateMedia(Path.GetFileNameWithoutExtension(inlineImage), mediaRootId, Constants.Conventions.MediaTypes.Image);
    
                using (Stream stream = response.GetResponseStream())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        stream.CopyTo(ms);
                        ms.Flush();
                        image.SetValue(Constants.Conventions.Media.File, Path.GetFileName(inlineImage), ms);
                        mediaService.Save(image, 0, false);
                        inlineImageId = image.Id;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            response?.Dispose();
        }
    }
    

    I'm using my own FileSystemProvider within the website to push to Azure. As far as I am aware the same calls would be made using both the media service and the backoffice - Which works, so I'm ruling that out.

    Anybody got any ideas? Looks like a bug in Umbraco to me.

    Here's a screenshot of a blob upload in the explorer.

    Azure blob storage - Note content length!

  • James Jackson-South 489 posts 1747 karma points c-trib
    Nov 18, 2016 @ 05:47
    James Jackson-South
    4

    Arrrggghhhh I'm an idiot.

    So it turns out Umbraco doesn't reset the input stream position to zero before saving. This is despite complaining that the stream is not seekable (if you don't pass a seekable stream) which surprised me.

    So the solution was simple. Set the position first!

    Lalalalalalalalalalahhhh..... Carry on people. Nothing to see here.

Please Sign in or register to post replies

Write your reply to:

Draft