From 2e32b6542158360d6928c1618aa9753a36768c24 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Wed, 5 Oct 2022 08:00:45 -0400 Subject: [PATCH] add file download event --- Oqtane.Server/Controllers/FileController.cs | 13 +++++++++---- .../CacheInvalidationHostedService.cs} | 4 ++-- Oqtane.Server/Pages/Files.cshtml.cs | 8 +++++--- 3 files changed, 16 insertions(+), 9 deletions(-) rename Oqtane.Server/Infrastructure/{CacheInvalidationService.cs => HostedServices/CacheInvalidationHostedService.cs} (87%) diff --git a/Oqtane.Server/Controllers/FileController.cs b/Oqtane.Server/Controllers/FileController.cs index 5594b168..fe8aab8c 100644 --- a/Oqtane.Server/Controllers/FileController.cs +++ b/Oqtane.Server/Controllers/FileController.cs @@ -491,10 +491,15 @@ namespace Oqtane.Controllers var filepath = _files.GetFilePath(file); if (System.IO.File.Exists(filepath)) { - var result = asAttachment - ? PhysicalFile(filepath, file.GetMimeType(), file.Name) - : PhysicalFile(filepath, file.GetMimeType()); - return result; + if (asAttachment) + { + _syncManager.AddSyncEvent(_alias.TenantId, EntityNames.File, file.FileId, "Download"); + return PhysicalFile(filepath, file.GetMimeType(), file.Name); + } + else + { + return PhysicalFile(filepath, file.GetMimeType()); + } } else { diff --git a/Oqtane.Server/Infrastructure/CacheInvalidationService.cs b/Oqtane.Server/Infrastructure/HostedServices/CacheInvalidationHostedService.cs similarity index 87% rename from Oqtane.Server/Infrastructure/CacheInvalidationService.cs rename to Oqtane.Server/Infrastructure/HostedServices/CacheInvalidationHostedService.cs index bae2d4f4..68eade75 100644 --- a/Oqtane.Server/Infrastructure/CacheInvalidationService.cs +++ b/Oqtane.Server/Infrastructure/HostedServices/CacheInvalidationHostedService.cs @@ -7,12 +7,12 @@ using Oqtane.Shared; namespace Oqtane.Infrastructure { - public class EventJob : IHostedService + public class CacheInvalidationHostedService : IHostedService { private readonly ISyncManager _syncManager; private readonly IMemoryCache _cache; - public EventJob(ISyncManager syncManager, IMemoryCache cache) + public CacheInvalidationHostedService(ISyncManager syncManager, IMemoryCache cache) { _syncManager = syncManager; _cache = cache; diff --git a/Oqtane.Server/Pages/Files.cshtml.cs b/Oqtane.Server/Pages/Files.cshtml.cs index 84d8ef16..3c78469e 100644 --- a/Oqtane.Server/Pages/Files.cshtml.cs +++ b/Oqtane.Server/Pages/Files.cshtml.cs @@ -23,15 +23,17 @@ namespace Oqtane.Pages private readonly IFileRepository _files; private readonly IUserPermissions _userPermissions; private readonly IUrlMappingRepository _urlMappings; + private readonly ISyncManager _syncManager; private readonly ILogManager _logger; private readonly Alias _alias; - public FilesModel(IWebHostEnvironment environment, IFileRepository files, IUserPermissions userPermissions, IUrlMappingRepository urlMappings, ILogManager logger, ITenantManager tenantManager) + public FilesModel(IWebHostEnvironment environment, IFileRepository files, IUserPermissions userPermissions, IUrlMappingRepository urlMappings, ISyncManager syncManager, ILogManager logger, ITenantManager tenantManager) { _environment = environment; _files = files; _userPermissions = userPermissions; _urlMappings = urlMappings; + _syncManager = syncManager; _logger = logger; _alias = tenantManager.GetAlias(); } @@ -43,10 +45,9 @@ namespace Oqtane.Pages var filename = ""; bool download = false; - if (path.Contains("?download")) + if (Request.Query.ContainsKey("download")) { download = true; - path = path.Substring(0, path.IndexOf("?download")); } var segments = path.Split('/'); @@ -78,6 +79,7 @@ namespace Oqtane.Pages { if (download) { + _syncManager.AddSyncEvent(_alias.TenantId, EntityNames.File, file.FileId, "Download"); return PhysicalFile(filepath, file.GetMimeType(), file.Name); } else