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.
I would have to traverse through the media tree to obtain to find the file. Like so:
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?
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.
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:
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.
I would have to traverse through the media tree to obtain to find the file. Like so:
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
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.
is working on a reply...