Created
May 31, 2019 15:00
-
-
Save Attackmonkey/d87e58d93c3043e34a38fad70f555a72 to your computer and use it in GitHub Desktop.
How to get all large media items from Umbraco. You COULD use the Media Service, but this will be CONSIDERABLY faster. This example is just a surface controller and has no authentication. On a production environment you'd probably want to limit something like this to authenticated users.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Web; | |
using System.Web.Mvc; | |
using Umbraco.Core.Models; | |
using Umbraco.Web; | |
using Umbraco.Web.Mvc; | |
namespace MySite.Web.Controllers | |
{ | |
public class GiantMediaController : SurfaceController | |
{ | |
public ActionResult GetGiantImages() | |
{ | |
var builder = new StringBuilder(); | |
var bigImageData = GetLargeImages(); | |
builder.AppendLine($"<h1>Images Over 3MB ({bigImageData.Count()} Files):</h1>"); | |
builder.AppendLine("<table border=\"1\">"); | |
builder.AppendLine("<tr><th>Image Name</th><th>Size (MB)</th><th>Width</th><th>Height</th><th>Link to File</th><th>In Recycle Bin</th></tr>"); | |
foreach (var image in bigImageData) | |
{ | |
var umbracoImage = Umbraco.TypedMedia(image.Id); | |
builder.AppendLine($"<tr><td>{image.Text}</td><td>{(image.DataNVarchar / 1048576).ToString("F2")} MB</td><td>{umbracoImage?.GetPropertyValue("umbracoWidth", "n/a")}</td><td>{umbracoImage?.GetPropertyValue("umbracoHeight", "n/a")}</td><td><a href='{umbracoImage?.Url}'>{umbracoImage?.Url}</a></td><td>@(umbracoImage == null)</td></tr>"); | |
} | |
builder.AppendLine("</table>"); | |
return Content(builder.ToString()); | |
} | |
private IEnumerable<BigImage> GetLargeImages() | |
{ | |
//Do this as a SQL query, as listing and filtering media via the service API is VERY slow | |
//Currently looking for images over 3MB, can make this smaller if need be | |
var bigImageData = ApplicationContext.DatabaseContext.Database.Query<BigImage>(@"SELECT umbracoNode.id, umbracoNode.text, cmsPropertyData.dataNvarchar FROM cmsPropertyData | |
INNER JOIN umbracoNode ON cmsPropertyData.contentNodeId = umbracoNode.id | |
INNER JOIN cmsPropertyType ON cmsPropertyData.propertytypeid = cmsPropertyType.id | |
WHERE cmsPropertyType.Alias = 'umbracoBytes' AND dataNVarchar IS NOT NULL AND | |
(CASE WHEN ISNUMERIC(dataNvarchar) = 1 AND NOT dataNvarchar LIKE '%,%' AND NOT dataNvarchar LIKE '%.%' THEN CAST(dataNvarchar AS int) ELSE 0 END) > 3145728 | |
ORDER BY CAST(dataNvarchar AS int) DESC").ToList(); | |
return bigImageData; | |
} | |
} | |
public class BigImage | |
{ | |
public int Id { get; set; } | |
public string Text { get; set; } | |
public decimal DataNVarchar { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment