protected virtual async Task <bool> AddLibraryItem(LibraryWriter writer, string fileName) { try { if (!this.PlaybackManager.IsSupported(fileName)) { Logger.Write(this, LogLevel.Debug, "File is not supported: {0}", fileName); return(false); } Logger.Write(this, LogLevel.Trace, "Adding file to library: {0}", fileName); var libraryItem = new LibraryItem() { DirectoryName = Path.GetDirectoryName(fileName), FileName = fileName, Status = LibraryItemStatus.Import }; libraryItem.SetImportDate(DateTime.UtcNow); await writer.Write(libraryItem).ConfigureAwait(false); return(true); } catch (Exception e) { Logger.Write(this, LogLevel.Debug, "Failed to add file \"{0}\" to library: {0}", fileName, e.Message); return(false); } }
public async Task Populate(IEnumerable <string> paths, CancellationToken cancellationToken) { if (this.ReportProgress) { await this.SetName("Populating library").ConfigureAwait(false); await this.SetPosition(0).ConfigureAwait(false); this.Timer.Interval = FAST_INTERVAL; this.Timer.Start(); } using (var writer = new LibraryWriter(this.Database, this.Transaction)) { foreach (var path in paths) { if (Directory.Exists(path)) { foreach (var fileName in FileSystemHelper.EnumerateFiles(path, "*.*", FileSystemHelper.SearchOption.Recursive)) { if (cancellationToken.IsCancellationRequested) { return; } var success = await this.AddLibraryItem(writer, fileName).ConfigureAwait(false); if (success && this.ReportProgress) { this.Current = fileName; Interlocked.Increment(ref this.position); } } } else if (File.Exists(path)) { var success = await this.AddLibraryItem(writer, path).ConfigureAwait(false); if (success && this.ReportProgress) { this.Current = path; Interlocked.Increment(ref this.position); } } } } }