-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (68 loc) · 1.83 KB
/
Copy pathProgram.cs
File metadata and controls
78 lines (68 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace FileDownloader
{
class Program
{
static void Main(string[] args)
{
var rawInput = File.ReadAllLines(args[0]);
var outFolder = Path.GetFullPath(args[1]);
ServicePointManager.ServerCertificateValidationCallback += delegate {return true; };
if (!Directory.Exists(outFolder))
{
Directory.CreateDirectory(outFolder);
}
Console.WriteLine("Writing to " + outFolder);
using (var client = new HttpClient())
{
foreach (var s in rawInput)
{
try
{
Go(s, outFolder, client).Wait();
}
catch (Exception ex)
{
Console.WriteLine("Failed to read file from {0}, {1}", s, ex.GetBaseException().Message);
}
}
}
Console.WriteLine("Done...");
Console.ReadKey(true);
}
static public async Task Go(string s, string outFolder, HttpClient client)
{
var uri = new Uri(s);
var fileName = Path.GetFileName(uri.AbsolutePath);
var outputFile = Path.Combine(outFolder, fileName);
var tempFile = Path.Combine(outFolder, fileName) + ".tmp";
if (File.Exists(outputFile))
{
Console.WriteLine("Skipping {0}", s);
return;
}
using (var request = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead))
{
request.EnsureSuccessStatusCode();
using (var fileStream = File.Create(tempFile))
using (var httpStream = await request.Content.ReadAsStreamAsync())
{
await httpStream.CopyToAsync(fileStream);
fileStream.Flush(true);
}
File.Move(tempFile, outputFile);
}
Console.WriteLine("Saved {0}", s);
}
}
}