Skip to content

Commit 61fd052

Browse files
committed
处理更多进程转换错误
1 parent c7673b6 commit 61fd052

3 files changed

Lines changed: 29 additions & 20 deletions

File tree

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
6363
// 继续执行 libwmf 的转换,此时不支持 emf 格式
6464
if (string.Equals(file.Extension, ".emf"))
6565
{
66-
context.LogMessage($"Convert emf to png is not supported with libwmf. File='{context.ImageFile.FullName}'");
66+
context.LogMessage($"Convert emf to png is not supported with libwmf. File:'{context.ImageFile.FullName}'");
6767

6868
return new ImageFileOptimizationResult()
6969
{
@@ -142,21 +142,30 @@ private static ImageFileOptimizationResult ConvertWithInkscapeLibWmf(ImageFileOp
142142
processStartInfo.ArgumentList.Add($"--wmf-fontdir={fontFolder}");
143143
}
144144

145-
using var process = Process.Start(processStartInfo);
146-
process?.WaitForExit(TimeSpan.FromSeconds(5));
147-
if (process?.ExitCode == 0 && File.Exists(svgFile))
145+
try
148146
{
149-
// 转换成功,再次执行 SVG 转 PNG 的转换
150-
// 由于可能存在 SVG 文件中包含无效字符的问题,因此需要修复一下
151-
var convertedFile = ImageFileOptimization.FixSvgInvalidCharacter(context with
147+
using var process = Process.Start(processStartInfo);
148+
process?.WaitForExit(TimeSpan.FromSeconds(5));
149+
if (process?.ExitCode == 0 && File.Exists(svgFile))
152150
{
153-
ImageFile = new FileInfo(svgFile)
154-
});
151+
// 转换成功,再次执行 SVG 转 PNG 的转换
152+
// 由于可能存在 SVG 文件中包含无效字符的问题,因此需要修复一下
153+
var convertedFile = ImageFileOptimization.FixSvgInvalidCharacter(context with
154+
{
155+
ImageFile = new FileInfo(svgFile)
156+
});
155157

156-
return new ImageFileOptimizationResult()
157-
{
158-
OptimizedImageFile = convertedFile
159-
};
158+
return new ImageFileOptimizationResult()
159+
{
160+
OptimizedImageFile = convertedFile
161+
};
162+
}
163+
}
164+
catch (Exception e)
165+
{
166+
// 比如 wmf2svg: error while loading shared libraries: libwmf-0.2.so.7: cannot open shared object file: No such file or directory 等错误
167+
context.LogMessage($"Convert emf or wmf to svg by libwmf failed. File:'{file}' Exception: {e}");
168+
return ImageFileOptimizationResult.FailException(e);
160169
}
161170

162171
return new ImageFileOptimizationResult()
@@ -174,7 +183,7 @@ private static ImageFileOptimizationResult ConvertWithInkscape(ImageFileOptimiza
174183

175184
var svgFile = Path.Join(workingFolder.FullName, $"{Path.GetFileNameWithoutExtension(file.Name)}_{Path.GetRandomFileName()}.svg");
176185

177-
context.LogMessage($"Start convert emf or wmf to png by Inkscape. File='{file}'");
186+
context.LogMessage($"Start convert emf or wmf to png by Inkscape. File:'{file}'");
178187

179188
var processStartInfo = new ProcessStartInfo("inkscape")
180189
{
@@ -199,7 +208,7 @@ private static ImageFileOptimizationResult ConvertWithInkscape(ImageFileOptimiza
199208
}
200209
else
201210
{
202-
context.LogMessage($"Convert emf or wmf to svg by Inkscape failed. File='{file}' ExitCode={process?.ExitCode}");
211+
context.LogMessage($"Convert emf or wmf to svg by Inkscape failed. File:'{file}' ExitCode:{process?.ExitCode}");
203212
}
204213
}
205214
catch (Exception e)
@@ -214,7 +223,7 @@ private static ImageFileOptimizationResult ConvertWithInkscape(ImageFileOptimiza
214223
}
215224
else
216225
{
217-
context.LogMessage($"Convert emf or wmf to svg by Inkscape failed. We will continue use libwmf to convert the image. File='{file}' Exception: {e}");
226+
context.LogMessage($"Convert emf or wmf to svg by Inkscape failed. We will continue use libwmf to convert the image. File:'{file}' Exception: {e}");
218227
}
219228

220229
return ImageFileOptimizationResult.FailException(e);
@@ -233,7 +242,7 @@ private static ImageFileOptimizationResult ConvertInWindows(ImageFileOptimizatio
233242
var file = context.ImageFile;
234243
var workingFolder = context.WorkingFolder;
235244

236-
context.LogMessage($"Start convert emf or wmf to png by GDI. File='{file}'");
245+
context.LogMessage($"Start convert emf or wmf to png by GDI. File:'{file}'");
237246

238247
try
239248
{
@@ -250,7 +259,7 @@ private static ImageFileOptimizationResult ConvertInWindows(ImageFileOptimizatio
250259
}
251260
catch (Exception e)
252261
{
253-
context.LogMessage($"Fail convert emf or wmf to png by GDI. File='{file}' Exception={e}");
262+
context.LogMessage($"Fail convert emf or wmf to png by GDI. File:'{file}' Exception:{e}");
254263

255264
return new ImageFileOptimizationResult
256265
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
3434
var maxImageWidth = context.MaxImageWidth;
3535
var maxImageHeight = context.MaxImageHeight;
3636

37-
context.LogMessage($"Start optimize image file. File='{imageFile}'");
37+
context.LogMessage($"Start optimize image file. File:'{imageFile}'");
3838

3939
if (!File.Exists(imageFile.FullName))
4040
{

src/MediaConverters/MediaConverters.Tool/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ internal static async Task<ErrorCode> RunAsync(Options options)
138138
}
139139

140140
stopwatch.Stop();
141-
context.LogMessage($"Success converted image. Cost {stopwatch.ElapsedMilliseconds}ms. OutputFile='{options.OutputFile}'");
141+
context.LogMessage($"Success converted image. Cost {stopwatch.ElapsedMilliseconds}ms. OutputFile:'{options.OutputFile}'");
142142

143143
return ErrorCode.Success;
144144
}

0 commit comments

Comments
 (0)