public static TemporaryFile Download([NotNull] this DownloadRetrievalMethod retrievalMethod, [NotNull] ITaskHandler handler, [CanBeNull] ICommandExecutor executor = null)
{
#region Sanity checks
if (retrievalMethod == null) throw new ArgumentNullException(nameof(retrievalMethod));
if (handler == null) throw new ArgumentNullException(nameof(handler));
#endregion
retrievalMethod.Validate();
if (executor == null) executor = new SimpleCommandExecutor();
new PerTypeDispatcher<DownloadRetrievalMethod>(ignoreMissing: false)
{
// 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(archive.Href.OriginalString);
executor.Execute(new SetValueCommand<string>(() => archive.MimeType, value => archive.MimeType = value, mimeType));
}
},
(SingleFile file) =>
{
// Guess file name based on URL
if (string.IsNullOrEmpty(file.Destination))
{
string destination = file.Href.GetLocalFileName();
executor.Execute(new SetValueCommand<string>(() => file.Destination, value => file.Destination = value, destination));
}
}
// ReSharper restore AccessToDisposedClosure
}.Dispatch(retrievalMethod);
// Download the file
var href = ModelUtils.GetAbsoluteHref(retrievalMethod.Href, executor.Path);
var downloadedFile = new TemporaryFile("0publish");
handler.RunTask(new DownloadFile(href, downloadedFile)); // Defer task to handler
UpdateSize(retrievalMethod, downloadedFile, executor);
return downloadedFile;
}
#endregion