@@ -567,28 +567,38 @@ public IEnumerable<IMedia> GetMediaInRecycleBin()
567
567
public IMedia GetMediaByPath ( string mediaPath )
568
568
{
569
569
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 ) ;
571
572
572
573
// If the image has been resized we strip the "_403x328" of the original "/media/1024/koala_403x328.jpg" url.
573
574
if ( isResized )
574
575
{
575
-
576
576
var underscoreIndex = mediaPath . LastIndexOf ( '_' ) ;
577
577
var dotIndex = mediaPath . LastIndexOf ( '.' ) ;
578
578
umbracoFileValue = string . Concat ( mediaPath . Substring ( 0 , underscoreIndex ) , mediaPath . Substring ( dotIndex ) ) ;
579
579
}
580
580
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 ) ;
588
589
589
590
using ( var uow = _uowProvider . GetUnitOfWork ( ) )
590
591
{
591
592
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
+
592
602
return propertyDataDto == null ? null : GetById ( propertyDataDto . NodeId ) ;
593
603
}
594
604
}
0 commit comments