Skip to content

Commit 3910243

Browse files
committed
Merge branch 'dev-v7' into dev-v7-centralizedloadbalancing
2 parents 516ae5f + 2f74dfe commit 3910243

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/Umbraco.Core/Services/MediaService.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Globalization;
44
using System.Linq;
5+
using System.Text.RegularExpressions;
56
using System.Threading;
67
using System.Xml.Linq;
78
using Umbraco.Core.Auditing;
@@ -567,28 +568,38 @@ public IEnumerable<IMedia> GetMediaInRecycleBin()
567568
public IMedia GetMediaByPath(string mediaPath)
568569
{
569570
var umbracoFileValue = mediaPath;
570-
var isResized = mediaPath.Contains("_") && mediaPath.Contains("x");
571+
const string Pattern = ".*[_][0-9]+[x][0-9]+[.].*";
572+
var isResized = Regex.IsMatch(mediaPath, Pattern);
571573

572574
// If the image has been resized we strip the "_403x328" of the original "/media/1024/koala_403x328.jpg" url.
573575
if (isResized)
574576
{
575-
576577
var underscoreIndex = mediaPath.LastIndexOf('_');
577578
var dotIndex = mediaPath.LastIndexOf('.');
578579
umbracoFileValue = string.Concat(mediaPath.Substring(0, underscoreIndex), mediaPath.Substring(dotIndex));
579580
}
580581

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);
582+
Func<string, Sql> createSql = url => new Sql().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 == url);
588+
589+
var sql = createSql(umbracoFileValue);
588590

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

0 commit comments

Comments
 (0)