CleanNZB::GetNZBsWithNoDatabaseEntry()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 9.1111
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Release;
6
use Blacklight\NZB;
7
use Blacklight\ReleaseImage;
8
use Blacklight\Releases;
9
use Illuminate\Console\Command;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Facades\File;
12
13
class CleanNZB extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'nntmux:nzbclean
21
    {--notindb : Delete NZBs that dont exist in database}
22
    {--notondisk : Delete release in database that dont have a NZB on disk}
23
    {--chunksize=25000 : Chunk size for releases query}
24
    {--delete : Pass this argument to actually delete the files. Otherwise it\'s just a dry run.}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Find NZBs that dont have a release, or releases that have no NZBs.';
32
33
    /**
34
     * Execute the console command.
35
     */
36
    public function handle(): void
37
    {
38
        // Check if any options are false
39
        if (! $this->option('notindb') && ! $this->option('notondisk')) {
40
            $this->error('You must specify at least one option. See: --help');
41
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
42
        }
43
        if ($this->option('notindb')) {
44
            $this->GetNZBsWithNoDatabaseEntry($this->option('delete'));
45
        }
46
        if ($this->option('notondisk')) {
47
            $this->GetReleasesWithNoNZBOnDisk($this->option('delete'));
48
        }
49
    }
50
51
    private function GetNZBsWithNoDatabaseEntry($delete = false)
52
    {
53
        $this->info('Getting list of NZB files on disk to check if they exist in database');
54
        $releases = new Release;
55
        $checked = $deleted = 0;
56
        // Get the list of NZBs in the NZB folder
57
        $dirItr = new \RecursiveDirectoryIterator(config('nntmux_settings.path_to_nzbs'));
58
        $itr = new \RecursiveIteratorIterator($dirItr, \RecursiveIteratorIterator::LEAVES_ONLY);
59
60
        // Checking filename GUIDs against the releases table
61
        foreach ($itr as $filePath) {
62
            $guid = stristr($filePath->getFilename(), '.nzb.gz', true);
63
            if (File::isFile($filePath) && $guid) {
64
                // If NZB file guid is not present in DB delete the file from disk
65
                if (! $releases->whereGuid($guid)->exists()) {
66
                    if ($delete) {
67
                        File::delete($filePath);
68
                    }
69
                    $deleted++;
70
                    $this->line("Deleted orphan file: $guid.nzb.gz");
71
                }
72
                $checked++;
73
            }
74
            echo "Checked: $checked / Deleted: $deleted\r";
75
        }
76
        $this->info("Checked: $checked / Deleted: $deleted");
77
    }
78
79
    private function GetReleasesWithNoNZBOnDisk($delete = false)
80
    {
81
        // Setup
82
        $nzb = new NZB;
83
        $rel = new Releases;
84
        $checked = $deleted = 0;
85
86
        $this->info('Getting list of releases from database to check if they have a corresponding NZB on disk');
87
        $total = Release::count();
88
        $this->alert("Total releases to check: $total");
89
90
        Release::where('nzbstatus', 1)->chunkById((int) $this->option('chunksize'), function (Collection $releases) use ($delete, &$checked, &$deleted, $nzb, $rel) {
91
            echo 'Total done: '.$checked."\r";
92
            foreach ($releases as $r) {
93
94
                if (! $nzb->NZBPath($r->guid)) {
95
                    if ($delete) {
96
                        $rel->deleteSingle(['g' => $r->guid, 'i' => $r->id], $nzb, new ReleaseImage);
97
                    }
98
                    $deleted++;
99
                    $this->line("Deleted: $r->searchname -> $r->guid");
100
                }
101
                $checked++;
102
            }
103
        });
104
        $this->info("Checked: $checked / Deleted: $deleted");
105
    }
106
}
107