122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
|
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace BetterEN5.Services.Conversion
|
||
|
|
{
|
||
|
|
public class ConversionEngine
|
||
|
|
{
|
||
|
|
private readonly PptxImporter _pptxImporter = new();
|
||
|
|
private readonly PptxExporter _pptxExporter = new();
|
||
|
|
private readonly EnbxWriter _enbxWriter = new();
|
||
|
|
private readonly EnbxReader _enbxReader = new();
|
||
|
|
|
||
|
|
public async Task<string> ConvertPptxToEnbxAsync(string pptxPath, IProgress<ConversionProgress>? progress = null)
|
||
|
|
{
|
||
|
|
if (!File.Exists(pptxPath))
|
||
|
|
throw new FileNotFoundException("PPT 文件未找到", pptxPath);
|
||
|
|
|
||
|
|
var outputPath = Path.ChangeExtension(pptxPath, ".enbx");
|
||
|
|
return await Task.Run(() =>
|
||
|
|
{
|
||
|
|
progress?.Report(new ConversionProgress(0, "读取 PPTX..."));
|
||
|
|
|
||
|
|
var doc = _pptxImporter.Import(pptxPath);
|
||
|
|
|
||
|
|
progress?.Report(new ConversionProgress(50, "写入 ENBX..."));
|
||
|
|
|
||
|
|
_enbxWriter.Export(doc, outputPath);
|
||
|
|
|
||
|
|
progress?.Report(new ConversionProgress(100, "转换完成"));
|
||
|
|
|
||
|
|
return outputPath;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string> ConvertEnbxToPptxAsync(string enbxPath, IProgress<ConversionProgress>? progress = null)
|
||
|
|
{
|
||
|
|
if (!File.Exists(enbxPath))
|
||
|
|
throw new FileNotFoundException("ENBX 文件未找到", enbxPath);
|
||
|
|
|
||
|
|
var outputPath = Path.ChangeExtension(enbxPath, ".pptx");
|
||
|
|
return await Task.Run(() =>
|
||
|
|
{
|
||
|
|
progress?.Report(new ConversionProgress(0, "读取 ENBX..."));
|
||
|
|
|
||
|
|
var doc = _enbxReader.Import(enbxPath);
|
||
|
|
|
||
|
|
progress?.Report(new ConversionProgress(50, "写入 PPTX..."));
|
||
|
|
|
||
|
|
_pptxExporter.Export(doc, outputPath);
|
||
|
|
|
||
|
|
progress?.Report(new ConversionProgress(100, "转换完成"));
|
||
|
|
|
||
|
|
return outputPath;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<SmfDocument> LoadPptxAsync(string pptxPath)
|
||
|
|
{
|
||
|
|
return await Task.Run(() => _pptxImporter.Import(pptxPath));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<SmfDocument> LoadEnbxAsync(string enbxPath)
|
||
|
|
{
|
||
|
|
return await Task.Run(() => _enbxReader.Import(enbxPath));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task SaveAsPptxAsync(SmfDocument doc, string outputPath)
|
||
|
|
{
|
||
|
|
await Task.Run(() => _pptxExporter.Export(doc, outputPath));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task SaveAsEnbxAsync(SmfDocument doc, string outputPath)
|
||
|
|
{
|
||
|
|
await Task.Run(() => _enbxWriter.Export(doc, outputPath));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task BatchConvertAsync(string[] inputPaths, string outputDir, bool toEnbx, IProgress<ConversionProgress>? progress = null)
|
||
|
|
{
|
||
|
|
int total = inputPaths.Length;
|
||
|
|
for (int i = 0; i < total; i++)
|
||
|
|
{
|
||
|
|
var input = inputPaths[i];
|
||
|
|
var fileName = Path.GetFileNameWithoutExtension(input);
|
||
|
|
var ext = toEnbx ? ".enbx" : ".pptx";
|
||
|
|
var output = Path.Combine(outputDir, fileName + ext);
|
||
|
|
|
||
|
|
if (toEnbx)
|
||
|
|
await ConvertPptxToEnbxAsync(input, progress);
|
||
|
|
else
|
||
|
|
await ConvertEnbxToPptxAsync(input, progress);
|
||
|
|
|
||
|
|
progress?.Report(new ConversionProgress((i + 1) * 100 / total, $"({i + 1}/{total}) 完成"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void FixConversionErrors()
|
||
|
|
{
|
||
|
|
var en5Path = Path.Combine(
|
||
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||
|
|
"Seewo", "EasiNote5");
|
||
|
|
var tempDir = Path.Combine(en5Path, "Temp", "Conversion");
|
||
|
|
if (Directory.Exists(tempDir))
|
||
|
|
{
|
||
|
|
try { Directory.Delete(tempDir, true); } catch { }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class ConversionProgress
|
||
|
|
{
|
||
|
|
public int Percentage { get; }
|
||
|
|
public string Message { get; }
|
||
|
|
|
||
|
|
public ConversionProgress(int percentage, string message)
|
||
|
|
{
|
||
|
|
Percentage = percentage;
|
||
|
|
Message = message;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|