From b73f683289f2c8ad54b9bdf2bca4cfd0c10ce7cf Mon Sep 17 00:00:00 2001 From: DariusIII Date: Wed, 28 Jan 2026 10:54:53 +0100 Subject: [PATCH] Manage exception with missing nzb --- app/Http/Controllers/GetNzbController.php | 12 +++++- tests/Unit/GetNzbControllerTest.php | 49 +++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/GetNzbControllerTest.php diff --git a/app/Http/Controllers/GetNzbController.php b/app/Http/Controllers/GetNzbController.php index 414fb07be..0b3d10ec3 100644 --- a/app/Http/Controllers/GetNzbController.php +++ b/app/Http/Controllers/GetNzbController.php @@ -329,8 +329,18 @@ class GetNzbController extends BasePageController */ private function streamModifiedNzbContent(string $nzbPath, int $uid): void { - $fileHandle = gzopen($nzbPath, 'rb'); + if (! File::exists($nzbPath)) { + Log::warning('NZB file not found during streaming', ['path' => $nzbPath]); + echo 'NZB file not found'; + + return; + } + + $fileHandle = @gzopen($nzbPath, 'rb'); if ($fileHandle === false) { + Log::warning('Failed to open NZB file for streaming', ['path' => $nzbPath]); + echo 'Failed to read NZB file'; + return; } diff --git a/tests/Unit/GetNzbControllerTest.php b/tests/Unit/GetNzbControllerTest.php new file mode 100644 index 000000000..3666a03ef --- /dev/null +++ b/tests/Unit/GetNzbControllerTest.php @@ -0,0 +1,49 @@ +assertFalse(file_exists($nonExistentPath)); + } + + /** + * Test that the file check prevents gzopen from being called on missing files + */ + public function test_file_check_prevents_gzopen_error(): void + { + $nonExistentPath = '/tmp/non-existent-'.uniqid().'.nzb.gz'; + // Simulate the fix logic from streamModifiedNzbContent + if (! file_exists($nonExistentPath)) { + $errorMessage = 'NZB file not found'; + $gzopenCalled = false; + } else { + $gzopenCalled = true; + $errorMessage = ''; + } + $this->assertFalse($gzopenCalled, 'gzopen should not be called for non-existent files'); + $this->assertStringContainsString('NZB file not found', $errorMessage); + } + + /** + * Test that file_exists returns true for existing files + */ + public function test_file_exists_returns_true_for_existing_file(): void + { + $tempPath = '/tmp/existing-file-'.uniqid().'.nzb.gz'; + file_put_contents($tempPath, 'test content'); + try { + $this->assertTrue(file_exists($tempPath)); + } finally { + unlink($tempPath); + } + } +}