Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Aug 26, 2019 @ 03:09
    jake williamson
    0

    custom file system issue - how to 'GetUnderlyingFileSystemProvider' in v8?

    hey out there,

    i'm struggling a little with converting a v7 controller that force downloads a media item to v8.

    the v7 code is as follows:

    var fileSystem = FileSystemProviderManager.Current.GetUnderlyingFileSystemProvider("media");
    
    var filePath = fileSystem.GetFullPath(fileUrl);
    
    if (!fileSystem.FileExists(filePath))
    {
        LogHelper.Warn<FileDownloadController>("The media item was not found.");
    
        throw new System.IO.FileNotFoundException("The media item was not found.", filePath);
    }
    
    return File(
        fileSystem.OpenFile(filePath),
        MimeMapping.GetMimeMapping(fileSystem.GetFileName(filePath)),
        fileSystem.GetFileName(fileUrl)
    );
    

    the line i'm stuck with is:

    var fileSystem = FileSystemProviderManager.Current.GetUnderlyingFileSystemProvider("media");
    

    i've been messing around for ages trying to find the equivalent code in v8 and just can't find it...

    the documentation here hasn't yet been updated for v8:

    https://our.umbraco.com/documentation/Extending/FileSystemProviders/

    which leaves me a little stuck! has anyone else hit this issue yet?

    any suggestions, as ever, are greatly received ;)

  • Marc Goodson 2157 posts 14435 karma points MVP 9x c-trib
    Aug 26, 2019 @ 07:42
    Marc Goodson
    101

    Hi Jake

    I reckon in V8 in the constructor for your controller you could inject in the IMediaFileSystem that is currently registered via DI?

    using System.Collections.Generic;
    using System.Web.Mvc;
    using Umbraco.Web.Models;
    using Umbraco8.Services;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Services;
    
    namespace Umbraco8.Controllers
    {
        public class MyCustomController : Umbraco.Web.Mvc.RenderMvcController
        { 
           private readonly IMediaFileSystem _mediaFileSystem;
            public MyCustomController(IMediaFileSystem mediaFileSystem)
            {
                 _mediaFileSystem = mediaFileSystem;
            }
            public override ActionResult Index(ContentModel model)
            { 
                // do something with MediaFileSystem
                string fullPath = _mediaFileSystem.GetFullPath(path);
            }
    }
    

    Some documentation on composing here: https://our.umbraco.com/Documentation/Implementation/Composing/ and some examples of injection of services here: https://our.umbraco.com/Documentation/Implementation/Services/

    regards

    Marc

  • jake williamson 207 posts 873 karma points
    Aug 26, 2019 @ 07:53
    jake williamson
    0

    spot on marc, that works a treat!

    now why didn't i think of that... think it may have been a case of over thinking the problem ;)

    cheers fella, much appreciated.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies