| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading; |
| | | 8 | | using MediaBrowser.Controller.Configuration; |
| | | 9 | | using MediaBrowser.Controller.Entities; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using Microsoft.Extensions.Logging; |
| | | 12 | | |
| | | 13 | | namespace Emby.Server.Implementations.IO |
| | | 14 | | { |
| | | 15 | | public sealed class FileRefresher : IDisposable |
| | | 16 | | { |
| | | 17 | | private readonly ILogger _logger; |
| | | 18 | | private readonly ILibraryManager _libraryManager; |
| | | 19 | | private readonly IServerConfigurationManager _configurationManager; |
| | | 20 | | |
| | 1 | 21 | | private readonly List<string> _affectedPaths = new(); |
| | 1 | 22 | | private readonly Lock _timerLock = new(); |
| | | 23 | | private Timer? _timer; |
| | | 24 | | private bool _disposed; |
| | | 25 | | |
| | | 26 | | public FileRefresher(string path, IServerConfigurationManager configurationManager, ILibraryManager libraryManag |
| | | 27 | | { |
| | 1 | 28 | | logger.LogDebug("New file refresher created for {0}", path); |
| | 1 | 29 | | Path = path; |
| | | 30 | | |
| | 1 | 31 | | _configurationManager = configurationManager; |
| | 1 | 32 | | _libraryManager = libraryManager; |
| | 1 | 33 | | _logger = logger; |
| | 1 | 34 | | AddPath(path); |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | public event EventHandler<EventArgs>? Completed; |
| | | 38 | | |
| | | 39 | | public string Path { get; private set; } |
| | | 40 | | |
| | | 41 | | private void AddAffectedPath(string path) |
| | | 42 | | { |
| | 2 | 43 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | | 44 | | |
| | 2 | 45 | | if (!_affectedPaths.Contains(path, StringComparer.Ordinal)) |
| | | 46 | | { |
| | 2 | 47 | | _affectedPaths.Add(path); |
| | | 48 | | } |
| | 2 | 49 | | } |
| | | 50 | | |
| | | 51 | | public void AddPath(string path) |
| | | 52 | | { |
| | 2 | 53 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | | 54 | | |
| | | 55 | | lock (_timerLock) |
| | | 56 | | { |
| | 2 | 57 | | AddAffectedPath(path); |
| | 2 | 58 | | } |
| | | 59 | | |
| | 2 | 60 | | RestartTimer(); |
| | 2 | 61 | | } |
| | | 62 | | |
| | | 63 | | public void RestartTimer() |
| | | 64 | | { |
| | 2 | 65 | | if (_disposed) |
| | | 66 | | { |
| | 0 | 67 | | return; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | lock (_timerLock) |
| | | 71 | | { |
| | 2 | 72 | | if (_disposed) |
| | | 73 | | { |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | 2 | 77 | | if (_timer is null) |
| | | 78 | | { |
| | 1 | 79 | | _timer = new Timer(OnTimerCallback, null, TimeSpan.FromSeconds(_configurationManager.Configuration.L |
| | | 80 | | } |
| | | 81 | | else |
| | | 82 | | { |
| | 1 | 83 | | _timer.Change(TimeSpan.FromSeconds(_configurationManager.Configuration.LibraryMonitorDelay), TimeSpa |
| | | 84 | | } |
| | 1 | 85 | | } |
| | 2 | 86 | | } |
| | | 87 | | |
| | | 88 | | public void ResetPath(string path, string? affectedFile) |
| | 0 | 89 | | { |
| | | 90 | | lock (_timerLock) |
| | | 91 | | { |
| | 0 | 92 | | _logger.LogDebug("Resetting file refresher from {0} to {1}", Path, path); |
| | | 93 | | |
| | 0 | 94 | | Path = path; |
| | 0 | 95 | | AddAffectedPath(path); |
| | | 96 | | |
| | 0 | 97 | | if (!string.IsNullOrEmpty(affectedFile)) |
| | | 98 | | { |
| | 0 | 99 | | AddAffectedPath(affectedFile); |
| | | 100 | | } |
| | 0 | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | RestartTimer(); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | private void OnTimerCallback(object? state) |
| | 1 | 107 | | { |
| | | 108 | | List<string> paths; |
| | | 109 | | |
| | | 110 | | lock (_timerLock) |
| | | 111 | | { |
| | 1 | 112 | | if (_disposed) |
| | | 113 | | { |
| | 0 | 114 | | return; |
| | | 115 | | } |
| | | 116 | | |
| | 1 | 117 | | paths = _affectedPaths.ToList(); |
| | 1 | 118 | | } |
| | | 119 | | |
| | 1 | 120 | | _logger.LogDebug("Timer stopped."); |
| | | 121 | | |
| | 1 | 122 | | DisposeTimer(); |
| | 1 | 123 | | Completed?.Invoke(this, EventArgs.Empty); |
| | | 124 | | |
| | | 125 | | try |
| | | 126 | | { |
| | 1 | 127 | | ProcessPathChanges(paths); |
| | 1 | 128 | | } |
| | 0 | 129 | | catch (Exception ex) |
| | | 130 | | { |
| | 0 | 131 | | _logger.LogError(ex, "Error processing directory changes"); |
| | 0 | 132 | | } |
| | 1 | 133 | | } |
| | | 134 | | |
| | | 135 | | private void ProcessPathChanges(List<string> paths) |
| | | 136 | | { |
| | 1 | 137 | | var itemsToRefresh = paths |
| | 1 | 138 | | .Distinct() |
| | 1 | 139 | | .Select(TryGetAffectedBaseItem) |
| | 1 | 140 | | .OfType<BaseItem>() |
| | 1 | 141 | | .DistinctBy(x => x.Id) |
| | 1 | 142 | | .ToList(); |
| | | 143 | | |
| | 2 | 144 | | foreach (var item in itemsToRefresh) |
| | | 145 | | { |
| | 0 | 146 | | if (item is AggregateFolder) |
| | | 147 | | { |
| | | 148 | | continue; |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | _logger.LogInformation("{Name} ({Path}) will be refreshed.", item.Name, item.Path); |
| | | 152 | | |
| | | 153 | | try |
| | | 154 | | { |
| | 0 | 155 | | item.ChangedExternally(); |
| | 0 | 156 | | } |
| | 0 | 157 | | catch (Exception ex) |
| | | 158 | | { |
| | 0 | 159 | | _logger.LogError(ex, "Error refreshing {Name}", item.Name); |
| | 0 | 160 | | } |
| | | 161 | | } |
| | 1 | 162 | | } |
| | | 163 | | |
| | | 164 | | private BaseItem? TryGetAffectedBaseItem(string path) |
| | | 165 | | { |
| | | 166 | | try |
| | | 167 | | { |
| | 2 | 168 | | return GetAffectedBaseItem(path); |
| | | 169 | | } |
| | 1 | 170 | | catch (Exception ex) |
| | | 171 | | { |
| | 1 | 172 | | _logger.LogError(ex, "Error finding the item affected by changes to {Path}", path); |
| | 1 | 173 | | return null; |
| | | 174 | | } |
| | 2 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets the affected base item. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <param name="path">The path.</param> |
| | | 181 | | /// <returns>BaseItem.</returns> |
| | | 182 | | private BaseItem? GetAffectedBaseItem(string path) |
| | | 183 | | { |
| | 2 | 184 | | BaseItem? item = null; |
| | | 185 | | |
| | 3 | 186 | | while (item is null && !string.IsNullOrEmpty(path)) |
| | | 187 | | { |
| | 2 | 188 | | item = _libraryManager.FindByPath(path, null); |
| | | 189 | | |
| | 1 | 190 | | path = System.IO.Path.GetDirectoryName(path) ?? string.Empty; |
| | | 191 | | } |
| | | 192 | | |
| | 1 | 193 | | if (item is not null) |
| | | 194 | | { |
| | | 195 | | // If the item has been deleted find the first valid parent that still exists |
| | 1 | 196 | | while (!Directory.Exists(item.Path) && !File.Exists(item.Path)) |
| | | 197 | | { |
| | 1 | 198 | | item = item.GetOwner() ?? item.GetParent(); |
| | | 199 | | |
| | 1 | 200 | | if (item is null) |
| | | 201 | | { |
| | | 202 | | break; |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | 1 | 207 | | return item; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | private void DisposeTimer() |
| | 2 | 211 | | { |
| | | 212 | | lock (_timerLock) |
| | | 213 | | { |
| | 2 | 214 | | if (_timer is not null) |
| | | 215 | | { |
| | 1 | 216 | | _timer.Dispose(); |
| | 1 | 217 | | _timer = null; |
| | | 218 | | } |
| | 2 | 219 | | } |
| | 2 | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <inheritdoc /> |
| | | 223 | | public void Dispose() |
| | | 224 | | { |
| | 1 | 225 | | if (_disposed) |
| | | 226 | | { |
| | 0 | 227 | | return; |
| | | 228 | | } |
| | | 229 | | |
| | 1 | 230 | | DisposeTimer(); |
| | 1 | 231 | | _disposed = true; |
| | 1 | 232 | | } |
| | | 233 | | } |
| | | 234 | | } |