Copied to clipboard

Flag this post as spam?

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


  • Jason D 71 posts 243 karma points
    Jun 06, 2024 @ 23:05
    Jason D
    0

    Get Media File by Name in Umbraco 13

    Hi there, I've been searching documentation and the web, while trying quite a few ways to simply lookup a media file by a name ("file.pdf"), by using the Umbraco API for the Media Service and/or Media File Manager, and I can't seem to accomplish it by using file name (versus path, GUID, etc).

    I am refactoring older code that used SQL to directly check for the file name in the database, but I want to have it updated.

    Is there no other way to check for existing media file?

    I actually had a pretty elaborate set of methods that would look locally, and then if no, would switch to look in the blob service. But this seemed like stepping outside the context of Umbraco's capabilities.

  • Simon Napper 109 posts 343 karma points
    Jun 07, 2024 @ 08:52
    Simon Napper
    0

    If you've got access to the published cache, you could try something like this (I'm writing this off the top of my head so might not work!)

    umbracoContext.Media.GetAtRoot().Where(x=>x.Value<string>("umbracoFile").InvariantEquals("file.pdf"));
    

    Something like that?

  • Yakov Lebski 581 posts 2249 karma points
    Jun 07, 2024 @ 15:18
    Yakov Lebski
    0
    private readonly IMediaService mediaService;       
    mediaService.GetMediaByPath(mediaPath)
    

    will generate direct SQL request to DB

  • Simon Napper 109 posts 343 karma points
    Jun 07, 2024 @ 15:32
    Simon Napper
    0

    It will if you have the full path (i.e. /media/1024/file.pdf) but not sure it will work with just the file name?

    In an ideal world it's best to avoid hitting the DB via the MediaService if you can access the cache for looking things up, but depends if you want to then update it once you've got it!

  • Yakov Lebski 581 posts 2249 karma points
    Jun 07, 2024 @ 15:53
    Yakov Lebski
    0

    yep, it will for relative path i.e. /media/1024/file.pdf,

    using publishing api need to also think about folder, for big website can be slow

    for big site examine query will be better solution

  • Jason D 71 posts 243 karma points
    Jun 07, 2024 @ 20:03
    Jason D
    0

    Thank you guys! You gave me some things to try.

    For context, I don't know the media folder ("media/0csnrul/"), or if my startup.cs is using local or azure media.

    Examine might be the best, however my goal is to get existing media file if exists and replace File, but keep path intact.

  • Jason D 71 posts 243 karma points
    Jun 17, 2024 @ 15:16
    Jason D
    100

    To add a solution to the "get media" question of this post. What is working pretty well is using Examine to search for the file by name, returning the top matching result, and then setting the new file from the stream to the existing media.

    I am trying to complete this project with being a batch process, but I'm having difficulties with it for some reason not always grabbing the new file from the stream, to upload in the existing media item to the media folder, but I am hopeful I'll get that part figured out.

Please Sign in or register to post replies

Write your reply to:

Draft