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
    1 week ago
    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
    1 week ago
    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
    1 week ago
    Andreas Emtinger
    0

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

Please Sign in or register to post replies

Write your reply to:

Draft