Skip to content

Commit

Permalink
feat: delete zero length file
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Mar 24, 2023
1 parent ec9849a commit e08da64
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 67 deletions.
15 changes: 11 additions & 4 deletions BilibiliLiveRecordDownLoader/Models/RoomStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private async Task StartRecordAsync(CancellationToken cancellationToken)
{
_logger.LogInformation(@"录制结束");

recorder.WriteToFileTask?.ContinueWith(task => ConvertToMp4Async(task.Result).Forget(), CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current).Forget();
recorder.WriteToFileTask?.ContinueWith(task => ProcessVideoAsync(task.Result).Forget(), CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current).Forget();
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
Expand Down Expand Up @@ -410,16 +410,23 @@ private async Task StartRecordAsync(CancellationToken cancellationToken)
}
}

private async Task ConvertToMp4Async(string file)
private async Task ProcessVideoAsync(string file)
{
try
{
if (!(IsAutoConvertMp4 ?? _config.IsAutoConvertMp4))
FileInfo fileInfo = new(file);
if (!fileInfo.Exists)
{
return;
}

if (fileInfo.Length is 0)
{
FileUtils.DeleteWithoutException(fileInfo.FullName);
return;
}

if (!File.Exists(file))
if (!(IsAutoConvertMp4 ?? _config.IsAutoConvertMp4))
{
return;
}
Expand Down
40 changes: 29 additions & 11 deletions BilibiliLiveRecordDownLoader/Utils/FileUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BilibiliLiveRecordDownLoader.Services;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Windows.Win32;
Expand Down Expand Up @@ -44,34 +45,51 @@ public static (ulong, ulong, ulong) GetDiskUsage(string path)

public static void DeleteWithoutException(string? path)
{
if (path is null || !File.Exists(path))
try
{
return;
if (path is null)
{
return;
}
File.Delete(path);
}
catch (Exception ex)
{
Logger.LogError(ex, $@"删除文件错误:{path}");
}
}

public static bool OpenUrl(string path)
{
try
{
File.Delete(path);
using Process process = new();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = path;
process.Start();
return true;
}
catch (Exception ex)
catch
{
Logger.LogWarning(ex, $@"删除文件错误:{path}");
return false;
}
}

public static void DeleteFilesWithoutException(string dirPath)
public static bool OpenDir(string dir)
{
if (!Directory.Exists(dir))
{
return false;
}

try
{
var di = new DirectoryInfo(dirPath);
if (di.Exists)
{
di.Delete(true);
}
return OpenUrl(dir);
}
catch
{
// ignored
}
return false;
}
}
36 changes: 0 additions & 36 deletions BilibiliLiveRecordDownLoader/Utils/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -36,40 +34,6 @@ public static string ToHumanBytesString(this ulong size)
return ToHumanBytesString((double)size);
}

public static bool OpenUrl(string path)
{
try
{
using Process process = new();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = path;
process.Start();
return true;
}
catch
{
return false;
}
}

public static bool OpenDir(string dir)
{
if (!Directory.Exists(dir))
{
return false;
}

try
{
return OpenUrl(dir);
}
catch
{
// ignored
}
return false;
}

public static string? GetAppVersion()
{
return typeof(App).Assembly.GetName().Version?.ToString();
Expand Down
31 changes: 19 additions & 12 deletions BilibiliLiveRecordDownLoader/Utils/XmlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;
using System.Runtime.CompilerServices;
using System.Xml;

namespace BilibiliLiveRecordDownLoader.Utils;
Expand All @@ -12,20 +12,27 @@ public static string EscapeXmlChars(this string? str)
return string.Empty;
}

var sb = new StringBuilder(str.Length);
foreach (var c in str)
try
{
if (XmlConvert.IsXmlChar(c))
{
sb.Append(c);
}
else
return XmlConvert.VerifyXmlChars(str);
}
catch (XmlException)
{
DefaultInterpolatedStringHandler handler = new(2, str.Length);
foreach (char c in str)
{
sb.Append(@"\u");
sb.Append($@"{(uint)c:x4}");
if (XmlConvert.IsXmlChar(c))
{
handler.AppendFormatted(c);
}
else
{
handler.AppendLiteral(@"\u");
handler.AppendFormatted((int)c, @"x4");
}
}
}

return sb.ToString();
return handler.ToStringAndClear();
}
}
}
4 changes: 2 additions & 2 deletions BilibiliLiveRecordDownLoader/ViewModels/SettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private IObservable<Unit> OpenDirectory()
{
return Observable.Start(() =>
{
Utils.Utils.OpenDir(Config.MainDir);
FileUtils.OpenDir(Config.MainDir);
return Unit.Default;
});
}
Expand Down Expand Up @@ -144,7 +144,7 @@ private async Task CheckUpdateAsync(CancellationToken token)
};
if (await dialog.SafeShowAsync() == ContentDialogResult.Primary)
{
Utils.Utils.OpenUrl(updateChecker.LatestVersionUrl);
FileUtils.OpenUrl(updateChecker.LatestVersionUrl);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BilibiliLiveRecordDownLoader.Enums;
using BilibiliLiveRecordDownLoader.Models;
using BilibiliLiveRecordDownLoader.Utils;
using BilibiliLiveRecordDownLoader.Views.Dialogs;
using DynamicData;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -304,7 +305,7 @@ private IObservable<Unit> OpenDir(object? data)

string path = Path.Combine(_config.MainDir, $@"{room.RoomId}");
Directory.CreateDirectory(path);
Utils.Utils.OpenDir(path);
FileUtils.OpenDir(path);
}
catch (Exception ex)
{
Expand All @@ -329,7 +330,7 @@ private IObservable<Unit> OpenLiveUrl(object? data)
{
continue;
}
Utils.Utils.OpenUrl($@"https://live.bilibili.com/{room.RoomId}");
FileUtils.OpenUrl($@"https://live.bilibili.com/{room.RoomId}");
}
}
catch (Exception ex)
Expand Down

0 comments on commit e08da64

Please sign in to comment.