-
Notifications
You must be signed in to change notification settings - Fork 18
/
Converter.cs
286 lines (255 loc) · 9.63 KB
/
Converter.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
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static WebOne.Program;
namespace WebOne
{
/// <summary>
/// External file format converter utility
/// </summary>
public class Converter
{
/// <summary>
/// Command or path to executable file of the converter
/// </summary>
public string Executable { get; private set; }
/// <summary>
/// Command line arguments of the converter
/// </summary>
public string CommandLine { get; private set; }
/// <summary>
/// Does the converter returning the result content through STDOUT pipe
/// </summary>
public bool UseStdout { get; private set; }
/// <summary>
/// Does the converter getting the source content through STDIN pipe
/// </summary>
public bool UseStdin { get; private set; }
/// <summary>
/// Does the converter downloading the content itself
/// </summary>
public bool SelfDownload { get; private set; }
/// <summary>
/// Register a converter
/// </summary>
/// <param name="Str">Line from webone.conf [Converters] section</param>
public Converter(string Str)
{
if (Str.IndexOf(" ") < 0) throw new Exception("Converter line is invalid");
//todo: make parsing paying attention to quotes, not simply by space character
Executable = Str.Substring(0, Str.IndexOf(" "));
CommandLine = Str.Substring(Str.IndexOf(" ") + 1);
if (!CommandLine.Contains("%DEST%")) UseStdout = true;
if (!CommandLine.Contains("%SRC%")) UseStdin = true;
SelfDownload = CommandLine.Contains("%SRCURL%");
if (UseStdin && SelfDownload) UseStdin = false;
}
/// <summary>
/// Run a converter (input: Stream; output: Stream)
/// </summary>
/// <param name="Log">Log writer for current convertion</param>
/// <param name="InputStream">Input stream with source content (or <see cref="null"/> if the converter can download itself)</param>
/// <param name="Args1">First set of converter arguments</param>
/// <param name="Args2">Second set of converter arguments</param>
/// <param name="DestinationType">Destination extension</param>
/// <param name="SrcUrl">Source content URL for converters which can download itself (or <see cref="null"/> for others)</param>
/// <returns>Stream with converted content</returns>
public Stream Run(LogWriter Log, Stream InputStream = null, string Args1 = "", string Args2 = "", string DestinationType = "tmp", string SrcUrl = null)
{
if (SelfDownload && (InputStream != null || SrcUrl == null))
throw new InvalidOperationException("The converter " + Executable + " can only download the content self");
int Rnd = new Random().Next();
string SourceTmpFile = ConfigFile.TemporaryDirectory + "convert-" + Rnd + ".orig.tmp";
string DestinationTmpFile = ConfigFile.TemporaryDirectory + "convert-" + Rnd + ".conv." + DestinationType;
if (!UseStdin && !SelfDownload)
{
//the converter input is Temporary File; save it
FileStream TmpFileStream = File.OpenWrite(SourceTmpFile);
InputStream.CopyTo(TmpFileStream);
TmpFileStream.Close();
}
//prepare command line
string ConvCommandLine = ProcessUriMasks(CommandLine, SrcUrl ?? "http://webone.github.io/index.htm")
.Replace("%SRC%", SourceTmpFile)
.Replace("%ARG1%", Args1)
.Replace("%DEST%", DestinationTmpFile)
.Replace("%ARG2%", Args2)
.Replace("%DESTEXT%", DestinationType)
.Replace("%SRCURL%", SrcUrl ?? "http://webone.github.io/index.htm");
//run the converter
return RunConverter(Log, ConvCommandLine, InputStream, SourceTmpFile, DestinationTmpFile);
}
/// <summary>
/// Run (really) the converter and get its result
/// </summary>
/// <param name="Log">Log writer for current convertion</param>
/// <param name="ConvCommandLine">Real command line (excluding executable file name)</param>
/// <param name="InputStream">Input stream with source content (or <see cref="null"/> if the converter can download itself)</param>
/// <param name="SourceTmpFile">Path to source temporary file (even if it doesn't exists)</param>
/// <param name="DestinationTmpFile">Path to destination temporary file (even if it doesn't exists)</param>
/// <returns>Stream with converted content (from stdout or a filestream)</returns>
private Stream RunConverter(LogWriter Log, string ConvCommandLine, Stream InputStream, string SourceTmpFile, string DestinationTmpFile)
{
//run the converter
ProcessStartInfo ConvProcInfo = new ProcessStartInfo();
ConvProcInfo.FileName = Executable;
ConvProcInfo.Arguments = ConvCommandLine;
ConvProcInfo.StandardOutputEncoding = Encoding.GetEncoding("latin1"); //see https://stackoverflow.com/a/5446177/7600726
ConvProcInfo.RedirectStandardOutput = true;
ConvProcInfo.RedirectStandardInput = true;
ConvProcInfo.UseShellExecute = false;
Process ConvProc = Process.Start(ConvProcInfo);
Log.WriteLine(" Converting: {0} {1}...", Executable, ConvCommandLine);
float ConvCpuLoad = 0;
if (UseStdout)
{
//the converter is outputing to stream
if (UseStdin)
{
#if DEBUG
Log.WriteLine(" Writing stdin...");
#endif
new Task(() => { try { InputStream.CopyTo(ConvProc.StandardInput.BaseStream); } catch { } }).Start();
}
#if DEBUG
Log.WriteLine(" Reading stdout...");
#endif
//new Task(() => { while (InputStream.CanRead) { } if (!ConvProc.HasExited) ConvProc.Kill(); Console.WriteLine(); }).Start();
//new Task(() => { while (!ConvProc.HasExited) { if (ClientResponse.StatusCode == 500) { if (!ConvProc.HasExited) { ConvProc.Kill(); } } } }).Start();
//new Task(() => { while (!ConvProc.HasExited) { CheckIdle(ref ConvCpuLoad, ref ConvProc); } }).Start();
//SendStream(ConvProc.StandardOutput.BaseStream, DestMime, false);
new Task(() =>
{
if (InputStream != null) new Task(() =>
{
while (InputStream.CanRead && !ConvProc.HasExited)
{
Thread.Sleep(5000);
if (InputStream.CanRead && !ConvProc.HasExited) Log.WriteLine(" Converter is still running.");
}
if (!ConvProc.HasExited) ConvProc.Kill(); Console.WriteLine();
}).Start();
new Task(() =>
{
while (!ConvProc.HasExited)
{
CheckIdle(ref ConvCpuLoad, ref ConvProc, Log);
}
}).Start();
#if DEBUG
Log.WriteLine(" Waiting for finish of converting...");
#endif
ConvProc.WaitForExit();
if (InputStream != null) InputStream.Close();
#if DEBUG
Log.WriteLine(" Converting end.");
#endif
if (File.Exists(SourceTmpFile)) File.Delete(SourceTmpFile);
if (File.Exists(DestinationTmpFile)) File.Delete(DestinationTmpFile);
#if DEBUG
Log.WriteLine(" Remove temporary files if any.");
#endif
}).Start();
return ConvProc.StandardOutput.BaseStream;
}
else
{
//the converter is outputing to temporary file
if (UseStdin)
{
#if DEBUG
Log.WriteLine(" Writing stdin...");
#endif
new Task(() => { try { InputStream.CopyTo(ConvProc.StandardInput.BaseStream); } catch { } }).Start();
}
#if DEBUG
Log.WriteLine(" Waiting for converter...");
#endif
ConvProc.WaitForExit();
InputStream.Close();
new Task(() =>
{
//wait 1 minute for let client time to download the file
Thread.Sleep(60000);
#if DEBUG
Log.WriteLine(" Remove temporary files.");
#endif
if (File.Exists(SourceTmpFile)) File.Delete(SourceTmpFile);
if (File.Exists(DestinationTmpFile)) File.Delete(DestinationTmpFile);
}).Start();
return File.OpenRead(DestinationTmpFile);
}
}
/// <summary>
/// Get CPU load for process
/// </summary>
/// <param name="process">The process object</param>
/// <returns>CPU usage in percents</returns>
private double GetUsage(Process process)
{
//thx to: https://stackoverflow.com/a/49064915/7600726
//see also https://www.mono-project.com/archived/mono_performance_counters/
if (process.HasExited) return double.MinValue;
// Preparing variable for application instance name
string name = "";
foreach (string instance in new PerformanceCounterCategory("Process").GetInstanceNames())
{
if (process.HasExited) return double.MinValue;
if (instance.StartsWith(process.ProcessName))
{
using (PerformanceCounter processId = new PerformanceCounter("Process", "ID Process", instance, true))
{
try
{
if (process.Id == (int)processId.RawValue)
{
name = instance;
break;
}
}
catch (InvalidOperationException)
{
//System.InvalidOperationException: "Instance 'convert#2' does not exist in the specified Category."
return 0;
}
}
}
}
PerformanceCounter cpu = new PerformanceCounter("Process", "% Processor Time", name, true);
// Getting first initial values
cpu.NextValue();
// Creating delay to get correct values of CPU usage during next query
Thread.Sleep(500);
try
{
if (process.HasExited) return double.MinValue;
return Math.Round(cpu.NextValue() / Environment.ProcessorCount, 2);
}
catch (InvalidOperationException)
{
//System.InvalidOperationException: "Instance 'convert#1' does not exist in the specified Category."
return 0;
}
}
/// <summary>
/// Check process for idle mode and kill it if yes
/// </summary>
/// <param name="AverageLoad">Average process load</param>
/// <param name="Proc">Which process</param>
private void CheckIdle(ref float AverageLoad, ref Process Proc, LogWriter Log)
{
Thread.Sleep(1000);
AverageLoad = (float)(AverageLoad + GetUsage(Proc)) / 2;
if (Math.Round(AverageLoad, 2) <= 0 && !Proc.HasExited)
{
//the process is counting crows. Fire!
Proc.Kill();
Console.WriteLine("\n");
Log.WriteLine(" Idle process killed.");
}
}
}
}