Copied to clipboard

Flag this post as spam?

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


  • Patrick Shaun McNicholas 24 posts 206 karma points
    Dec 14, 2021 @ 17:23
    Patrick Shaun McNicholas
    0

    Finding Media Items by the file name

    I am migrating from version 7 to 8 and need a method to search the entire media library for a media item that has a pattern identifiable name. For example...

    Educational Website with courses identified as Course [edu-111] would have a matching pdf file in the media directory that is imported into the media directory in Umbraco and saved with the file name edu-111.pdf

    I am certain there's an easy method to accomplish this using the either a direct lookup similar to finding a published document node but I can't find the reference for this or any articles describing the process. If I needed to find the document as a published node then I would do the following

    var homeNode = Model.Root();
    var courseInformationNode = homeNode.DescendantsOfType("courseInformationPage").Where(x => x.Name == "edu-111");
    

    Is there a similar query method for published Media items that would be similar to this type of published content search?

  • Neil Chapman 42 posts 170 karma points
    Dec 15, 2021 @ 09:51
    Neil Chapman
    1

    You can get the Media Root and do it similar to what you've done, however, if you've got thousands of media files it's gonna be slow as balls.

    I've done a similar thing before searching for a media file by it's filename using the External Index. Media files are stored in the External Index and searching will be rapido.

    if (_examine.TryGetIndex(UmbracoIndexes.ExternalIndexName, out var index))
    {
        var searcher = index.GetSearcher();
    
        var results = searcher.CreateQuery(IndexTypes.Media).Field("umbracoFile", "imageUrl").Execute();
        if (results.Count() > 0)
        {
            var image = Umbraco.Media(results.FirstOrDefault().Id);
        }
    }
    
  • Patrick Shaun McNicholas 24 posts 206 karma points
    Mar 09, 2022 @ 04:46
    Patrick Shaun McNicholas
    100

    I just wanted to give an update on this issue so maybe someone else will have a similar need and can use it. I created a media picker property on the course type documents, and then used that media picker to store the pdf files that were associated with the document - so document courseName-courseNumber ending with .pdf would still be resolved, however in order to dynamically link it within the thousands of documents in the library during migration this is expensive.

    Instead of permanently performing the search every time a course document is loaded, what I did was leave the mediaPicker empty until the site was migrated and each course page opened in a browser, and now if the mediaPicker is empty when a course page is requested, the system will search for mediaItem.Url.Contains("/courseSubject-courseNumber.pdf") and if found it will return the document to link to the person and will also set the mediaPicker item on the document so that in the future it no longer has to search because the file ID is now stored in the document and it's just a direct IPublishedContent picker object with all it's associated glory...

    Simple is as Simple does...

    // Check to see if we already have a SylabusPDF syllabusPDF object assigned to this course.
    string sylabiUrl = String.Empty;
    bool syllabusPDFNodeSet = false;
    var thisSyllabusPDF = Model.Value<IPublishedContent>("syllabusPDF");
    if(thisSyllabusPDF != null){
        sylabiUrl = thisSyllabusPDF.Url;
        syllabusPDFNodeSet = true;
        if(Request.Url.Host == "betace.fresno.edu" || Request.Url.Host == "devce.fresno.edu"){
            <p class="color-alert">Name: @thisSyllabusPDF.Name | ID: @thisSyllabusPDF.Id | Key: @thisSyllabusPDF.Key | Url: @thisSyllabusPDF.Url</p>
        }
    }
    if(!syllabusPDFNodeSet)
    {
        var MediaRoot = Umbraco.MediaAtRoot();
        var sylabiFolder = MediaRoot.FirstOrDefault();
        var sylabiPdf = MediaRoot.FirstOrDefault();
        foreach (var mediaItem in MediaRoot){
            if(mediaItem.Name == "Course Syllabi"){
                sylabiFolder = mediaItem;
            }
        }
        foreach (var mediaItem in sylabiFolder.Children().Where(x => x.IsVisible()).OrderBy(x => x.CreateDate)){
            if(mediaItem.Url.Contains(Model.Name.ToLower() + ".pdf")){
                sylabiPdf = mediaItem;
                var thisCourseDocument = contentService.GetById(Model.Id);
                thisCourseDocument.SetValue("syllabusPDF", mediaItem.Id);
                contentService.SaveAndPublish(thisCourseDocument);
            }
        }
        if(sylabiPdf.Id != sylabiFolder.Id & sylabiPdf.Url != null){
            sylabiUrl = sylabiPdf.Url;
        }
    }
    // End SylabusPDF syllabusPDF object.
    
Please Sign in or register to post replies

Write your reply to:

Draft