diff --git a/uSync.History/Controllers/uSyncHistoryController.cs b/uSync.History/Controllers/uSyncHistoryController.cs index c971af2b..e5d7d2a9 100644 --- a/uSync.History/Controllers/uSyncHistoryController.cs +++ b/uSync.History/Controllers/uSyncHistoryController.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using Umbraco.Cms.Core.Hosting; using Umbraco.Cms.Infrastructure.Migrations.Expressions.Delete; using Umbraco.Cms.Web.BackOffice.Controllers; using Umbraco.Cms.Web.Common.Attributes; @@ -11,21 +12,20 @@ namespace uSync.History.Controllers [PluginController("uSync")] public class uSyncHistoryController : UmbracoAuthorizedApiController { - private readonly uSyncConfigService _configService; + private readonly IHostingEnvironment _hostingEnvironment; private readonly SyncFileService _syncFileService; - public uSyncHistoryController(uSyncConfigService configService, SyncFileService syncFileService) + public uSyncHistoryController(SyncFileService syncFileService, IHostingEnvironment hostingEnvironment) { - _configService = configService; _syncFileService = syncFileService; + _hostingEnvironment = hostingEnvironment; } public bool GetApi() => true; public IEnumerable GetHistory() { - var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder()); - var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history")); + string historyFolder = GetHistoryFolder(); var files = _syncFileService.GetFiles(historyFolder, "*.json") .Select(x => x.Substring(historyFolder.Length + 1)); @@ -38,11 +38,17 @@ public IEnumerable GetHistory() return list.OrderByDescending(x => x.Date); } + private string GetHistoryFolder() + { + var rootFolder = _syncFileService.GetAbsPath(_hostingEnvironment.LocalTempPath); + var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "uSync", "history")); + return historyFolder; + } + public bool ClearHistory() { // 1. get history folder - var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder()); - var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history")); + string historyFolder = GetHistoryFolder(); // 2. get history files var files = _syncFileService.GetFiles(historyFolder, "*.json"); // 3. delet this @@ -56,8 +62,7 @@ public bool ClearHistory() public HistoryInfo LoadHistory(string filePath) { - var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder()); - var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history")); + string historyFolder = GetHistoryFolder(); var fullPath = Path.Combine(historyFolder, filePath); string contents = _syncFileService.LoadContent(fullPath); diff --git a/uSync.History/HistoryInfo.cs b/uSync.History/HistoryInfo.cs index 9f0e6452..25fb82d9 100644 --- a/uSync.History/HistoryInfo.cs +++ b/uSync.History/HistoryInfo.cs @@ -17,5 +17,9 @@ public class HistoryInfo public string Method { get; set; } public string FilePath { get; set; } + + public int Changes { get; set; } + + public int Total { get; set; } } } diff --git a/uSync.History/uSyncHistory.cs b/uSync.History/uSyncHistory.cs index 96ef351b..479d9555 100644 --- a/uSync.History/uSyncHistory.cs +++ b/uSync.History/uSyncHistory.cs @@ -9,9 +9,9 @@ namespace uSync.History { public class uSyncHistory : ISyncAddOn { - public string Name => "uSync History"; + public string Name => "_uSync History"; - public string Version => "1.0"; + public string Version => typeof(uSyncHistory).Assembly.GetName().Version.ToString(3); public string Icon => "icon-calendar-alt"; diff --git a/uSync.History/uSyncHistoryNotificationHandler.cs b/uSync.History/uSyncHistoryNotificationHandler.cs index 09059625..bd0262e4 100644 --- a/uSync.History/uSyncHistoryNotificationHandler.cs +++ b/uSync.History/uSyncHistoryNotificationHandler.cs @@ -1,5 +1,7 @@ -using Newtonsoft.Json; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Hosting; using Umbraco.Cms.Core.Security; using uSync.BackOffice; using uSync.BackOffice.Configuration; @@ -11,15 +13,21 @@ internal class uSyncHistoryNotificationHandler : INotificationHandler, INotificationHandler { - private readonly uSyncConfigService _configService; + private readonly IHostingEnvironment _hostingEnvironment; private readonly SyncFileService _syncFileService; private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; + private readonly ILogger _logger; - public uSyncHistoryNotificationHandler(uSyncConfigService configService, SyncFileService syncFileService, IBackOfficeSecurityAccessor backOfficeSecurityAccessor) + public uSyncHistoryNotificationHandler( + SyncFileService syncFileService, + IBackOfficeSecurityAccessor backOfficeSecurityAccessor, + IHostingEnvironment hostingEnvironment, + ILogger logger) { - _configService = configService; _syncFileService = syncFileService; _backOfficeSecurityAccessor = backOfficeSecurityAccessor; + _hostingEnvironment = hostingEnvironment; + _logger = logger; } public void Handle(uSyncImportCompletedNotification notification) @@ -30,7 +38,7 @@ public void Handle(uSyncImportCompletedNotification notification) if (changeActions.Any()) { - SaveActions(changeActions, "Import"); + SaveActions(changeActions, "Import", notification.Actions.Count()); } } @@ -42,28 +50,37 @@ public void Handle(uSyncExportCompletedNotification notification) if (changeActions.Any()) { - SaveActions(changeActions, "Export"); + SaveActions(changeActions, "Export", notification.Actions.Count()); } } - private void SaveActions(IEnumerable actions, string method) + private void SaveActions(IEnumerable actions, string method, int total) { - var historyInfo = new HistoryInfo + try { - Actions = actions, - Date = DateTime.Now, - Username = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Username ?? "Background Process", - Method = method - }; + var historyInfo = new HistoryInfo + { + Actions = actions, + Date = DateTime.Now, + Username = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Username ?? "Background Process", + Method = method, + Total = total, + Changes = actions.CountChanges() + }; - var historyJson = JsonConvert.SerializeObject(historyInfo, Formatting.Indented); + var historyJson = JsonConvert.SerializeObject(historyInfo, Formatting.Indented); - var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder()); - var historyFile = Path.Combine(rootFolder, "..", "history", DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".json"); + var rootFolder = _syncFileService.GetAbsPath(_hostingEnvironment.LocalTempPath); + var historyFile = Path.Combine(rootFolder, "uSync", "history", DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".json"); - _syncFileService.CreateFoldersForFile(historyFile); + _syncFileService.CreateFoldersForFile(historyFile); - _syncFileService.SaveFile(historyFile, historyJson); + _syncFileService.SaveFile(historyFile, historyJson); + } + catch(Exception ex) + { + _logger.LogWarning(ex, "Failed to save history."); + } } } } diff --git a/uSync.History/wwwroot/uSyncHistory/dashboard.html b/uSync.History/wwwroot/uSyncHistory/dashboard.html index 958dcb11..5e5dd62c 100644 --- a/uSync.History/wwwroot/uSyncHistory/dashboard.html +++ b/uSync.History/wwwroot/uSyncHistory/dashboard.html @@ -22,7 +22,9 @@
- +
+
+ {{item.changes}}/{{item.total}}
@@ -38,7 +40,7 @@
-
+