Skip to content

Commit a49daec

Browse files
authored
Delete output file on error if it's empty (#1101)
1 parent e0f7957 commit a49daec

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

TwitchDownloaderCore/ChatDownloader.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
257257
// Open the destination file so that it exists in the filesystem.
258258
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
259259

260+
try
261+
{
262+
await DownloadAsyncImpl(outputFileInfo, outputFs, cancellationToken);
263+
}
264+
catch
265+
{
266+
outputFileInfo.Refresh();
267+
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
268+
{
269+
outputFileInfo.Delete();
270+
}
271+
272+
throw;
273+
}
274+
}
275+
276+
private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
277+
{
260278
DownloadType downloadType = downloadOptions.Id.All(char.IsDigit) ? DownloadType.Video : DownloadType.Clip;
261279

262280
var (chatRoot, connectionCount) = await InitChatRoot(downloadType);

TwitchDownloaderCore/ChatRenderer.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ public async Task RenderVideoAsync(CancellationToken cancellationToken)
7979
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
8080
await using var maskFs = maskFileInfo?.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
8181

82+
try
83+
{
84+
await RenderAsyncImpl(outputFileInfo, outputFs, maskFileInfo, maskFs, cancellationToken);
85+
}
86+
catch
87+
{
88+
outputFileInfo.Refresh();
89+
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
90+
{
91+
outputFileInfo.Delete();
92+
}
93+
94+
if (maskFileInfo is not null)
95+
{
96+
maskFileInfo.Refresh();
97+
if (maskFileInfo.Exists && maskFileInfo.Length == 0)
98+
{
99+
maskFileInfo.Delete();
100+
}
101+
}
102+
103+
throw;
104+
}
105+
}
106+
107+
private async Task RenderAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, FileInfo maskFileInfo, FileStream maskFs, CancellationToken cancellationToken)
108+
{
82109
_progress.SetStatus("Fetching Images [1/2]");
83110
await Task.Run(() => FetchScaledImages(cancellationToken), cancellationToken);
84111

TwitchDownloaderCore/ChatUpdater.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ public async Task UpdateAsync(CancellationToken cancellationToken)
3838
// Open the destination file so that it exists in the filesystem.
3939
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
4040

41+
try
42+
{
43+
await UpdateAsyncImpl(outputFileInfo, outputFs, cancellationToken);
44+
}
45+
catch
46+
{
47+
outputFileInfo.Refresh();
48+
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
49+
{
50+
outputFileInfo.Delete();
51+
}
52+
53+
throw;
54+
}
55+
}
56+
57+
private async Task UpdateAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
58+
{
4159
chatRoot.FileInfo = new() { Version = ChatRootVersion.CurrentVersion, CreatedAt = chatRoot.FileInfo.CreatedAt, UpdatedAt = DateTime.Now };
4260
if (!Path.GetExtension(_updateOptions.InputFile.Replace(".gz", ""))!.Equals(".json", StringComparison.OrdinalIgnoreCase))
4361
{

TwitchDownloaderCore/ClipDownloader.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
3737
// Open the destination file so that it exists in the filesystem.
3838
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
3939

40+
try
41+
{
42+
await DownloadAsyncImpl(outputFileInfo, outputFs, cancellationToken);
43+
}
44+
catch
45+
{
46+
outputFileInfo.Refresh();
47+
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
48+
{
49+
outputFileInfo.Delete();
50+
}
51+
52+
throw;
53+
}
54+
}
55+
56+
private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
57+
{
4058
_progress.SetStatus("Fetching Clip Info");
4159

4260
var downloadUrl = await GetDownloadUrl();

TwitchDownloaderCore/TsMerger.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ public async Task MergeAsync(CancellationToken cancellationToken)
3232
// Open the destination file so that it exists in the filesystem.
3333
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
3434

35+
try
36+
{
37+
await MergeAsyncImpl(outputFs, cancellationToken);
38+
}
39+
catch
40+
{
41+
outputFileInfo.Refresh();
42+
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
43+
{
44+
outputFileInfo.Delete();
45+
}
46+
47+
throw;
48+
}
49+
}
50+
51+
private async Task MergeAsyncImpl(FileStream outputFs, CancellationToken cancellationToken)
52+
{
3553
var isM3U8 = false;
3654
var isFirst = true;
3755
var fileList = new List<string>();

TwitchDownloaderCore/VideoDownloader.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
4545
// Open the destination file so that it exists in the filesystem.
4646
await using var outputFs = outputFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
4747

48+
try
49+
{
50+
await DownloadAsyncImpl(outputFileInfo, outputFs, cancellationToken);
51+
}
52+
catch
53+
{
54+
outputFileInfo.Refresh();
55+
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
56+
{
57+
outputFileInfo.Delete();
58+
}
59+
60+
throw;
61+
}
62+
}
63+
64+
private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputFs, CancellationToken cancellationToken)
65+
{
4866
await TwitchHelper.CleanupAbandonedVideoCaches(downloadOptions.TempFolder, downloadOptions.CacheCleanerCallback, _progress);
4967

5068
string downloadFolder = Path.Combine(

0 commit comments

Comments
 (0)