Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete output file on error if it's empty #1101

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading