Skip to content

Commit dd1e6a5

Browse files
committed
More work on image processing
1 parent 0cdf75b commit dd1e6a5

37 files changed

Lines changed: 418 additions & 607 deletions

src/Libraries/SmartStore.Core/Events/IConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ namespace SmartStore.Core.Events
33
{
44
public interface IConsumer<T>
55
{
6-
void HandleEvent(T eventMessage);
6+
void HandleEvent(T message);
77
}
88
}

src/Libraries/SmartStore.Core/Logging/Notifier.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace SmartStore.Core.Logging
88
{
9-
109
public interface INotifier
1110
{
1211
void Add(NotifyType type, LocalizedString message, bool durable = true);

src/Libraries/SmartStore.Core/SmartStore.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@
663663
<Compile Include="Utilities\Threading\ReadLockDisposable.cs" />
664664
<Compile Include="Utilities\Threading\UpgradeableReadLockDisposable.cs" />
665665
<Compile Include="Utilities\Threading\WriteLockDisposable.cs" />
666+
<Compile Include="Utilities\Throttle.cs" />
666667
<Compile Include="Utilities\TypeHelper.cs" />
667668
<Compile Include="Utilities\Wildcard.cs" />
668669
<Compile Include="WebHelper.cs" />
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Concurrent;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SmartStore.Core.Utilities
9+
{
10+
public static class Throttle
11+
{
12+
private readonly static ConcurrentDictionary<string, CheckEntry> _checks = new ConcurrentDictionary<string, CheckEntry>(StringComparer.OrdinalIgnoreCase);
13+
14+
/// <summary>
15+
/// Performs a throttled check.
16+
/// </summary>
17+
/// <param name="key">Identifier for the check process</param>
18+
/// <param name="interval">Interval between actual checks</param>
19+
/// <param name="check">The check factory</param>
20+
/// <returns>Check result</returns>
21+
public static bool Check(string key, TimeSpan interval, Func<bool> check)
22+
{
23+
return Check(key, interval, false, check);
24+
}
25+
26+
/// <summary>
27+
/// Performs a throttled check.
28+
/// </summary>
29+
/// <param name="key">Identifier for the check process</param>
30+
/// <param name="interval">Interval between actual checks</param>
31+
/// <param name="recheckWhenFalse"></param>
32+
/// <param name="check">The check factory</param>
33+
/// <returns>Check result</returns>
34+
public static bool Check(string key, TimeSpan interval, bool recheckWhenFalse, Func<bool> check)
35+
{
36+
Guard.NotEmpty(key, nameof(key));
37+
Guard.NotNull(check, nameof(check));
38+
39+
bool added = false;
40+
var now = DateTime.UtcNow;
41+
42+
var entry = _checks.GetOrAdd(key, x =>
43+
{
44+
added = true;
45+
return new CheckEntry { Value = check(), NextCheckUtc = (now + interval) };
46+
});
47+
48+
if (added)
49+
{
50+
return entry.Value;
51+
}
52+
53+
var ok = entry.Value;
54+
var isOverdue = (!ok && recheckWhenFalse) || (now > entry.NextCheckUtc);
55+
56+
if (isOverdue)
57+
{
58+
// Check is overdue: recheck
59+
ok = check();
60+
_checks.TryUpdate(key, new CheckEntry { Value = ok, NextCheckUtc = (now + interval) }, entry);
61+
}
62+
63+
return ok;
64+
}
65+
66+
class CheckEntry
67+
{
68+
public bool Value { get; set; }
69+
public DateTime NextCheckUtc { get; set; }
70+
}
71+
}
72+
}

src/Libraries/SmartStore.Core/WebHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public partial class WebHelper : IWebHelper
2525
private static object s_lock = new object();
2626
private static bool? s_optimizedCompilationsEnabled;
2727
private static AspNetHostingPermissionLevel? s_trustLevel;
28-
private static readonly Regex s_staticExts = new Regex(@"(.*?)\.(css|js|png|jpg|jpeg|gif|scss|less|bmp|html|htm|xml|pdf|doc|xls|rar|zip|ico|eot|svg|ttf|woff|otf|axd|ashx)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
28+
private static readonly Regex s_staticExts = new Regex(@"(.*?)\.(css|js|png|jpg|jpeg|gif|webp|scss|less|bmp|html|htm|xml|pdf|doc|xls|rar|zip|7z|ico|eot|svg|ttf|woff|otf|axd|ashx)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
2929
private static readonly Regex s_htmlPathPattern = new Regex(@"(?<=(?:href|src)=(?:""|'))(?!https?://)(?<url>[^(?:""|')]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
3030
private static readonly Regex s_cssPathPattern = new Regex(@"url\('(?<url>.+)'\)", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
3131
private static ConcurrentDictionary<int, string> s_safeLocalHostNames = new ConcurrentDictionary<int, string>();

src/Libraries/SmartStore.Services/Catalog/Importer/CategoryImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ protected virtual int ProcessPictures(
223223
}
224224

225225
var size = Size.Empty;
226-
pictureBinary = _pictureService.ValidatePicture(pictureBinary, out size);
226+
pictureBinary = _pictureService.ValidatePicture(pictureBinary, image.MimeType, out size);
227227
pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId);
228228

229229
if (pictureBinary != null && pictureBinary.Length > 0)

src/Libraries/SmartStore.Services/Catalog/Importer/ProductImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ protected virtual void ProcessProductPictures(ImportExecuteContext context, IEnu
560560
}
561561

562562
var size = Size.Empty;
563-
pictureBinary = _pictureService.ValidatePicture(pictureBinary, out size);
563+
pictureBinary = _pictureService.ValidatePicture(pictureBinary, image.MimeType, out size);
564564
pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId);
565565

566566
if (pictureBinary != null && pictureBinary.Length > 0)

src/Libraries/SmartStore.Services/CommonServices.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using SmartStore.Services.Stores;
1313
using Autofac;
1414
using SmartStore.Services.Helpers;
15+
using SmartStore.Services.Media;
1516

1617
namespace SmartStore.Services
1718
{
@@ -28,6 +29,7 @@ public class CommonServices : ICommonServices
2829
private readonly Lazy<IEventPublisher> _eventPublisher;
2930
private readonly Lazy<ILocalizationService> _localization;
3031
private readonly Lazy<ICustomerActivityService> _customerActivity;
32+
private readonly Lazy<IPictureService> _pictureService;
3133
private readonly Lazy<INotifier> _notifier;
3234
private readonly Lazy<IPermissionService> _permissions;
3335
private readonly Lazy<ISettingService> _settings;
@@ -48,6 +50,7 @@ public CommonServices(
4850
Lazy<IEventPublisher> eventPublisher,
4951
Lazy<ILocalizationService> localization,
5052
Lazy<ICustomerActivityService> customerActivity,
53+
Lazy<IPictureService> pictureService,
5154
Lazy<INotifier> notifier,
5255
Lazy<IPermissionService> permissions,
5356
Lazy<ISettingService> settings,
@@ -67,6 +70,7 @@ public CommonServices(
6770
this._eventPublisher = eventPublisher;
6871
this._localization = localization;
6972
this._customerActivity = customerActivity;
73+
this._pictureService = pictureService;
7074
this._notifier = notifier;
7175
this._permissions = permissions;
7276
this._settings = settings;
@@ -164,6 +168,14 @@ public ICustomerActivityService CustomerActivity
164168
}
165169
}
166170

171+
public IPictureService PictureService
172+
{
173+
get
174+
{
175+
return _pictureService.Value;
176+
}
177+
}
178+
167179
public INotifier Notifier
168180
{
169181
get

src/Libraries/SmartStore.Services/Customers/Importer/CustomerImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ protected virtual int ProcessAvatars(
604604
}
605605

606606
var size = Size.Empty;
607-
pictureBinary = _pictureService.ValidatePicture(pictureBinary, out size);
607+
pictureBinary = _pictureService.ValidatePicture(pictureBinary, image.MimeType, out size);
608608
pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId);
609609

610610
if (pictureBinary != null && pictureBinary.Length > 0)

src/Libraries/SmartStore.Services/ICommonServices.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using SmartStore.Services.Stores;
1212
using SmartStore.Services.Helpers;
1313
using Autofac;
14+
using SmartStore.Services.Media;
1415

1516
namespace SmartStore.Services
1617
{
@@ -76,6 +77,11 @@ ICustomerActivityService CustomerActivity
7677
get;
7778
}
7879

80+
IPictureService PictureService
81+
{
82+
get;
83+
}
84+
7985
INotifier Notifier
8086
{
8187
get;

0 commit comments

Comments
 (0)