protected override void ProcessRecord() { var taggedAudioFile = new TaggedAudioFile(AudioFile); var substituter = new MetadataSubstituter(taggedAudioFile.Metadata); DirectoryInfo outputDirectory; try { outputDirectory = new DirectoryInfo( this.GetFileSystemPaths(substituter.Substitute(Path), substituter.Substitute(LiteralPath)).First()); } catch (ItemNotFoundException e) { outputDirectory = new DirectoryInfo(e.ItemName); } if (ShouldProcess(AudioFile.FileInfo.FullName)) { taggedAudioFile.Metadata.CoverArt?.Export(outputDirectory, substituter.Substitute(Name), Replace); } if (PassThru) { WriteObject(taggedAudioFile); } }
protected override void ProcessRecord() { var taggedAudioFile = new TaggedAudioFile(AudioFile); var substituter = new MetadataSubstituter(taggedAudioFile.Metadata); string outputDirectory; try { outputDirectory = this.GetFileSystemPaths(substituter.Substitute(Path), substituter.Substitute(LiteralPath)).First(); } catch (ItemNotFoundException e) { outputDirectory = e.ItemName; } if (!ShouldProcess(AudioFile.FileInfo.FullName)) { return; } Directory.CreateDirectory(outputDirectory); WriteObject(new TaggedAudioFile(taggedAudioFile.FileInfo.CopyTo(System.IO.Path.Combine(outputDirectory, substituter.Substitute(Name == null ? System.IO.Path.GetFileNameWithoutExtension(taggedAudioFile.FileInfo.Name) : Name) + taggedAudioFile.FileInfo.Extension), Replace))); }
protected override void EndProcessing() { if (_audioFiles.Count > 0) { var completed = 0; using (var outputQueue = new BlockingCollection <object>()) { outputQueue.Add(new ProgressRecord(0, string.Format(CultureInfo.CurrentCulture, Resources.ExportAudioFileCommandActivityMessage, Encoder), string.Format(CultureInfo.CurrentCulture, Resources.ExportAudioFileCommandStatusMessage, 0, _audioFiles.Count)) { PercentComplete = 0 }); Task.Run(() => Parallel.ForEach(_audioFiles, new ParallelOptions { CancellationToken = _cancelSource.Token }, audioFile => { try { var substituter = new MetadataSubstituter(audioFile.Metadata); DirectoryInfo outputDirectory; try { outputDirectory = new DirectoryInfo(this.GetFileSystemPaths(substituter.Substitute(Path), substituter.Substitute(LiteralPath)).First()); } catch (ItemNotFoundException e) { outputDirectory = new DirectoryInfo(e.ItemName); } ExportableAudioFile result = audioFile.Export(Encoder, _cancelSource.Token, new HashTableToSettingsDictionaryAdapter(Setting), outputDirectory, string.IsNullOrEmpty(Name) ? null : substituter.Substitute(Name), Replace); Interlocked.Increment(ref completed); outputQueue.Add(result); outputQueue.Add(new ProgressRecord(0, string.Format(CultureInfo.CurrentCulture, Resources.ExportAudioFileCommandActivityMessage, Encoder), string.Format(CultureInfo.CurrentCulture, Resources.ExportAudioFileCommandStatusMessage, completed, _audioFiles.Count)) { PercentComplete = completed.GetPercent(_audioFiles.Count) }); } catch (Exception e) { Interlocked.Increment(ref completed); outputQueue.Add(new ErrorRecord(e, e.HResult.ToString(CultureInfo.CurrentCulture), ErrorCategory.WriteError, audioFile)); } })).ContinueWith(task => outputQueue.CompleteAdding()); // Process output on the main thread: this.ProcessOutput(outputQueue, _cancelSource.Token); } } }