diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index f978006c15..34b2c5ab93 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -572,23 +572,32 @@ namespace Umbraco.Core.Services // If the image has been resized we strip the "_403x328" of the original "/media/1024/koala_403x328.jpg" url. if (isResized) { - var underscoreIndex = mediaPath.LastIndexOf('_'); var dotIndex = mediaPath.LastIndexOf('.'); umbracoFileValue = string.Concat(mediaPath.Substring(0, underscoreIndex), mediaPath.Substring(dotIndex)); } - var sql = new Sql() - .Select("*") - .From() - .InnerJoin() - .On(left => left.PropertyTypeId, right => right.Id) - .Where(x => x.Alias == "umbracoFile") - .Where(x => x.VarChar == umbracoFileValue); + Func createSql = url => new Sql().Select("*") + .From() + .InnerJoin() + .On(left => left.PropertyTypeId, right => right.Id) + .Where(x => x.Alias == "umbracoFile") + .Where(x => x.VarChar == url); + + var sql = createSql(umbracoFileValue); using (var uow = _uowProvider.GetUnitOfWork()) { var propertyDataDto = uow.Database.Fetch(sql).FirstOrDefault(); + + // If the stripped-down url returns null, we try again with the original url. + // Previously, the function would fail on e.g. "my_x_image.jpg" + if (propertyDataDto == null) + { + sql = createSql(mediaPath); + propertyDataDto = uow.Database.Fetch(sql).FirstOrDefault(); + } + return propertyDataDto == null ? null : GetById(propertyDataDto.NodeId); } }