Skip to content

Commit 456e7d9

Browse files
committed
Merge branch '7.2.0-U4-6004' of https://github.com/21robin12/Umbraco-CMS into 21robin12-7.2.0-U4-6004
2 parents 5bf9362 + c256266 commit 456e7d9

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/Umbraco.Core/Services/MediaService.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,28 +567,38 @@ public IEnumerable<IMedia> GetMediaInRecycleBin()
567567
public IMedia GetMediaByPath(string mediaPath)
568568
{
569569
var umbracoFileValue = mediaPath;
570-
var isResized = mediaPath.Contains("_") && mediaPath.Contains("x");
570+
const string Pattern = ".*[_][0-9]+[x][0-9]+[.].*";
571+
var isResized = Regex.IsMatch(mediaPath, Pattern);
571572

572573
// If the image has been resized we strip the "_403x328" of the original "/media/1024/koala_403x328.jpg" url.
573574
if (isResized)
574575
{
575-
576576
var underscoreIndex = mediaPath.LastIndexOf('_');
577577
var dotIndex = mediaPath.LastIndexOf('.');
578578
umbracoFileValue = string.Concat(mediaPath.Substring(0, underscoreIndex), mediaPath.Substring(dotIndex));
579579
}
580580

581-
var sql = new Sql()
582-
.Select("*")
583-
.From<PropertyDataDto>()
584-
.InnerJoin<PropertyTypeDto>()
585-
.On<PropertyDataDto, PropertyTypeDto>(left => left.PropertyTypeId, right => right.Id)
586-
.Where<PropertyTypeDto>(x => x.Alias == "umbracoFile")
587-
.Where<PropertyDataDto>(x => x.VarChar == umbracoFileValue);
581+
Func<string, Sql> createSql = url => new Sql().Select("*")
582+
.From<PropertyDataDto>()
583+
.InnerJoin<PropertyTypeDto>()
584+
.On<PropertyDataDto, PropertyTypeDto>(left => left.PropertyTypeId, right => right.Id)
585+
.Where<PropertyTypeDto>(x => x.Alias == "umbracoFile")
586+
.Where<PropertyDataDto>(x => x.VarChar == url);
587+
588+
var sql = createSql(umbracoFileValue);
588589

589590
using (var uow = _uowProvider.GetUnitOfWork())
590591
{
591592
var propertyDataDto = uow.Database.Fetch<PropertyDataDto, PropertyTypeDto>(sql).FirstOrDefault();
593+
594+
// If the stripped-down url returns null, we try again with the original url.
595+
// Previously, the function would fail on e.g. "my_x_image.jpg"
596+
if (propertyDataDto == null)
597+
{
598+
sql = createSql(mediaPath);
599+
propertyDataDto = uow.Database.Fetch<PropertyDataDto, PropertyTypeDto>(sql).FirstOrDefault();
600+
}
601+
592602
return propertyDataDto == null ? null : GetById(propertyDataDto.NodeId);
593603
}
594604
}

0 commit comments

Comments
 (0)