-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileDownloader.cs
148 lines (134 loc) · 4.63 KB
/
FileDownloader.cs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using Common.Logging;
namespace TranscodeProcessor
{
/// <summary>
/// Handle file download/ffprobe for files provided
/// </summary>
public class FileDownload : Sprite.ISimpleRunnable
{
private static ILog log = LogManager.GetCurrentClassLogger();
public FileDownload(string token, string url, string workingDir, string dest)
{
this.dest = dest;
this.id = token;
this.url = url;
this.workingDir = workingDir;
this.complete = false;
this.failed = false;
this.probeInfo = null;
}
public FileDownload(string token, string workingDir, string dest)
{
this.dest = dest;
this.id = token;
this.workingDir = workingDir;
this.downloaded = true;
this.probeInfo = null;
this.failed = false;
}
protected bool downloaded = false;
protected string id;
protected string dest;
protected string url;
protected bool complete;
protected string workingDir;
protected bool failed;
protected ProbeInfo probeInfo;
public void Download()
{
try
{
if (downloaded)
{
if (this.probeInfo == null)
{
var data = FFmpeg.ExecProbe(buildProbeArguments(this.workingDir + dest));
if (string.IsNullOrEmpty(data))
{
this.complete = true;
this.failed = true;
log.Debug("Failed to probe information from video file");
throw new JobFailureException("Failed to probe information from video file");
}
var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric));
this.probeInfo = serializer.Deserialize<ProbeInfo>(data, false);
}
this.complete = true;
}
else
{
//Download the file
System.Net.WebClient wc = new System.Net.WebClient();
wc.DownloadFile(url, this.workingDir + dest);
if (this.probeInfo == null)
{
var data = FFmpeg.ExecProbe(buildProbeArguments(this.workingDir + dest));
if (string.IsNullOrEmpty(data))
{
this.complete = true;
this.failed = true;
log.Debug("Failed to probe information from video file");
throw new JobFailureException("Failed to probe information from video file");
}
var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric));
this.probeInfo = serializer.Deserialize<ProbeInfo>(data, false);
}
this.downloaded = true;
this.complete = true;
}
}
catch (Exception ex)
{
log.Error("An error occured Download()", ex);
}
}
public string WorkingDirectory
{
get { return this.workingDir; }
}
public ProbeInfo ProbeInfo
{
get { return this.probeInfo; }
}
public bool IsComplete
{
get { return this.complete; }
}
internal bool IsFailed
{
get { return this.failed; }
}
public string Destination
{
get
{
return this.dest;
}
}
private string buildProbeArguments(string filename)
{
string command = "-i " + filename + " -v quiet -print_format json -show_format -show_streams";
return command;
}
public override string ToString()
{
return "Token: " + id + " Dest: " + dest + " URL: " + url + " Complete: " + complete;
}
public object Handle()
{
Download();
return this;
}
public object Handle(object resource)
{
throw new NotImplementedException();
}
}
}