serge
serge

Reputation: 15229

umbraco: get a list of media of a media node

I use Umbraco 4x.

I need a way to get the list of the media from a media node and its size, in order to see how and where to optimize the (uploaded) pictures.

Because some uploaded pictures are too large, so it takes time to display them, so if I am searching a a way to optimize the media loading.

I found a little sql script that list the medias

select cmsPropertyData.id from cmsPropertyData
inner join cmsPropertyType on cmsPropertyData.propertytypeid = cmsPropertyType.id
inner join cmsDataType on cmsPropertyType.dataTypeId = cmsDataType.nodeId
where cmsDataType.controlId = '5032a6e6-69e3-491d-bb28-cd31cd11086c' 
    and cmsPropertyData.dataNvarchar is not null

enter image description here

but I rather need a list of nodes from a given node, the path and the file size.

Upvotes: 0

Views: 1466

Answers (2)

harvzor
harvzor

Reputation: 2908

I find it a bit easier using the built in Umbraco APIs to get information about media. This should work provided you're using CSHTML somewhere:

@{ 
    // Make sure to replace MEDIA_ID with the ID of your folder.
    var folder = Umbraco.TypedMedia(MEDIA_ID);
    var mediaItems = folder.Descendants();

    <table>
        <thead>
            <tr>
                <td>ID</td>
                <td>URL</td>
                <td>Size</td>
            </tr>
        </thead>
        <tbody>
            @foreach (var mediaItem in mediaItems)
            {
                <tr>
                    <td>@(mediaItem.Id)</td>
                    <td>@(mediaItem.Url)</td>
                    <td>@(mediaItem.GetPropertyValue("umbracoBytes"))</td>
                </tr>
            }
        </tbody>
    </table>
}

This gets your folder, then find all of children and displays them to the page.

Alternatively you could refactor this into a C# API or Surface controller.

Update: this was tested on Umbraco 7 so I'm not sure if it will work with Umbraco 4.

Upvotes: 2

harvzor
harvzor

Reputation: 2908

Rather than trying to find out image sizes using the database, I recommend just checking the file system (assuming your storing the media locally and not on a cloud/CDN).

You can just browse your /media/ found to check which files are the largest.

Or better yet, download a program like WinDirStat and get it to analyse your /media/ folder:

enter image description here

With WinDirStat, you can see a visual representation of the files. With my result, I can see my largest image is called banner1.jpg (with the media ID 1021). It has a size of about 700KB.

I hope this is relevant to your needs.

Upvotes: 0

Related Questions