Skip to content

Commit 6287b3f

Browse files
mapitmanCopilot
andauthored
feat: cleanup temp rip directory after successful encode process (#49)
* feat: cleanup temp rip directory after successful encode process * Update src/RipSharp/Services/DiscRipper.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Only auto-cleanup auto-generated temp directories - Add TempWasAutoGenerated flag to RipOptions to track whether temp directory was auto-generated - Modify CleanupTempDirectory to only delete auto-generated temp directories - Preserve user-specified temp directories to prevent accidental data loss - Addresses PR review feedback about safety of cleanup operation * test: update tests for random temp directory naming - Update tests to verify temp directories use random 8-char names starting with dot - Verify TempWasAutoGenerated flag is set correctly for auto-generated directories - Ensure flag is false for user-specified temp directories --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0e32fd7 commit 6287b3f

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/RipSharp.Tests/Core/RipOptionsTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public void ParseArgs_WithoutTemp_DefaultsToOutputDotMakemkv()
9898

9999
var result = RipOptions.ParseArgs(args);
100100

101-
result.Temp.Should().Be(Path.Combine("/tmp/movies", ".makemkv"));
101+
result.Temp.Should().StartWith("/tmp/movies/.");
102+
Path.GetFileName(result.Temp).Should().MatchRegex("^\\.[a-z0-9]{8}$");
103+
result.TempWasAutoGenerated.Should().BeTrue();
102104
}
103105

104106
[Fact]
@@ -109,6 +111,7 @@ public void ParseArgs_WithTemp_SetsCustomTempDirectory()
109111
var result = RipOptions.ParseArgs(args);
110112

111113
result.Temp.Should().Be("/mnt/large-disk/temp");
114+
result.TempWasAutoGenerated.Should().BeFalse();
112115
}
113116

114117
[Fact]
@@ -516,7 +519,9 @@ public void ParseArgs_MovieScenario_ParsesCorrectly()
516519
result.Tv.Should().BeFalse();
517520
result.Title.Should().Be("The Matrix");
518521
result.Year.Should().Be(1999);
519-
result.Temp.Should().Be(Path.Combine("~/Movies", ".makemkv"));
522+
result.Temp.Should().StartWith("~/Movies/.");
523+
Path.GetFileName(result.Temp).Should().MatchRegex("^\\.[a-z0-9]{8}$");
524+
result.TempWasAutoGenerated.Should().BeTrue();
520525
}
521526

522527
[Fact]

src/RipSharp/Core/RipOptions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class RipOptions
1717
public bool Debug { get; set; }
1818
public string? DiscType { get; set; } // dvd|bd|uhd
1919
public bool ShowHelp { get; set; }
20+
public bool TempWasAutoGenerated { get; set; }
2021

2122
public static RipOptions ParseArgs(string[] args)
2223
{
@@ -74,11 +75,17 @@ public static RipOptions ParseArgs(string[] args)
7475
}
7576
if (string.IsNullOrEmpty(opts.Temp))
7677
{
77-
opts.Temp = Path.Combine(opts.Output, ".makemkv");
78+
opts.Temp = Path.Combine(opts.Output, GetTempDirectoryName());
79+
opts.TempWasAutoGenerated = true;
7880
}
7981
return opts;
8082
}
8183

84+
private static string GetTempDirectoryName()
85+
{
86+
return $".{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}";
87+
}
88+
8289
public static void DisplayHelp(IConsoleWriter writer)
8390
{
8491
writer.Plain("ripsharp - DVD/Blu-Ray/UHD disc ripping tool");
@@ -95,7 +102,7 @@ public static void DisplayHelp(IConsoleWriter writer)
95102
writer.Plain(" - movie: Treat as single movie");
96103
writer.Plain(" - tv: Treat as TV series");
97104
writer.Plain(" --disc PATH Optical drive path (default: disc:0)");
98-
writer.Plain(" --temp PATH Temporary ripping directory (default: {output}/.makemkv)");
105+
writer.Plain(" --temp PATH Temporary ripping directory (default: {output}/<RANDOM_STRING>)");
99106
writer.Plain(" --title TEXT Custom title for file naming");
100107
writer.Plain(" --year YYYY Release year (movies only)");
101108
writer.Plain(" --season N Season number (TV only, default: 1)");

src/RipSharp/Services/DiscRipper.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ public async Task<List<string>> ProcessDiscAsync(RipOptions options)
5252
}
5353

5454
var finalFiles = await EncodeAndRenameAsync(discInfo, titleIds, rippedFilesMap, metadata, options);
55+
56+
if (finalFiles.Count > 0)
57+
{
58+
CleanupTempDirectory(options);
59+
}
60+
else if (rippedFilesMap.Count > 0)
61+
{
62+
_notifier.Error($"No files were successfully encoded; temporary files have been left in: {options.Temp}");
63+
}
64+
5565
_notifier.Success($"Processing complete. Output files: {finalFiles.Count}");
5666
foreach (var f in finalFiles) _notifier.Plain(f);
5767
return finalFiles;
@@ -262,4 +272,37 @@ private async Task<List<string>> EncodeAndRenameAsync(DiscInfo discInfo, List<in
262272
}
263273
return finalFiles;
264274
}
275+
276+
private void CleanupTempDirectory(RipOptions options)
277+
{
278+
if (string.IsNullOrWhiteSpace(options.Temp)) return;
279+
280+
// Only auto-cleanup directories that were auto-generated
281+
if (!options.TempWasAutoGenerated)
282+
{
283+
_notifier.Muted($"Preserving user-specified temp directory: {options.Temp}");
284+
return;
285+
}
286+
287+
var tempPath = Path.GetFullPath(options.Temp);
288+
var outputPath = Path.GetFullPath(options.Output);
289+
290+
if (tempPath == outputPath)
291+
{
292+
_notifier.Warning("Skipping temp cleanup because temp directory matches the output directory.");
293+
return;
294+
}
295+
296+
if (!Directory.Exists(tempPath)) return;
297+
298+
try
299+
{
300+
Directory.Delete(tempPath, recursive: true);
301+
_notifier.Muted($"Cleaned up temporary rip files at {tempPath}");
302+
}
303+
catch (Exception ex)
304+
{
305+
_notifier.Warning($"Failed to clean up temp directory '{tempPath}': {ex.Message}");
306+
}
307+
}
265308
}

0 commit comments

Comments
 (0)