/
FileUploadCacheUpdateHandler.cs
94 lines (79 loc) · 3.19 KB
/
FileUploadCacheUpdateHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using cloudscribe.FileManager.Web.Events;
using cloudscribe.FileManager.Web.Models;
using cloudscribe.PwaKit.Interfaces;
using cloudscribe.PwaKit.Models;
using cloudscribe.PwaKit.Services;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace cloudscribe.PwaKit.Integration.CloudscribeCore
{
public class FileUploadCacheUpdateHandler : IHandleFilesUploaded
{
public FileUploadCacheUpdateHandler(
IOptions<PwaContentFilesPreCacheOptions> optionsAccessor,
IPushNotificationsQueue pushNotificationsQueue,
IUserIdResolver userIdResolver,
ITenantIdResolver tenantIdResolver
)
{
_options = optionsAccessor.Value;
_pushNotificationsQueue = pushNotificationsQueue;
_userIdResolver = userIdResolver;
_tenantIdResolver = tenantIdResolver;
}
private readonly PwaContentFilesPreCacheOptions _options;
private readonly IPushNotificationsQueue _pushNotificationsQueue;
private readonly IUserIdResolver _userIdResolver;
private readonly ITenantIdResolver _tenantIdResolver;
public Task Handle(IEnumerable<UploadResult> uploadInfoList)
{
//TODO: would be better to send the whole list in one message
// but this will require different handling in serviceworker
foreach(var u in uploadInfoList)
{
if(!string.IsNullOrEmpty(u.OriginalUrl))
{
var ext = Path.GetExtension(u.OriginalUrl).ToLower();
if(_options.FileExtensionsToCache.Contains(ext))
{
SendNewContentNotification(u.OriginalUrl);
}
}
if (!string.IsNullOrEmpty(u.ResizedUrl))
{
var ext = Path.GetExtension(u.ResizedUrl).ToLower();
if (_options.FileExtensionsToCache.Contains(ext))
{
SendNewContentNotification(u.ResizedUrl);
}
}
if (!string.IsNullOrEmpty(u.ThumbUrl))
{
var ext = Path.GetExtension(u.ThumbUrl).ToLower();
if (_options.FileExtensionsToCache.Contains(ext))
{
SendNewContentNotification(u.ThumbUrl);
}
}
}
return Task.CompletedTask;
}
private void SendNewContentNotification(string url)
{
var message = new PushMessageModel()
{
MessageType = "newcontent",
Body = "New content",
Data = url
};
var queueItem = new PushQueueItem(
message,
BuiltInRecipientProviderNames.AllButCurrentUserPushNotificationRecipientProvider);
queueItem.TenantId = _tenantIdResolver.GetTenantId();
queueItem.RecipientProviderCustom1 = _userIdResolver.GetCurrentUserId();
_pushNotificationsQueue.Enqueue(queueItem);
}
}
}