mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 01:08:56 +00:00
Update nzb import
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
/**
|
||||
* NZB import result values used while processing imported NZB files.
|
||||
*/
|
||||
enum NzbImportStatus: string
|
||||
{
|
||||
case Inserted = 'inserted';
|
||||
|
||||
case Duplicate = 'duplicate';
|
||||
|
||||
case Blacklisted = 'blacklisted';
|
||||
|
||||
case NoGroup = 'nogroup';
|
||||
|
||||
case Failed = 'failed';
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Nzb;
|
||||
|
||||
use App\Enums\NzbImportStatus;
|
||||
use App\Models\Release;
|
||||
use App\Models\Settings;
|
||||
use App\Models\UsenetGroup;
|
||||
@@ -95,7 +96,7 @@ class NzbImportService
|
||||
}
|
||||
|
||||
$start = now()->toImmutable()->format('Y-m-d H:i:s');
|
||||
$nzbsImported = $nzbsSkipped = 0;
|
||||
$nzbsImported = $nzbsSkipped = $nzbsDuplicate = 0;
|
||||
|
||||
// Convert all files to string paths and filter to only process NZB files
|
||||
$nzbFiles = [];
|
||||
@@ -160,13 +161,13 @@ class NzbImportService
|
||||
// Try to insert the NZB details into the DB.
|
||||
$nzbFileName = $useNzbName === true ? str_ireplace('.nzb', '', basename($nzbFilePath)) : '';
|
||||
try {
|
||||
$inserted = $this->scanNZBFile($nzbXML, $nzbFileName, $source);
|
||||
$importStatus = $this->scanNZBFile($nzbXML, $nzbFileName, $source);
|
||||
} catch (\Exception $e) {
|
||||
$this->echoOut('ERROR: Problem inserting: '.$nzbFilePath);
|
||||
$inserted = false;
|
||||
$importStatus = NzbImportStatus::Failed;
|
||||
}
|
||||
|
||||
if ($inserted) {
|
||||
if ($importStatus === NzbImportStatus::Inserted) {
|
||||
// Try to copy the NZB to the NZB folder.
|
||||
$path = $this->nzb->getNzbPath($this->relGuid, 0, true);
|
||||
|
||||
@@ -195,11 +196,26 @@ class NzbImportService
|
||||
$nzbsImported++;
|
||||
}
|
||||
} else {
|
||||
$this->echoOut('ERROR: Failed to insert NZB!');
|
||||
if ($deleteFailed) {
|
||||
File::delete($nzbFilePath);
|
||||
if ($importStatus === NzbImportStatus::Duplicate) {
|
||||
$nzbsDuplicate++;
|
||||
|
||||
if ($delete || $deleteFailed) {
|
||||
File::delete($nzbFilePath);
|
||||
}
|
||||
} else {
|
||||
if (in_array($importStatus, [NzbImportStatus::Blacklisted, NzbImportStatus::NoGroup], true)) {
|
||||
if ($delete || $deleteFailed) {
|
||||
File::delete($nzbFilePath);
|
||||
}
|
||||
} else {
|
||||
$this->echoOut('ERROR: Failed to insert NZB!');
|
||||
if ($deleteFailed) {
|
||||
File::delete($nzbFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
$nzbsSkipped++;
|
||||
}
|
||||
$nzbsSkipped++;
|
||||
}
|
||||
} else {
|
||||
$this->echoOut('ERROR: Unable to fetch: '.$nzbFilePath);
|
||||
@@ -207,12 +223,14 @@ class NzbImportService
|
||||
}
|
||||
}
|
||||
$this->echoOut(
|
||||
'Proccessed '.
|
||||
'Processed '.
|
||||
$nzbsImported.
|
||||
' NZBs in '.
|
||||
now()->diffInSeconds($start, true).' seconds, '.
|
||||
$nzbsSkipped.
|
||||
' NZBs were skipped.'
|
||||
' NZBs were skipped, '.
|
||||
$nzbsDuplicate.
|
||||
' were duplicates.'
|
||||
);
|
||||
|
||||
if ($this->browser) {
|
||||
@@ -227,7 +245,7 @@ class NzbImportService
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function scanNZBFile(mixed &$nzbXML, mixed $nzbFileName = '', mixed $source = ''): bool
|
||||
protected function scanNZBFile(mixed &$nzbXML, mixed $nzbFileName = '', mixed $source = ''): NzbImportStatus
|
||||
{
|
||||
$binary_names = [];
|
||||
$totalFiles = $totalSize = $groupID = 0;
|
||||
@@ -314,7 +332,7 @@ class NzbImportService
|
||||
// Persist blacklist usage stats if we matched any rule during this NZB processing
|
||||
$this->blacklistService->updateBlacklistUsage($this->blacklistService->getAndClearIdsToUpdate()); // @phpstan-ignore argument.type
|
||||
|
||||
return false;
|
||||
return $isBlackListed ? NzbImportStatus::Blacklisted : NzbImportStatus::NoGroup;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,7 +370,7 @@ class NzbImportService
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function insertNZB(mixed $nzbDetails): bool
|
||||
protected function insertNZB(mixed $nzbDetails): NzbImportStatus
|
||||
{
|
||||
// Make up a GUID for the release.
|
||||
$this->relGuid = Str::uuid()->toString();
|
||||
@@ -405,16 +423,16 @@ class NzbImportService
|
||||
} else {
|
||||
$this->echoOut('This release is already in our DB so skipping: '.$subject);
|
||||
|
||||
return false;
|
||||
return NzbImportStatus::Duplicate;
|
||||
}
|
||||
|
||||
if ($relID === null) {
|
||||
$this->echoOut('ERROR: Problem inserting: '.$subject);
|
||||
|
||||
return false;
|
||||
return NzbImportStatus::Failed;
|
||||
}
|
||||
|
||||
return true;
|
||||
return NzbImportStatus::Inserted;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Enums\NzbImportStatus;
|
||||
use App\Services\Nzb\NzbImportService;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PDO;
|
||||
use Tests\TestCase;
|
||||
|
||||
final class NzbImportServiceTest extends TestCase
|
||||
{
|
||||
private string $databasePath;
|
||||
|
||||
/**
|
||||
* @var array<string, string|false>
|
||||
*/
|
||||
private array $originalEnvironment = [];
|
||||
|
||||
public function createApplication()
|
||||
{
|
||||
$this->databasePath = sys_get_temp_dir().'/nntmux-nzb-import-test.sqlite';
|
||||
|
||||
$this->originalEnvironment = [
|
||||
'APP_ENV' => getenv('APP_ENV'),
|
||||
'DB_CONNECTION' => getenv('DB_CONNECTION'),
|
||||
'DB_DATABASE' => getenv('DB_DATABASE'),
|
||||
];
|
||||
|
||||
if (file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
|
||||
$pdo = new PDO('sqlite:'.$this->databasePath);
|
||||
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
|
||||
$pdo->exec("INSERT INTO settings (name, value) VALUES
|
||||
('categorizeforeign', '0'),
|
||||
('catwebdl', '0'),
|
||||
('title', 'NNTmux Test'),
|
||||
('home_link', '/')");
|
||||
|
||||
$this->setEnvironmentValue('APP_ENV', 'testing');
|
||||
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
|
||||
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
|
||||
|
||||
$app = require __DIR__.'/../../bootstrap/app.php';
|
||||
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
config([
|
||||
'database.default' => 'sqlite',
|
||||
'database.connections.sqlite.database' => $this->databasePath,
|
||||
'app.key' => 'base64:'.base64_encode(random_bytes(32)),
|
||||
]);
|
||||
|
||||
DB::purge();
|
||||
DB::reconnect();
|
||||
Cache::flush();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
|
||||
foreach ($this->originalEnvironment as $key => $value) {
|
||||
$this->setEnvironmentValue($key, $value === false ? null : $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_begin_import_uses_specific_messages_and_counts_duplicates_separately(): void
|
||||
{
|
||||
$duplicateFile = $this->makeNzbFile('duplicate');
|
||||
$blacklistedFile = $this->makeNzbFile('blacklisted');
|
||||
$noGroupFile = $this->makeNzbFile('nogroup');
|
||||
$failedFile = $this->makeNzbFile('failed');
|
||||
|
||||
$service = new class(['Browser' => true], [NzbImportStatus::Duplicate, NzbImportStatus::Blacklisted, NzbImportStatus::NoGroup, NzbImportStatus::Failed]) extends NzbImportService
|
||||
{
|
||||
/**
|
||||
* @param array<NzbImportStatus> $statuses
|
||||
*/
|
||||
public function __construct(array $options, private array $statuses)
|
||||
{
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
protected function getAllGroups(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function scanNZBFile(mixed &$nzbXML, mixed $nzbFileName = '', mixed $source = ''): NzbImportStatus
|
||||
{
|
||||
$status = array_shift($this->statuses) ?? NzbImportStatus::Failed;
|
||||
|
||||
match ($status) {
|
||||
NzbImportStatus::Duplicate => $this->echoOut('This release is already in our DB so skipping: duplicate subject'),
|
||||
NzbImportStatus::Blacklisted => $this->echoOut('Subject is blacklisted: blacklisted subject'),
|
||||
NzbImportStatus::NoGroup => $this->echoOut('No group found for missing-group subject (one of alt.test are missing'),
|
||||
default => null,
|
||||
};
|
||||
|
||||
return $status;
|
||||
}
|
||||
};
|
||||
|
||||
$result = $service->beginImport(
|
||||
[$duplicateFile, $blacklistedFile, $noGroupFile, $failedFile],
|
||||
delete: false,
|
||||
deleteFailed: true,
|
||||
);
|
||||
|
||||
$this->assertIsString($result);
|
||||
$this->assertStringContainsString('This release is already in our DB so skipping: duplicate subject', $result);
|
||||
$this->assertStringContainsString('Subject is blacklisted: blacklisted subject', $result);
|
||||
$this->assertStringContainsString('No group found for missing-group subject (one of alt.test are missing', $result);
|
||||
$this->assertSame(1, substr_count($result, 'ERROR: Failed to insert NZB!'));
|
||||
$this->assertStringContainsString('Processed 0 NZBs in ', $result);
|
||||
$this->assertStringContainsString('3 NZBs were skipped, 1 were duplicates.', $result);
|
||||
|
||||
$this->assertFileDoesNotExist($duplicateFile);
|
||||
$this->assertFileDoesNotExist($blacklistedFile);
|
||||
$this->assertFileDoesNotExist($noGroupFile);
|
||||
$this->assertFileDoesNotExist($failedFile);
|
||||
}
|
||||
|
||||
public function test_begin_import_deletes_duplicate_blacklisted_and_no_group_files_when_delete_is_enabled(): void
|
||||
{
|
||||
$duplicateFile = $this->makeNzbFile('duplicate-delete');
|
||||
$blacklistedFile = $this->makeNzbFile('blacklisted-delete');
|
||||
$noGroupFile = $this->makeNzbFile('nogroup-delete');
|
||||
|
||||
$service = new class(['Browser' => true], [NzbImportStatus::Duplicate, NzbImportStatus::Blacklisted, NzbImportStatus::NoGroup]) extends NzbImportService
|
||||
{
|
||||
/**
|
||||
* @param array<NzbImportStatus> $statuses
|
||||
*/
|
||||
public function __construct(array $options, private array $statuses)
|
||||
{
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
protected function getAllGroups(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function scanNZBFile(mixed &$nzbXML, mixed $nzbFileName = '', mixed $source = ''): NzbImportStatus
|
||||
{
|
||||
$status = array_shift($this->statuses) ?? NzbImportStatus::Failed;
|
||||
|
||||
match ($status) {
|
||||
NzbImportStatus::Duplicate => $this->echoOut('This release is already in our DB so skipping: duplicate subject'),
|
||||
NzbImportStatus::Blacklisted => $this->echoOut('Subject is blacklisted: blacklisted subject'),
|
||||
NzbImportStatus::NoGroup => $this->echoOut('No group found for missing-group subject (one of alt.test are missing'),
|
||||
default => null,
|
||||
};
|
||||
|
||||
return $status;
|
||||
}
|
||||
};
|
||||
|
||||
$result = $service->beginImport(
|
||||
[$duplicateFile, $blacklistedFile, $noGroupFile],
|
||||
delete: true,
|
||||
deleteFailed: false,
|
||||
);
|
||||
|
||||
$this->assertIsString($result);
|
||||
$this->assertStringNotContainsString('ERROR: Failed to insert NZB!', $result);
|
||||
$this->assertStringContainsString('2 NZBs were skipped, 1 were duplicates.', $result);
|
||||
|
||||
$this->assertFileDoesNotExist($duplicateFile);
|
||||
$this->assertFileDoesNotExist($blacklistedFile);
|
||||
$this->assertFileDoesNotExist($noGroupFile);
|
||||
}
|
||||
|
||||
private function makeNzbFile(string $suffix): string
|
||||
{
|
||||
$path = sys_get_temp_dir().'/'.$suffix.'-'.bin2hex(random_bytes(5)).'.nzb';
|
||||
file_put_contents($path, '<nzb></nzb>');
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
private function setEnvironmentValue(string $key, ?string $value): void
|
||||
{
|
||||
if ($value === null) {
|
||||
putenv($key);
|
||||
unset($_ENV[$key], $_SERVER[$key]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
putenv("{$key}={$value}");
|
||||
$_ENV[$key] = $value;
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user