0

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.

serge
  • 13,940
  • 35
  • 121
  • 205
  • `SHOW CREATE TABLE` – Rick James Dec 01 '17 at 02:21
  • Isn't there only one piece of media for each media node? I don't really understand your question. – harvzor Dec 01 '17 at 13:22
  • I have multiple sites. Say "Site A" has a folder in the media folders. I need to recuperate all medias that the "SiteA" is using, I mean, the list of media from the "SiteA" media folder. – serge Dec 01 '17 at 13:29

2 Answers2

2

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.

harvzor
  • 2,832
  • 1
  • 22
  • 40
0

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.

harvzor
  • 2,832
  • 1
  • 22
  • 40
  • the problem is I have multiple sites, each of them having a media folder in the backOffice. And I need to analyse site by site the medias, so, all medias from a media folder – serge Dec 01 '17 at 13:32