Copied to clipboard

Flag this post as spam?

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


  • Andreas Emtinger 23 posts 185 karma points
    Oct 13, 2021 @ 22:08
    Andreas Emtinger
    0

    Change storage location of the media folder

    Hi, I have a problem reading from media folder (Or Umbraco has).

    I have followed these instructions to move my media folder: https://our.umbraco.com/documentation/Extending/FileSystemProviders/

    My composer:

    public class SetMediaFileSystemComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.SetMediaFileSystem((factory) =>
            {
                var hostingEnvironment = factory.GetRequiredService<IHostingEnvironment>();
                var settings = factory.GetRequiredService<ISiteSettings>();
                var globalSettings = factory.GetRequiredService<IOptions<GlobalSettings>>().Value;
                var mediaFolder = settings.MediaPath.Fallback(globalSettings.UmbracoMediaPath);
    
                var rootPath = mediaFolder.StartsWith("~")
                    ? hostingEnvironment.MapPathWebRoot(mediaFolder)
                    : mediaFolder;
    
                var rootUrl = hostingEnvironment.ToAbsolute("/Media");
    
                return new PhysicalFileSystem(
                    factory.GetRequiredService<IIOHelper>(),
                    hostingEnvironment,
                    factory.GetRequiredService<ILogger<PhysicalFileSystem>>(),
                    rootPath,
                    rootUrl);
            });
        }
    }
    

    My ISiteSettings is from my appsettings.json:

    {
      "SiteSettings": {
        "MediaPath": "D:\\Code\\_RuntimeFiles\\Media"
      }
    }  
    

    When I upload a image in BackOffice, I can see that the image is created in D:\Code_RuntimeFiles\Media

    But Umbraco can't display the image in BackOffice or in my views.

    A bug?

    When I added a custom ImageController to override the "get" part, I could display the image (BackOffice and in my views). But then ImageProccessor didn't started...

    public class MediaController : Controller
    {
        private readonly MediaFileManager _mediaFileManager;
        private readonly IImageUrlGenerator _imageUrlGenerator;
    
        public MediaController(
            MediaFileManager fileManager, 
            IImageUrlGenerator imageUrlGenerator)
        {
            _mediaFileManager = fileManager;
            _imageUrlGenerator = imageUrlGenerator;
        }
    
        public IActionResult Index(string id, string file)
        {
            var imagePath = Path.Combine(id, file);
    
            var ext = Path.GetExtension(imagePath);
    
            // we need to check if it is an image by extension
            if (_imageUrlGenerator.IsSupportedImageFormat(ext) == false)
                return NotFound();
    
            var image = _mediaFileManager.FileSystem.OpenFile(imagePath);
            return File(image, MediaTypeNames.Image.Jpeg);
        }
    }
    
  • Andreas Emtinger 23 posts 185 karma points
    Oct 13, 2021 @ 22:50
    Andreas Emtinger
    0

    Update: I removed my MediaController.

    I added this to Startup.cs/Configure:

    if (siteSettings.MediaPath.IsInvalid() || siteSettings.MediaPath.StartsWith("~"))
        return;
    
    var mediaPath = hostingEnvironment.ToAbsolute(globalSettings.Value.UmbracoMediaPath);
    
    app.UseStaticFiles(new StaticFileOptions
    {
       FileProvider = new PhysicalFileProvider(siteSettings.MediaPath),
       RequestPath = mediaPath,
    });
    

    Now I can see the images. BUT I get original images, not cropped (when I am using crop).

    Why? :(

  • Andreas Emtinger 23 posts 185 karma points
    Oct 13, 2021 @ 22:58
    Andreas Emtinger
    0

    Thumbnail image in BackOffice is not 500px as requested: Not a thumbnail...

  • Arjan H. 221 posts 457 karma points c-trib
    Nov 16, 2021 @ 23:32
    Arjan H.
    0

    Hi Andreas. Did you manage to resolve this? I'm having the same issue. I was able to implement a custom FileSystemProvider, but using a directory outside of wwwroot breaks ImageSharp. I think we need to look at how the Umbraco.StorageProviders.AzureBlob package works and implement a similar FileSystemProvider:

    https://our.umbraco.com/documentation/Extending/FileSystemProviders/Azure-Blob-Storage/

    https://github.com/umbraco/Umbraco.StorageProviders

    https://our.umbraco.com/forum/umbraco-9/106879-umbraco-9-and-media-folder-location

  • andrew shearer 506 posts 652 karma points
    Nov 17, 2021 @ 01:28
Please Sign in or register to post replies

Write your reply to:

Draft