private static bool PreCheckWatchItemExpiry(WatchFolderItem newWatchItem, WatchFolderItem oldWatchItem) { if (oldWatchItem == null || oldWatchItem.LastChanged.AddHours(1) > DateTime.UtcNow) { return true; } newWatchItem.TotalSize = oldWatchItem.TotalSize; newWatchItem.Hash = oldWatchItem.Hash; return false; }
private static bool PreCheckWatchItemExpiry(WatchFolderItem newWatchItem, WatchFolderItem oldWatchItem) { if (oldWatchItem == null || oldWatchItem.LastChanged.AddHours(1) > DateTime.UtcNow) { return(true); } newWatchItem.TotalSize = oldWatchItem.TotalSize; newWatchItem.Hash = oldWatchItem.Hash; return(false); }
private static void UpdateWatchItemExpiry(WatchFolderItem newWatchItem, WatchFolderItem oldWatchItem, TimeSpan waitPeriod) { if (oldWatchItem != null && newWatchItem.Hash == oldWatchItem.Hash) { newWatchItem.LastChanged = oldWatchItem.LastChanged; } else { newWatchItem.LastChanged = DateTime.UtcNow; } var remainingTime = waitPeriod - (DateTime.UtcNow - newWatchItem.LastChanged); if (remainingTime > TimeSpan.Zero) { newWatchItem.RemainingTime = remainingTime; newWatchItem.Status = DownloadItemStatus.Downloading; } }
private IEnumerable<WatchFolderItem> GetDownloadItems(string watchFolder, Dictionary<string, WatchFolderItem> lastWatchItems, TimeSpan waitPeriod) { foreach (var folder in _diskProvider.GetDirectories(watchFolder)) { var title = FileNameBuilder.CleanFileName(Path.GetFileName(folder)); var newWatchItem = new WatchFolderItem { DownloadId = Path.GetFileName(folder) + "_" + _diskProvider.FolderGetCreationTime(folder).Ticks, Title = title, OutputPath = new OsPath(folder), Status = DownloadItemStatus.Completed, RemainingTime = TimeSpan.Zero }; var oldWatchItem = lastWatchItems.GetValueOrDefault(newWatchItem.DownloadId); if (PreCheckWatchItemExpiry(newWatchItem, oldWatchItem)) { var files = _diskProvider.GetFiles(folder, SearchOption.AllDirectories); newWatchItem.TotalSize = files.Select(_diskProvider.GetFileSize).Sum(); newWatchItem.Hash = GetHash(folder, files); if (files.Any(_diskProvider.IsFileLocked)) { newWatchItem.Status = DownloadItemStatus.Downloading; newWatchItem.RemainingTime = null; } UpdateWatchItemExpiry(newWatchItem, oldWatchItem, waitPeriod); } yield return newWatchItem; } foreach (var videoFile in _diskScanService.GetVideoFiles(watchFolder, false)) { var title = FileNameBuilder.CleanFileName(Path.GetFileName(videoFile)); var newWatchItem = new WatchFolderItem { DownloadId = Path.GetFileName(videoFile) + "_" + _diskProvider.FileGetLastWrite(videoFile).Ticks, Title = title, OutputPath = new OsPath(videoFile), Status = DownloadItemStatus.Completed, RemainingTime = TimeSpan.Zero }; var oldWatchItem = lastWatchItems.GetValueOrDefault(newWatchItem.DownloadId); if (PreCheckWatchItemExpiry(newWatchItem, oldWatchItem)) { newWatchItem.TotalSize = _diskProvider.GetFileSize(videoFile); newWatchItem.Hash = GetHash(videoFile); if (_diskProvider.IsFileLocked(videoFile)) { newWatchItem.Status = DownloadItemStatus.Downloading; } UpdateWatchItemExpiry(newWatchItem, oldWatchItem, waitPeriod); } yield return newWatchItem; } }
private IEnumerable <WatchFolderItem> GetDownloadItems(string watchFolder, Dictionary <string, WatchFolderItem> lastWatchItems, TimeSpan waitPeriod) { // get a fresh naming config each time, in case the user has made changes foreach (var folder in _diskProvider.GetDirectories(watchFolder)) { var title = FileNameBuilder.CleanFileName(Path.GetFileName(folder)); var newWatchItem = new WatchFolderItem { DownloadId = Path.GetFileName(folder) + "_" + _diskProvider.FolderGetCreationTime(folder).Ticks, Title = title, OutputPath = new OsPath(folder), Status = DownloadItemStatus.Completed, RemainingTime = TimeSpan.Zero }; var oldWatchItem = lastWatchItems.GetValueOrDefault(newWatchItem.DownloadId); if (PreCheckWatchItemExpiry(newWatchItem, oldWatchItem)) { var files = _diskProvider.GetFiles(folder, SearchOption.AllDirectories); newWatchItem.TotalSize = files.Select(_diskProvider.GetFileSize).Sum(); newWatchItem.Hash = GetHash(folder, files); if (files.Any(_diskProvider.IsFileLocked)) { newWatchItem.Status = DownloadItemStatus.Downloading; newWatchItem.RemainingTime = null; } UpdateWatchItemExpiry(newWatchItem, oldWatchItem, waitPeriod); } yield return(newWatchItem); } foreach (var videoFile in _diskScanService.GetVideoFiles(watchFolder, false)) { var title = FileNameBuilder.CleanFileName(Path.GetFileName(videoFile)); var newWatchItem = new WatchFolderItem { DownloadId = Path.GetFileName(videoFile) + "_" + _diskProvider.FileGetLastWrite(videoFile).Ticks, Title = title, OutputPath = new OsPath(videoFile), Status = DownloadItemStatus.Completed, RemainingTime = TimeSpan.Zero }; var oldWatchItem = lastWatchItems.GetValueOrDefault(newWatchItem.DownloadId); if (PreCheckWatchItemExpiry(newWatchItem, oldWatchItem)) { newWatchItem.TotalSize = _diskProvider.GetFileSize(videoFile); newWatchItem.Hash = GetHash(videoFile); if (_diskProvider.IsFileLocked(videoFile)) { newWatchItem.Status = DownloadItemStatus.Downloading; } UpdateWatchItemExpiry(newWatchItem, oldWatchItem, waitPeriod); } yield return(newWatchItem); } }
private IEnumerable <WatchFolderItem> GetDownloadItems(string watchFolder, Dictionary <string, WatchFolderItem> lastWatchItems, TimeSpan waitPeriod) { foreach (var folder in _diskScanService.FilterPaths(watchFolder, _diskProvider.GetDirectories(watchFolder))) { var title = FileNameBuilder.CleanFileName(Path.GetFileName(folder)); var newWatchItem = new WatchFolderItem { DownloadId = Path.GetFileName(folder) + "_" + _diskProvider.FolderGetCreationTime(folder).Ticks, Title = title, OutputPath = new OsPath(folder), Status = DownloadItemStatus.Completed, RemainingTime = TimeSpan.Zero }; var oldWatchItem = lastWatchItems.GetValueOrDefault(newWatchItem.DownloadId); if (PreCheckWatchItemExpiry(newWatchItem, oldWatchItem)) { var files = _diskProvider.GetFiles(folder, SearchOption.AllDirectories); newWatchItem.TotalSize = files.Select(_diskProvider.GetFileSize).Sum(); newWatchItem.Hash = GetHash(folder, files); if (files.Any(_diskProvider.IsFileLocked)) { newWatchItem.Status = DownloadItemStatus.Downloading; newWatchItem.RemainingTime = null; } UpdateWatchItemExpiry(newWatchItem, oldWatchItem, waitPeriod); } yield return(newWatchItem); } foreach (var audioFile in _diskScanService.FilterFiles(watchFolder, _diskScanService.GetAudioFiles(watchFolder, false))) { var title = FileNameBuilder.CleanFileName(audioFile.Name); var newWatchItem = new WatchFolderItem { DownloadId = audioFile.Name + "_" + audioFile.LastWriteTimeUtc.Ticks, Title = title, OutputPath = new OsPath(audioFile.FullName), Status = DownloadItemStatus.Completed, RemainingTime = TimeSpan.Zero }; var oldWatchItem = lastWatchItems.GetValueOrDefault(newWatchItem.DownloadId); if (PreCheckWatchItemExpiry(newWatchItem, oldWatchItem)) { newWatchItem.TotalSize = audioFile.Length; newWatchItem.Hash = GetHash(audioFile.FullName); if (_diskProvider.IsFileLocked(audioFile.FullName)) { newWatchItem.Status = DownloadItemStatus.Downloading; } UpdateWatchItemExpiry(newWatchItem, oldWatchItem, waitPeriod); } yield return(newWatchItem); } }