This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTasks.cs
554 lines (508 loc) · 19.9 KB
/
Tasks.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Mygod.Xml.Linq;
namespace Mygod.Skylark
{
public static class TaskType
{
public const string NoTask = "ready",
OfflineDownloadTask = "offline-download",
CompressTask = "compress",
DecompressTask = "decompress",
FtpUploadTask = "ftp-upload",
ConvertTask = "convert",
CrossAppCopyTask = "cross-app-copy",
UploadTask = "upload",
BatchMergeVATask = "batch-merge-va";
}
public enum TaskStatus
{
Terminated, Error, Starting, Working, Done
}
public abstract partial class CloudTask
{
protected CloudTask(string filePath)
{
if (!String.IsNullOrEmpty(FilePath = filePath) && File.Exists(filePath))
TaskXml = XHelper.Load(filePath).Root;
}
protected CloudTask(string filePath, string root)
{
FilePath = filePath;
TaskXml = new XElement(root);
}
protected static readonly Regex
AccountRemover = new Regex(@"^ftp:\/\/[^\/]*?:[^\/]*?@", RegexOptions.Compiled);
protected readonly XElement TaskXml;
protected readonly string FilePath;
public int PID
{
get { return TaskXml == null ? 0 : TaskXml.GetAttributeValueWithDefault<int>("pid"); }
set { TaskXml.SetAttributeValue("pid", value); }
}
public string ErrorMessage
{
get { return TaskXml == null ? "任务数据缺失!" : TaskXml.GetAttributeValue("message"); }
set { TaskXml.SetAttributeValue("message", value); }
}
public abstract string Type { get; }
public abstract DateTime? StartTime { get; set; } // leave it for derived classes
public DateTime? EndTime
{
get
{
if (TaskXml == null) return null;
var endTime = TaskXml.GetAttributeValueWithDefault<long>("endTime", -1);
return endTime < 0 ? null
: (DateTime?)new DateTime(TaskXml.GetAttributeValue<long>("endTime"), DateTimeKind.Utc);
}
set
{
if (value.HasValue) TaskXml.SetAttributeValue("endTime", value.Value.Ticks);
else TaskXml.SetAttributeValue("endTime", null);
}
}
public virtual long? FileLength
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue<long?>("size"); }
set
{
if (value.HasValue) TaskXml.SetAttributeValue("size", value.Value);
else TaskXml.SetAttributeValue("size", null);
}
}
public virtual long ProcessedFileLength
{
get { return TaskXml == null ? 0 : TaskXml.GetAttributeValueWithDefault<long>("sizeProcessed"); }
set { TaskXml.SetAttributeValue("sizeProcessed", value); }
}
public virtual double? Percentage { get { return 100.0 * ProcessedFileLength / FileLength; } }
public void Save()
{
lock (TaskXml)
{
TaskXml.Save(FilePath);
}
}
public abstract void Finish();
public static void KillProcessTree(int pid)
{
if (pid == 0) return;
try
{
foreach (var mbo in
new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid).Get())
KillProcessTree(int.Parse(mbo["ProcessID"].ToString()));
}
catch (UnauthorizedAccessException)
{
// TODO: better cancel signal? http://stackoverflow.com/a/15281070
}
try
{
Process.GetProcessById(pid).Kill();
}
catch { }
}
}
public interface IRemoteTask
{
string Url { get; }
}
public interface ISingleSource
{
string Source { get; }
}
public interface IMultipleSources
{
string CurrentSource { get; }
long ProcessedSourceCount { get; }
IEnumerable<string> Sources { get; }
}
public abstract partial class GenerateFileTask : CloudTask
{
protected GenerateFileTask(string relativePath) : base(FileHelper.GetDataFilePath(relativePath))
{
RelativePath = relativePath;
}
protected GenerateFileTask(string relativePath, string state)
: base(FileHelper.GetDataFilePath(relativePath), "file")
{
State = state;
Mime = Helper.GetMimeType(RelativePath = relativePath);
StartTime = DateTime.UtcNow;
File.WriteAllText(FileHelper.GetFilePath(relativePath), string.Empty); // temp
}
public override sealed string Type { get { return State; } }
public override sealed DateTime? StartTime
{
get
{
return TaskXml == null
? null : (DateTime?)new DateTime(TaskXml.GetAttributeValue<long>("startTime"), DateTimeKind.Utc);
}
set
{
if (value.HasValue) TaskXml.SetAttributeValue("startTime", value.Value.Ticks);
else TaskXml.SetAttributeValue("startTime", null);
}
}
public string State
{
get { return TaskXml.GetAttributeValue("state"); }
set { TaskXml.SetAttributeValue("state", value); }
}
public string Mime
{
get { return TaskXml.GetAttributeValue("mime"); }
set { TaskXml.SetAttributeValue("mime", value); }
}
public string RelativePath { get; private set; }
public override void Finish()
{
State = TaskType.NoTask;
Save();
}
}
public abstract class OneToOneFileTask : GenerateFileTask, ISingleSource
{
protected OneToOneFileTask(string relativePath)
: base(relativePath)
{
}
protected OneToOneFileTask(string source, string target, string state)
: base(target, state)
{
Source = source;
}
public string Source
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("source"); }
set { TaskXml.SetAttributeValue("source", value); }
}
}
public abstract partial class MultipleToOneFileTask : GenerateFileTask, IMultipleSources
{
protected MultipleToOneFileTask(string relativePath) : base(relativePath)
{
}
protected MultipleToOneFileTask(IEnumerable<string> sources, string relativePath, string state)
: base(relativePath, state)
{
Sources = sources;
}
public string CurrentSource
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("currentFile"); }
set { TaskXml.SetAttributeValue("currentFile", value); }
}
public long ProcessedSourceCount
{
get { return TaskXml == null ? 0 : TaskXml.GetAttributeValueWithDefault<long>("sourceProcessed"); }
set { TaskXml.SetAttributeValue("sourceProcessed", value); }
}
public IEnumerable<string> Sources
{
get { return TaskXml.ElementsCaseInsensitive("source").Select(file => file.Value); }
set
{
TaskXml.ElementsCaseInsensitive("source").Remove();
foreach (var file in value) TaskXml.Add(new XElement("source", file));
}
}
}
public sealed partial class OfflineDownloadTask : GenerateFileTask, IRemoteTask
{
public OfflineDownloadTask(string relativePath) : base(relativePath)
{
}
public string Url
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("url"); }
set { TaskXml.SetAttributeValue("url", AccountRemover.Replace(value, "ftp://")); }
}
public override long ProcessedFileLength
{
get
{
if (base.ProcessedFileLength > 0) return base.ProcessedFileLength;
var file = new FileInfo(FileHelper.GetFilePath(RelativePath));
return file.Exists ? file.Length : 0;
}
set { throw new NotSupportedException(); }
}
}
public sealed partial class CompressTask : MultipleToOneFileTask
{
public CompressTask(string archiveFilePath) : base(archiveFilePath)
{
}
public CompressTask(string archiveFilePath, IEnumerable<string> files, string baseFolder = null,
string compressionLevel = null) : base(files, archiveFilePath, TaskType.CompressTask)
{
TaskXml.SetAttributeValue("compressionLevel", compressionLevel ?? "Ultra");
BaseFolder = baseFolder ?? string.Empty;
}
public string BaseFolder
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("baseFolder"); }
set { TaskXml.SetAttributeValue("baseFolder", value); }
}
}
public sealed partial class ConvertTask : OneToOneFileTask
{
public ConvertTask(string relativePath) : base(relativePath)
{
}
public ConvertTask(string source, string target, TimeSpan duration, string audioPath = null,
string arguments = null) : base(source, target, TaskType.ConvertTask)
{
Duration = duration;
AudioPath = audioPath;
Arguments = arguments;
}
public TimeSpan ProcessedDuration
{
get { return new TimeSpan(TaskXml.GetAttributeValueWithDefault<long>("durationProcessed")); }
set { TaskXml.SetAttributeValue("durationProcessed", value.Ticks); }
}
public TimeSpan Duration
{
get { return new TimeSpan(TaskXml.GetAttributeValue<long>("duration")); }
set { TaskXml.SetAttributeValue("duration", value.Ticks); }
}
public string AudioPath
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("audioPath"); }
set { TaskXml.SetAttributeValue("audioPath", value); }
}
public string Arguments
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("arguments"); }
set { TaskXml.SetAttributeValue("arguments", value); }
}
public override double? Percentage { get { return 100.0 * ProcessedDuration.Ticks / Duration.Ticks; } }
private static readonly Regex DurationParser = new Regex("Duration: (.*?),", RegexOptions.Compiled);
public static ConvertTask Create(string source, string target, string size = null, string vcodec = null,
string acodec = null, string scodec = null, string audioPath = null,
string startPoint = null, string endPoint = null)
{
var arguments = string.Empty;
if (!string.IsNullOrWhiteSpace(size)) arguments += " -s " + size;
if (!string.IsNullOrWhiteSpace(vcodec)) arguments += " -vcodec " + vcodec;
if (!string.IsNullOrWhiteSpace(acodec)) arguments += " -acodec " + acodec;
if (!string.IsNullOrWhiteSpace(scodec)) arguments += " -scodec " + scodec;
TimeSpan duration;
TimeSpan.TryParse(DurationParser.Match(FFmpeg.Analyze(FileHelper.GetFilePath(source))).Groups[1].Value,
out duration); // silent fail with TimeSpan.Zero
TimeSpan start = FFmpeg.Parse(startPoint), end = FFmpeg.Parse(endPoint, duration);
if (start <= TimeSpan.Zero) start = TimeSpan.Zero;
else arguments += " -ss " + startPoint;
if (end >= duration) end = duration;
else arguments += " -to " + endPoint;
return new ConvertTask(source, target, end - start, audioPath, arguments);
}
}
public abstract partial class GeneralTask : CloudTask
{
protected GeneralTask(string id) : base(FileHelper.GetDataPath(id + ".task"))
{
ID = id;
}
protected GeneralTask(string type, bool create)
: base(FileHelper.GetDataPath(DateTime.UtcNow.Shorten() + ".task"), type)
{
if (!create)
throw new NotSupportedException("You should call .ctor(id) if you are NOT going to create something!");
ID = Path.GetFileNameWithoutExtension(FilePath);
}
public string ID { get; private set; }
public override sealed DateTime? StartTime
{
get { return Helper.Deshorten(ID); }
set { throw new NotSupportedException(); }
}
public override sealed string Type { get { return TaskXml.Name.LocalName; } }
public override void Finish()
{
EndTime = DateTime.UtcNow;
Save();
}
}
public abstract partial class MultipleFilesTask : GeneralTask
{
protected MultipleFilesTask(string id) : base(id)
{
}
protected MultipleFilesTask(string type, string target) : base(type, true)
{
Target = target;
}
public virtual string CurrentFile
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("currentFile"); }
set { TaskXml.SetAttributeValue("currentFile", value); }
}
public virtual long? FileCount
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue<long?>("fileCount"); }
set
{
if (value.HasValue) TaskXml.SetAttributeValue("fileCount", value.Value);
else TaskXml.SetAttributeValue("fileCount", null);
}
}
public virtual long ProcessedFileCount
{
get { return TaskXml == null ? 0 : TaskXml.GetAttributeValueWithDefault<long>("fileProcessed"); }
set { TaskXml.SetAttributeValue("fileProcessed", value); }
}
public string Target
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("target"); }
set { TaskXml.SetAttributeValue("target", value); }
}
}
public abstract partial class OneToMultipleFilesTask : MultipleFilesTask, ISingleSource
{
protected OneToMultipleFilesTask(string id) : base(id)
{
}
protected OneToMultipleFilesTask(string type, string source, string target) : base(type, target)
{
Source = source;
}
public string Source
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("source"); }
set { TaskXml.SetAttributeValue("source", value); }
}
}
public abstract class MultipleToMultipleFilesTask : MultipleFilesTask, IMultipleSources
{
protected MultipleToMultipleFilesTask(string id) : base(id)
{
}
protected MultipleToMultipleFilesTask(string type, IEnumerable<string> sources, string target)
: base(type, target)
{
Sources = sources;
}
public string CurrentSource
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("currentFile"); }
set { TaskXml.SetAttributeValue("currentFile", value); }
}
public long ProcessedSourceCount
{
get { return TaskXml == null ? 0 : TaskXml.GetAttributeValueWithDefault<long>("sourceProcessed"); }
set { TaskXml.SetAttributeValue("sourceProcessed", value); }
}
public IEnumerable<string> Sources
{
get { return TaskXml.ElementsCaseInsensitive("source").Select(file => file.Value); }
set
{
TaskXml.ElementsCaseInsensitive("source").Remove();
foreach (var file in value) TaskXml.Add(new XElement("source", file));
}
}
}
public sealed partial class FtpUploadTask : GeneralTask, IRemoteTask, IMultipleSources
{
public FtpUploadTask(string id) : base(id)
{
}
public FtpUploadTask(string baseFolder, IEnumerable<string> sources, string url)
: base(TaskType.FtpUploadTask, true)
{
BaseFolder = baseFolder ?? string.Empty;
Sources = sources;
Url = url;
}
public string Url
{
get { return TaskXml == null ? null : AccountRemover.Replace(TaskXml.GetAttributeValue("url"), "ftp://"); }
set { TaskXml.SetAttributeValue("url", value); }
}
internal string UrlFull { get { return TaskXml == null ? null : TaskXml.GetAttributeValue("url"); } }
public string BaseFolder
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("baseFolder"); }
set { TaskXml.SetAttributeValue("baseFolder", value); }
}
public string CurrentSource
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("currentFile"); }
set { TaskXml.SetAttributeValue("currentFile", value); }
}
public long? SourceCount
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue<long?>("sourceCount"); }
set
{
if (value.HasValue) TaskXml.SetAttributeValue("sourceCount", value.Value);
else TaskXml.SetAttributeValue("sourceCount", null);
}
}
public long ProcessedSourceCount
{
get { return TaskXml == null ? 0 : TaskXml.GetAttributeValueWithDefault<long>("sourceProcessed"); }
set { TaskXml.SetAttributeValue("sourceProcessed", value); }
}
public IEnumerable<string> Sources
{
get { return TaskXml.ElementsCaseInsensitive("source").Select(file => file.Value); }
set
{
TaskXml.ElementsCaseInsensitive("source").Remove();
foreach (var file in value) TaskXml.Add(new XElement("source", file));
}
}
}
public sealed partial class CrossAppCopyTask : MultipleFilesTask
{
public CrossAppCopyTask(string id) : base(id)
{
}
public CrossAppCopyTask(string domain, string source, string target, string password = null)
: base(TaskType.CrossAppCopyTask, target)
{
Domain = domain;
Source = source;
Password = password;
}
public string Domain
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("domain"); }
set { TaskXml.SetAttributeValue("domain", value); }
}
public string Source
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("source"); }
set { TaskXml.SetAttributeValue("source", value); }
}
public string Password
{
get { return TaskXml == null ? null : TaskXml.GetAttributeValue("password"); }
set { TaskXml.SetAttributeValue("password", value); }
}
public override long? FileCount { get { return null; } set { throw new NotSupportedException(); } }
public override long? FileLength { get { return null; } set { throw new NotSupportedException(); } }
}
public sealed partial class DecompressTask : OneToMultipleFilesTask
{
public DecompressTask(string id) : base(id)
{
}
public DecompressTask(string source, string target) : base(TaskType.DecompressTask, source, target)
{
}
}
}