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