Mediaservices and efficiency
# help-with-umbraco
m
I have a question bout Mediaservices and potential issues with performance. The challenge is this. I want to create certain folders as "protected" in order to have certain items only on demand for authenticated members. So when I use the middleware to check for folder access I have to use ms to get the friendly name of the folder. Is there a more efficient way of doing this?
This is my "Kiss" code. Not very advanced ๐Ÿ™‚
Copy code
public async Task InvokeAsync(HttpContext context, IMemberManager memberManager)
        {

            if (!context.Request.Path.StartsWithSegments("/media"))
            {
                await _next(context);
            }
            else
            {
                var media = _mediaService.GetMediaByPath(context.Request.Path);
                var folder = _mediaService.GetById(media.ParentId);

                if (folder.Name == "Protected")
                {
                    context.Response.StatusCode = 401;
                }
                if (context.User.Identity.IsAuthenticated)
                {

                    await _next(context);
                }
                else
                {
                    context.Response.StatusCode = 401;
                }
            }
        }
the challenge would be to find a better way to get that foldername without ms?
j
Easy way: use a package https://soetemansoftware.nl/media-protect Another way just off the top of my head, have a checkbox on the media items called โ€˜Protectedโ€™ - if you want it set automatically based on a folder you could set the value of the protected checkbox at the point of creation via notifications. Probably need a notification event if a file gets moved also? This would then allow you to use the cache rather than hitting the database and then essentially the same middleware - found this which is basically doing what I envisaged https://our.umbraco.com/forum/using-umbraco-and-getting-started/110195-custom-front-end-media-controller
m
I thought of that but it didn't quite work in this case. The checkbox idea was cool but can you access that without ms?
c
I just had a similar requirement. Client selling pdf's they upload to Media section but obv don't want anyone that has the link to access them. We used media-protect to do that. We just created a Media folder called "Secure" and put anything we needed to protect under that. Works fine. Only downside from my POV is it only works on Windows and not Linux which is what I prefer to develop on. But yes, the package works. If there's another way, I'd love to hear it ๐Ÿ™‚
m
Just a Q to both of you. Say I use the Checkbox approach. How would I accesss that from MW without using Mediaservices? I mean wouldn't it be about the same issue? Since this code is hit everytime any media is accessed.
c
I think the answers in here:-
Copy code
var typedMediaPickerSingle = Model.SinglePhoto;
        if (typedMediaPickerSingle is MediaWithCrops singlePhoto)
        {
            bool isSecure = singlePhoto.Content.Value<bool>("mycheckbox");  <<======

            int imageWidth = otherPhoto.Content.Value<int>("umbracoWidth");
            int imageHeight = otherPhoto.Content.Value<int>("umbracoHeight");

            <div class="single-photo">
                <slimsy-picture media-item="@singlePhoto" width="@(imageWidth)" height="@(imageHeight)" render-lqip="true" render-webp-alternative="true" alt-text="@singlePhoto.Name"></slimsy-picture>
            </div>
        }
j
Did you figure it out? It is accessible via the cache but Iโ€™m not sure if the cache is available or not at this stage of the pipeline so will need to do some testing. Iโ€™ve got a requirement to do something similar soon
m
I kept the mediaservice but simplified it a bit.
I will tell when we put this into production if it i crashed or not ๐Ÿ˜„
It is not a huge public site but time will tell if this works
j
Doesn't work on linux? Do you have details on why ?
c
Because it hooks into IIS which doesn't exist on Linux. I know, I've hinted to @Richard Soeteman about it. Maybe one day ๐Ÿ™‚
j
Thanks for replying, yes indeed - we are planning infra for some Umbraco 13/14 builds and some require this package - worth to know as it's currently down as linux app services
r
@Craig100 @Jamie T Should work on Linux now as well since all is Middleware these days
c
Great, I'll try that on a bigish project when I get time. It's got Vendr in it too so I'll have to get creative with SQLite (or use Docker if I have to), but at least it means I can take it off the Windoze VM. ๐Ÿ™‚
j
@User tip
2 Views