2016-07-15 08:31:50 -07:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
2016-11-08 15:14:47 -08:00
|
|
|
|
using System;
|
2016-07-15 08:31:50 -07:00
|
|
|
|
using System.IO;
|
2016-06-27 18:26:57 -07:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using Microsoft.Build.Framework;
|
|
|
|
|
using Microsoft.Build.Utilities;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Build
|
|
|
|
|
{
|
|
|
|
|
public class DownloadFile : Task
|
|
|
|
|
{
|
|
|
|
|
[Required]
|
|
|
|
|
public string Uri { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string DestinationPath { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool Overwrite { get; set; }
|
|
|
|
|
|
|
|
|
|
public override bool Execute()
|
|
|
|
|
{
|
|
|
|
|
FS.Mkdirp(Path.GetDirectoryName(DestinationPath));
|
|
|
|
|
|
|
|
|
|
if (File.Exists(DestinationPath) && !Overwrite)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 17:21:37 -08:00
|
|
|
|
const string FileUriProtocol = "file://";
|
2016-06-29 02:41:38 -05:00
|
|
|
|
|
2017-02-14 17:21:37 -08:00
|
|
|
|
if (Uri.StartsWith(FileUriProtocol, StringComparison.Ordinal))
|
2016-06-27 18:26:57 -07:00
|
|
|
|
{
|
2017-02-14 17:21:37 -08:00
|
|
|
|
var filePath = Uri.Substring(FileUriProtocol.Length);
|
|
|
|
|
Log.LogMessage($"Copying '{filePath}' to '{DestinationPath}'");
|
|
|
|
|
File.Copy(filePath, DestinationPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.LogMessage($"Downloading '{Uri}' to '{DestinationPath}'");
|
2016-06-27 18:26:57 -07:00
|
|
|
|
|
2017-02-14 17:21:37 -08:00
|
|
|
|
using (var httpClient = new HttpClient())
|
2016-06-27 18:26:57 -07:00
|
|
|
|
{
|
2017-02-14 17:21:37 -08:00
|
|
|
|
var getTask = httpClient.GetStreamAsync(Uri);
|
|
|
|
|
|
|
|
|
|
try
|
2016-11-08 15:14:47 -08:00
|
|
|
|
{
|
2017-02-14 17:21:37 -08:00
|
|
|
|
using (var outStream = File.Create(DestinationPath))
|
|
|
|
|
{
|
|
|
|
|
getTask.Result.CopyTo(outStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
File.Delete(DestinationPath);
|
|
|
|
|
throw;
|
2016-11-08 15:14:47 -08:00
|
|
|
|
}
|
2016-06-27 18:26:57 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|