BililiveRecorder/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs

280 lines
10 KiB
C#
Raw Normal View History

2021-02-23 18:03:37 +08:00
using System;
2021-02-27 18:14:44 +08:00
using System.IO;
2021-02-23 18:03:37 +08:00
using System.Threading.Tasks;
using System.Windows;
2021-04-19 18:20:14 +08:00
using BililiveRecorder.ToolBox;
2021-04-14 23:46:24 +08:00
using BililiveRecorder.ToolBox.Commands;
2021-02-27 18:14:44 +08:00
using BililiveRecorder.WPF.Controls;
using Microsoft.WindowsAPICodePack.Dialogs;
using Serilog;
2021-02-23 18:03:37 +08:00
2021-02-27 18:14:44 +08:00
#nullable enable
2021-02-23 18:03:37 +08:00
namespace BililiveRecorder.WPF.Pages
{
/// <summary>
/// Interaction logic for ToolboxAutoFixPage.xaml
/// </summary>
public partial class ToolboxAutoFixPage
{
2021-02-27 18:14:44 +08:00
private static readonly ILogger logger = Log.ForContext<ToolboxAutoFixPage>();
2021-02-23 18:03:37 +08:00
public ToolboxAutoFixPage()
{
2021-02-27 18:14:44 +08:00
this.InitializeComponent();
}
private void SelectFile_Button_Click(object sender, RoutedEventArgs e)
{
// var title = LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:WorkDirectorySelector_Title");
var title = "选择要修复的文件";
var fileDialog = new CommonOpenFileDialog()
{
Title = title,
IsFolderPicker = false,
Multiselect = false,
AllowNonFileSystemItems = false,
AddToMostRecentlyUsedList = false,
EnsurePathExists = true,
EnsureFileExists = true,
NavigateToShortcut = true,
Filters =
{
2021-04-19 18:20:14 +08:00
new CommonFileDialogFilter("Flv files",".flv"),
new CommonFileDialogFilter("Flv Xml files",".xml,.gz")
2021-02-27 18:14:44 +08:00
}
};
if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
this.FileNameTextBox.Text = fileDialog.FileName;
}
}
private async void Fix_Button_Click(object sender, RoutedEventArgs e)
{
AutoFixProgressDialog? progressDialog = null;
try
{
var inputPath = this.FileNameTextBox.Text;
if (string.IsNullOrWhiteSpace(inputPath) || !File.Exists(inputPath))
return;
logger.Debug("修复文件 {Path}", inputPath);
progressDialog = new AutoFixProgressDialog();
var showTask = progressDialog.ShowAsync();
2021-04-14 23:46:24 +08:00
string? output_path;
2021-02-27 18:14:44 +08:00
{
var title = "选择保存位置";
var fileDialog = new CommonSaveFileDialog()
{
Title = title,
AddToMostRecentlyUsedList = false,
EnsurePathExists = true,
EnsureValidNames = true,
NavigateToShortcut = true,
OverwritePrompt = false,
DefaultDirectory = Path.GetDirectoryName(inputPath),
DefaultFileName = Path.GetFileName(inputPath)
};
if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok)
2021-04-14 23:46:24 +08:00
output_path = fileDialog.FileName;
2021-02-27 18:14:44 +08:00
else
return;
}
2021-04-14 23:46:24 +08:00
var req = new FixRequest
2021-02-27 18:14:44 +08:00
{
2021-04-14 23:46:24 +08:00
Input = inputPath,
OutputBase = output_path,
};
2021-02-27 18:14:44 +08:00
2021-04-14 23:46:24 +08:00
var handler = new FixHandler();
2021-02-27 18:14:44 +08:00
2021-04-14 23:46:24 +08:00
var resp = await handler.Handle(req, async p =>
{
await this.Dispatcher.InvokeAsync(() =>
{
progressDialog.Progress = (int)(p * 98d);
});
2021-02-27 18:14:44 +08:00
}).ConfigureAwait(true);
2021-04-19 18:20:14 +08:00
if (resp.Status != ResponseStatus.OK)
{
logger.Warning(resp.Exception, "修复时发生错误 (@Status)", resp.Status);
await Task.Run(() =>
{
// TODO 翻译
// 例:在读取文件时发生了错误
// 选择的不是 FLV 文件
// FLV 文件格式错误
MessageBox.Show($"错误类型: {resp.Status}\n{resp.ErrorMessage}", "修复时发生错误", MessageBoxButton.OK, MessageBoxImage.Warning);
}).ConfigureAwait(true);
}
2021-02-27 18:14:44 +08:00
progressDialog.Hide();
await showTask.ConfigureAwait(true);
}
catch (Exception ex)
{
2021-04-19 18:20:14 +08:00
logger.Error(ex, "修复时发生未处理的错误");
2021-02-27 18:14:44 +08:00
}
finally
{
try
{
_ = this.Dispatcher.BeginInvoke((Action)(() => progressDialog?.Hide()));
}
catch (Exception) { }
}
}
2021-02-27 20:44:04 +08:00
private async void Analyze_Button_Click(object sender, RoutedEventArgs e)
2021-02-27 18:14:44 +08:00
{
AutoFixProgressDialog? progressDialog = null;
try
{
var inputPath = this.FileNameTextBox.Text;
if (string.IsNullOrWhiteSpace(inputPath) || !File.Exists(inputPath))
return;
logger.Debug("分析文件 {Path}", inputPath);
progressDialog = new AutoFixProgressDialog();
var showTask = progressDialog.ShowAsync();
2021-04-14 23:46:24 +08:00
var req = new AnalyzeRequest
2021-02-27 20:44:04 +08:00
{
2021-04-14 23:46:24 +08:00
Input = inputPath
};
2021-02-27 18:14:44 +08:00
2021-04-14 23:46:24 +08:00
var handler = new AnalyzeHandler();
2021-02-27 20:44:04 +08:00
2021-04-14 23:46:24 +08:00
var resp = await handler.Handle(req, async p =>
2021-02-27 20:44:04 +08:00
{
2021-04-14 23:46:24 +08:00
await this.Dispatcher.InvokeAsync(() =>
{
progressDialog.Progress = (int)(p * 98d);
});
}).ConfigureAwait(true);
2021-02-27 20:44:04 +08:00
2021-04-19 18:20:14 +08:00
if (resp.Status != ResponseStatus.OK)
{
logger.Warning(resp.Exception, "分析时发生错误 (@Status)", resp.Status);
await Task.Run(() =>
{
// TODO 翻译
// 例:在读取文件时发生了错误
// 选择的不是 FLV 文件
// FLV 文件格式错误
MessageBox.Show($"错误类型: {resp.Status}\n{resp.ErrorMessage}", "分析时发生错误", MessageBoxButton.OK, MessageBoxImage.Warning);
}).ConfigureAwait(true);
}
else
{
this.analyzeResultDisplayArea.DataContext = resp.Result;
}
2021-02-27 20:44:04 +08:00
progressDialog.Hide();
await showTask.ConfigureAwait(true);
2021-02-27 18:14:44 +08:00
}
catch (Exception ex)
{
2021-04-19 18:20:14 +08:00
logger.Error(ex, "分析时发生未处理的错误");
2021-02-27 18:14:44 +08:00
}
finally
{
try
{
_ = this.Dispatcher.BeginInvoke((Action)(() => progressDialog?.Hide()));
}
catch (Exception) { }
}
}
private async void Export_Button_Click(object sender, RoutedEventArgs e)
{
AutoFixProgressDialog? progressDialog = null;
try
{
var inputPath = this.FileNameTextBox.Text;
if (string.IsNullOrWhiteSpace(inputPath) || !File.Exists(inputPath))
return;
logger.Debug("导出文件 {Path}", inputPath);
progressDialog = new AutoFixProgressDialog();
var showTask = progressDialog.ShowAsync();
var outputPath = string.Empty;
{
var title = "选择保存位置";
var fileDialog = new CommonSaveFileDialog()
{
Title = title,
AddToMostRecentlyUsedList = false,
EnsurePathExists = true,
EnsureValidNames = true,
NavigateToShortcut = true,
DefaultDirectory = Path.GetDirectoryName(inputPath),
DefaultFileName = Path.GetFileNameWithoutExtension(inputPath) + ".brec.xml.gz"
};
if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok)
outputPath = fileDialog.FileName;
else
return;
}
if (File.Exists(outputPath))
{
MessageBox.Show("保存位置已经存在文件");
return;
}
2021-04-14 23:46:24 +08:00
var req = new ExportRequest
2021-02-27 18:14:44 +08:00
{
2021-04-14 23:46:24 +08:00
Input = inputPath,
Output = outputPath
};
2021-02-27 18:14:44 +08:00
2021-04-14 23:46:24 +08:00
var handler = new ExportHandler();
var resp = await handler.Handle(req, async p =>
2021-03-02 23:39:54 +08:00
{
2021-04-14 23:46:24 +08:00
await this.Dispatcher.InvokeAsync(() =>
2021-02-27 18:14:44 +08:00
{
2021-04-14 23:46:24 +08:00
progressDialog.Progress = (int)(p * 95d);
2021-02-27 18:14:44 +08:00
});
2021-03-02 23:39:54 +08:00
}).ConfigureAwait(true);
2021-02-27 18:14:44 +08:00
2021-04-19 18:20:14 +08:00
if (resp.Status != ResponseStatus.OK)
{
logger.Warning(resp.Exception, "导出分析数据时发生错误 (@Status)", resp.Status);
await Task.Run(() =>
{
// TODO 翻译
// 例:在读取文件时发生了错误
// 选择的不是 FLV 文件
// FLV 文件格式错误
MessageBox.Show($"错误类型: {resp.Status}\n{resp.ErrorMessage}", "导出分析数据时发生错误", MessageBoxButton.OK, MessageBoxImage.Warning);
}).ConfigureAwait(true);
}
2021-02-27 18:14:44 +08:00
progressDialog.Hide();
await showTask.ConfigureAwait(true);
}
catch (Exception ex)
{
2021-04-19 18:20:14 +08:00
logger.Error(ex, "导出时发生未处理的错误");
2021-02-27 18:14:44 +08:00
}
finally
{
try
{
_ = this.Dispatcher.BeginInvoke((Action)(() => progressDialog?.Hide()));
}
catch (Exception) { }
}
}
2021-02-23 18:03:37 +08:00
}
}