Skip to content

Commit 436c7fc

Browse files
committed
拆分调用方法
1 parent 40a1159 commit 436c7fc

1 file changed

Lines changed: 117 additions & 63 deletions

File tree

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/EnhancedGraphicsMetafileOptimization.cs

Lines changed: 117 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -51,44 +51,23 @@ public static ImageFileOptimizationResult ConvertWmfOrEmfToPngFile(ImageFileOpti
5151
private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationContext context)
5252
{
5353
var file = context.ImageFile;
54-
var workingFolder = context.WorkingFolder;
5554

5655
// 在 Linux 上,先尝试使用 Inkscape 进行转换,如失败,再使用 libwmf 进行转换
5756
// 调用 Inkscape 进行转换
58-
var svgFile = Path.Join(workingFolder.FullName, $"{Path.GetFileNameWithoutExtension(file.Name)}_{Path.GetRandomFileName()}.svg");
59-
60-
context.LogMessage($"Start convert emf or wmf to png by Inkscape. File='{file}'");
57+
ImageFileOptimizationResult result = ConvertWithInkscape(context);
58+
if (result.OptimizedImageFile is { } svgFile)
6159
{
62-
var processStartInfo = new ProcessStartInfo("inkscape")
63-
{
64-
ArgumentList =
65-
{
66-
"--export-plain-svg",
67-
$"--export-filename={svgFile}",
68-
file.FullName,
69-
}
70-
};
71-
try
72-
{
73-
using var process = Process.Start(processStartInfo);
74-
process?.WaitForExit(TimeSpan.FromSeconds(5));
75-
if (process?.ExitCode == 0 && File.Exists(svgFile))
76-
{
77-
// 转换成功,再次执行 SVG 转 PNG 的转换
78-
return ConvertSvgToPngFile(new FileInfo(svgFile));
79-
}
80-
}
81-
catch (Exception e)
82-
{
83-
// 失败了,继续调用 libwmf 进行转换
84-
context.LogMessage($"Convert emf or wmf to svg by Inkscape failed. We will continue use libwmf to convert the image. File='{file}' Exception: {e}");
85-
}
60+
return ConvertSvgToPngFile(svgFile);
61+
}
62+
else
63+
{
64+
// 失败了,没关系,继续使用 libwmf 进行转换
8665
}
8766

8867
// 继续执行 libwmf 的转换,此时不支持 emf 格式
8968
if (string.Equals(file.Extension, ".emf"))
9069
{
91-
context.LogMessage($"Convert emf to png is not supported with libwmf. File='{file}'");
70+
context.LogMessage($"Convert emf to png is not supported with libwmf. File='{context.ImageFile.FullName}'");
9271

9372
return new ImageFileOptimizationResult()
9473
{
@@ -99,43 +78,15 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
9978

10079
// 使用 libwmf 进行转换
10180

81+
result = ConvertWithInkscapeLibWmf(context);
82+
if (result.OptimizedImageFile is { } svgLibWmfFile)
10283
{
103-
// ./wmf2svg -o 1.svg image.wmf
104-
var processStartInfo = new ProcessStartInfo("wmf2svg")
105-
{
106-
ArgumentList =
107-
{
108-
"-o",
109-
svgFile,
110-
file.FullName,
111-
}
112-
};
113-
114-
var fontFolder = Path.Join(AppContext.BaseDirectory, "fonts");
115-
if (Directory.Exists(fontFolder))
116-
{
117-
processStartInfo.ArgumentList.Add($"--wmf-fontdir={fontFolder}");
118-
}
119-
120-
using var process = Process.Start(processStartInfo);
121-
process?.WaitForExit(TimeSpan.FromSeconds(5));
122-
if (process?.ExitCode == 0 && File.Exists(svgFile))
123-
{
124-
// 转换成功,再次执行 SVG 转 PNG 的转换
125-
var convertedFile = ImageFileOptimization.FixSvgInvalidCharacter(context with
126-
{
127-
ImageFile = new FileInfo(svgFile)
128-
});
129-
130-
return ConvertSvgToPngFile(convertedFile);
131-
}
84+
return ConvertSvgToPngFile(svgLibWmfFile);
13285
}
133-
134-
return new ImageFileOptimizationResult()
86+
else
13587
{
136-
OptimizedImageFile = null,
137-
FailureReason = ImageFileOptimizationFailureReason.NotSupported
138-
};
88+
return result;
89+
}
13990

14091
ImageFileOptimizationResult ConvertSvgToPngFile(FileInfo svgImageFile)
14192
{
@@ -175,6 +126,109 @@ ImageFileOptimizationResult ConvertSvgToPngFile(FileInfo svgImageFile)
175126
}
176127
}
177128

129+
[SupportedOSPlatform("linux")]
130+
private static ImageFileOptimizationResult ConvertWithInkscapeLibWmf(ImageFileOptimizationContext context)
131+
{
132+
var file = context.ImageFile;
133+
var workingFolder = context.WorkingFolder;
134+
135+
var svgFile = Path.Join(workingFolder.FullName, $"{Path.GetFileNameWithoutExtension(file.Name)}_{Path.GetRandomFileName()}.svg");
136+
137+
// ./wmf2svg -o 1.svg image.wmf
138+
var processStartInfo = new ProcessStartInfo("wmf2svg")
139+
{
140+
ArgumentList =
141+
{
142+
"-o",
143+
svgFile,
144+
file.FullName,
145+
}
146+
};
147+
148+
var fontFolder = Path.Join(AppContext.BaseDirectory, "fonts");
149+
if (Directory.Exists(fontFolder))
150+
{
151+
processStartInfo.ArgumentList.Add($"--wmf-fontdir={fontFolder}");
152+
}
153+
154+
using var process = Process.Start(processStartInfo);
155+
process?.WaitForExit(TimeSpan.FromSeconds(5));
156+
if (process?.ExitCode == 0 && File.Exists(svgFile))
157+
{
158+
// 转换成功,再次执行 SVG 转 PNG 的转换
159+
var convertedFile = ImageFileOptimization.FixSvgInvalidCharacter(context with
160+
{
161+
ImageFile = new FileInfo(svgFile)
162+
});
163+
164+
return new ImageFileOptimizationResult()
165+
{
166+
OptimizedImageFile = convertedFile
167+
};
168+
}
169+
170+
return new ImageFileOptimizationResult()
171+
{
172+
OptimizedImageFile = null,
173+
FailureReason = ImageFileOptimizationFailureReason.NotSupported,
174+
};
175+
}
176+
177+
[SupportedOSPlatform("linux")]
178+
private static ImageFileOptimizationResult ConvertWithInkscape(ImageFileOptimizationContext context)
179+
{
180+
var file = context.ImageFile;
181+
var workingFolder = context.WorkingFolder;
182+
183+
var svgFile = Path.Join(workingFolder.FullName, $"{Path.GetFileNameWithoutExtension(file.Name)}_{Path.GetRandomFileName()}.svg");
184+
185+
context.LogMessage($"Start convert emf or wmf to png by Inkscape. File='{file}'");
186+
187+
var processStartInfo = new ProcessStartInfo("inkscape")
188+
{
189+
ArgumentList =
190+
{
191+
"--export-plain-svg",
192+
$"--export-filename={svgFile}",
193+
file.FullName,
194+
}
195+
};
196+
try
197+
{
198+
using var process = Process.Start(processStartInfo);
199+
process?.WaitForExit(TimeSpan.FromSeconds(5));
200+
if (process?.ExitCode == 0 && File.Exists(svgFile))
201+
{
202+
// 转换成功,再次执行 SVG 转 PNG 的转换
203+
return new ImageFileOptimizationResult()
204+
{
205+
OptimizedImageFile = new FileInfo(svgFile)
206+
};
207+
}
208+
else
209+
{
210+
context.LogMessage($"Convert emf or wmf to svg by Inkscape failed. File='{file}' ExitCode={process?.ExitCode}");
211+
}
212+
}
213+
catch (Exception e)
214+
{
215+
// 失败了,继续调用 libwmf 进行转换
216+
context.LogMessage($"Convert emf or wmf to svg by Inkscape failed. We will continue use libwmf to convert the image. File='{file}' Exception: {e}");
217+
return new ImageFileOptimizationResult()
218+
{
219+
OptimizedImageFile = null,
220+
Exception = e,
221+
FailureReason = ImageFileOptimizationFailureReason.NotSupported,
222+
};
223+
}
224+
225+
return new ImageFileOptimizationResult()
226+
{
227+
OptimizedImageFile = null,
228+
FailureReason = ImageFileOptimizationFailureReason.NotSupported,
229+
};
230+
}
231+
178232
[SupportedOSPlatform("windows6.1")]
179233
private static ImageFileOptimizationResult ConvertInWindows(ImageFileOptimizationContext context)
180234
{

0 commit comments

Comments
 (0)