Skip to content

Commit

Permalink
Delete output file on error if it's empty (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN authored Jun 17, 2024
1 parent e0f7957 commit a49daec
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
18 changes: 18 additions & 0 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,24 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
// Open the destination file so that it exists in the filesystem.
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);

try
{
await DownloadAsyncImpl(outputFileInfo, outputFs, cancellationToken);
}
catch
{
outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
outputFileInfo.Delete();
}

throw;
}
}

private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
{
DownloadType downloadType = downloadOptions.Id.All(char.IsDigit) ? DownloadType.Video : DownloadType.Clip;

var (chatRoot, connectionCount) = await InitChatRoot(downloadType);
Expand Down
27 changes: 27 additions & 0 deletions TwitchDownloaderCore/ChatRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ public async Task RenderVideoAsync(CancellationToken cancellationToken)
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
await using var maskFs = maskFileInfo?.Open(FileMode.Create, FileAccess.Write, FileShare.Read);

try
{
await RenderAsyncImpl(outputFileInfo, outputFs, maskFileInfo, maskFs, cancellationToken);
}
catch
{
outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
outputFileInfo.Delete();
}

if (maskFileInfo is not null)
{
maskFileInfo.Refresh();
if (maskFileInfo.Exists && maskFileInfo.Length == 0)
{
maskFileInfo.Delete();
}
}

throw;
}
}

private async Task RenderAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, FileInfo maskFileInfo, FileStream maskFs, CancellationToken cancellationToken)
{
_progress.SetStatus("Fetching Images [1/2]");
await Task.Run(() => FetchScaledImages(cancellationToken), cancellationToken);

Expand Down
18 changes: 18 additions & 0 deletions TwitchDownloaderCore/ChatUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public async Task UpdateAsync(CancellationToken cancellationToken)
// Open the destination file so that it exists in the filesystem.
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);

try
{
await UpdateAsyncImpl(outputFileInfo, outputFs, cancellationToken);
}
catch
{
outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
outputFileInfo.Delete();
}

throw;
}
}

private async Task UpdateAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
{
chatRoot.FileInfo = new() { Version = ChatRootVersion.CurrentVersion, CreatedAt = chatRoot.FileInfo.CreatedAt, UpdatedAt = DateTime.Now };
if (!Path.GetExtension(_updateOptions.InputFile.Replace(".gz", ""))!.Equals(".json", StringComparison.OrdinalIgnoreCase))
{
Expand Down
18 changes: 18 additions & 0 deletions TwitchDownloaderCore/ClipDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
// Open the destination file so that it exists in the filesystem.
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);

try
{
await DownloadAsyncImpl(outputFileInfo, outputFs, cancellationToken);
}
catch
{
outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
outputFileInfo.Delete();
}

throw;
}
}

private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
{
_progress.SetStatus("Fetching Clip Info");

var downloadUrl = await GetDownloadUrl();
Expand Down
18 changes: 18 additions & 0 deletions TwitchDownloaderCore/TsMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ public async Task MergeAsync(CancellationToken cancellationToken)
// Open the destination file so that it exists in the filesystem.
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);

try
{
await MergeAsyncImpl(outputFs, cancellationToken);
}
catch
{
outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
outputFileInfo.Delete();
}

throw;
}
}

private async Task MergeAsyncImpl(FileStream outputFs, CancellationToken cancellationToken)
{
var isM3U8 = false;
var isFirst = true;
var fileList = new List<string>();
Expand Down
18 changes: 18 additions & 0 deletions TwitchDownloaderCore/VideoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
// Open the destination file so that it exists in the filesystem.
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);

try
{
await DownloadAsyncImpl(outputFileInfo, outputFs, cancellationToken);
}
catch
{
outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
outputFileInfo.Delete();
}

throw;
}
}

private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
{
await TwitchHelper.CleanupAbandonedVideoCaches(downloadOptions.TempFolder, downloadOptions.CacheCleanerCallback, _progress);

string downloadFolder = Path.Combine(
Expand Down

0 comments on commit a49daec

Please sign in to comment.