public static TemporaryDirectory LocalApply([NotNull] this DownloadRetrievalMethod retrievalMethod, string localPath, [NotNull] ITaskHandler handler, [CanBeNull] ICommandExecutor executor = null)
{
#region Sanity checks
if (retrievalMethod == null) throw new ArgumentNullException(nameof(retrievalMethod));
if (string.IsNullOrEmpty(localPath)) throw new ArgumentNullException(nameof(localPath));
if (handler == null) throw new ArgumentNullException(nameof(handler));
#endregion
if (executor == null) executor = new SimpleCommandExecutor();
UpdateSize(retrievalMethod, localPath, executor);
var extractionDir = new TemporaryDirectory("0publish");
try
{
new PerTypeDispatcher<DownloadRetrievalMethod>(ignoreMissing: true)
{
// ReSharper disable AccessToDisposedClosure
(Archive archive) =>
{
// Guess MIME types now because the file ending is not known later
if (string.IsNullOrEmpty(archive.MimeType))
{
string mimeType = Archive.GuessMimeType(localPath);
executor.Execute(new SetValueCommand<string>(() => archive.MimeType, value => archive.MimeType = value, mimeType));
}
archive.Apply(localPath, extractionDir, handler);
},
(SingleFile file) =>
{
// Guess file name based on local path
if (string.IsNullOrEmpty(file.Destination))
{
string destination = Path.GetFileName(localPath);
executor.Execute(new SetValueCommand<string>(() => file.Destination, value => file.Destination = value, destination));
}
file.Apply(localPath, extractionDir, handler);
}
// ReSharper restore AccessToDisposedClosure
}.Dispatch(retrievalMethod);
}
#region Error handling
catch
{
extractionDir.Dispose();
throw;
}
#endregion
return extractionDir;
}
#endregion