Copied to clipboard

Flag this post as spam?

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


  • Francis 23 posts 123 karma points
    May 16, 2024 @ 11:16
    Francis
    0

    V12.3.6 How to Obtain Media nodes from IPublishedMediaCache Using Context URL

    Hello,

    I wonder if you can help me out.

    I'm trying to implement a middleware that would intercept url requests from media files and find the media node for it for some processing.

    Example:

    If I enter https://localhost/mediafolder/4ghyxy/samplefile.pdf from the address bar, the middleware should intercept it and find the media node it's associated to and obtain it's properties for further processing

    This can easily be done in IMediaService by doing:

    var media = mediaService.GetMediaByPath(context.Request.Path);
    

    However, since this middleware would have multiple requests (I imagine) I wonder if I should be using the Media cache (IPublishedMediaCache) instead for this.

    My problem with that is I can't find any method of obtaining the node that is as efficient as the one above.

    enter image description here

    I would have to traverse through the media tree to obtain to find the file. Like so:

    enter image description here

    Question 1: Is there a more efficient way of doing in the IPublishedMediaCache?

    Question 2: Or is it ok to simply use the IMediaService in this middleware?

    Thanks in advance

  • Yashwin2492 8 posts 28 karma points
    Aug 01, 2024 @ 09:11
    Yashwin2492
    0

    You can use "GetSingleByXPath" method and use the filename to get the media. For example, GetSingleByXPath($"//umbracoFile[contains(., '{yourFileName}')]")

    But this method obsolete in v14. Not sure about the alternate solution from v14.

Please Sign in or register to post replies

Write your reply to:

Draft