Skip to content

GetFullPath doesn't work as expected #11539

Closed
@bjarnef

Description

@bjarnef

Which exact Umbraco version are you using? For example: 8.13.1 - don't just write v8

9.0.1

Bug summary

I have a custom Tag Helper where I inject MediaFileManager. From this I can also access IFileSystem via the FileSystem property.

Here is an example, where File is a relative path returned from mediaItem.Url():

namespace MyProject.Core.TagHelpers
{
    using System;
    using System.IO;

    using Microsoft.AspNetCore.Html;
    using Microsoft.AspNetCore.Razor.TagHelpers;
    using Microsoft.Extensions.Logging;
    using Umbraco.Cms.Core.Hosting;
    using Umbraco.Cms.Core.IO;

    [HtmlTargetElement("svg-icon", TagStructure = TagStructure.NormalOrSelfClosing)]
    public class SvgIconTagHelper : TagHelper
    {
        private readonly IIOHelper _ioHelper;
        private readonly IHostingEnvironment _hostingEnvironment;
        private readonly MediaFileManager _mediaFileManager;
        private readonly IFileSystem _fileSystem;
        private readonly ILogger<SvgIconTagHelper> _logger;

        public SvgIconTagHelper(
            IIOHelper ioHelper,
            IHostingEnvironment hostingEnvironment,
            MediaFileManager mediaFileManager,
            IFileSystem _fileSystem,
            ILogger<SvgIconTagHelper> logger)
        {
            //_webHostEnvironment = webHostEnvironment;
            _ioHelper = ioHelper;
            _hostingEnvironment = hostingEnvironment;
            _mediaFileManager = mediaFileManager;
            _logger = logger;
        }

        /// <summary>
        /// Path to SVG file
        /// </summary>
	public string File { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (string.IsNullOrWhiteSpace(File))
            {
                throw new ArgumentNullException(nameof(File));
            }

            output.TagName = "span";

            var classAttribute = output.Attributes["class"];
            var classes = (classAttribute?.Value as HtmlString)?.Value ?? string.Empty;
            classes += " icon";
            classes = classes.Trim();
            output.Attributes.SetAttribute("class", classes);

            output.TagMode = TagMode.StartTagAndEndTag;

            try
            {
                //string filePath = _ioHelper.GetRelativePath(File);
                //var path = _mediaFileManager.FileSystem.GetFullPath(filePath);

                var fullPath = _mediaFileManager.FileSystem.GetFullPath(File);

                _logger.LogInformation("Full path {Path}", fullPath);

                if (_mediaFileManager.FileSystem.FileExists(fullPath))
                {
                    var stream = _mediaFileManager.FileSystem.OpenFile(fullPath);
                    stream.Seek(0, SeekOrigin.Begin);

                    using (StreamReader sr = new StreamReader(stream))
                    {
                        var svg = sr.ReadToEnd();
                        if (!string.IsNullOrEmpty(svg))
                        {
                            output.Content.SetHtmlContent(svg);
                        }
                    }
                }
                    
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed reading SVG icon");
            }
        }
    }

}

Locally this works an GetFullPath() returns C:\VSProjects\my-project\UmbracoCloud\src\UmbracoProject\wwwroot\media\rybamegd\document.svg and render the SVG Icon.

However on Umbraco Cloud is log this path from GetFullPath():
Full path "media/~/media/rybamegd/document.svg"

I would have expected it to return an absolute path to the media file. Not sure if it's a core issue or maybe the implementation of GetFullPath() here?
https://github.com/umbraco/Umbraco.StorageProviders/blob/main/src/Umbraco.StorageProviders.AzureBlob/IO/AzureBlobFileSystem.cs#L209-L216

The tag helper is used as the following:

<svg-icon file="@icon.Url()"></svg-icon>

Specifics

No response

Steps to reproduce

Inject MediaFileManager to a custom Tag Helper or just in view file for testing.

Use the following to get the full path to the file.

var path = mediaItem.Url();
var fullPath = _mediaFileManager.FileSystem.GetFullPath(path);

Expected result / actual result

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions