Issues (378)

app/Console/Commands/NntmuxRemoveBadReleases.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Release;
6
use App\Models\ReleaseFile;
7
use Blacklight\ManticoreSearch;
8
use Blacklight\NZB;
9
use Blacklight\ReleaseImage;
10
use Elasticsearch;
11
use Elasticsearch\Common\Exceptions\Missing404Exception;
12
use Illuminate\Console\Command;
13
use Illuminate\Support\Facades\File;
14
15
class NntmuxRemoveBadReleases extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'nntmux:remove-bad';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Update releases that have passworded files inside archives and remove releases that cannot be PPA\'d properly';
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @return void
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     *
45
     * @throws \Exception
46
     */
47
    public function handle(): void
48
    {
49
        // Select releases with password status -2 and smaller and delete them. Also delete the files from the filesystem.
50
        $badReleases = Release::query()->where('passwordstatus', '<=', -2)->get();
51
        foreach ($badReleases as $badRelease) {
52
            $nzbPath = (new NZB)->getNZBPath($badRelease->guid);
0 ignored issues
show
The property guid does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
53
            File::delete($nzbPath);
54
            (new ReleaseImage)->delete($badRelease->guid);
55
            if (config('nntmux.elasticsearch_enabled') === true) {
56
                $params = [
57
                    'index' => 'releases',
58
                    'id' => $badRelease->id,
59
                ];
60
61
                try {
62
                    Elasticsearch::delete($params);
63
                } catch (Missing404Exception $e) {
64
                    // we do nothing here just catch the error, we don't care if release is missing from ES, we are deleting it anyway
65
                }
66
            } else {
67
                $identifiers = [
68
                    'i' => $badRelease->id,
69
                ];
70
                // Delete from sphinx.
71
                (new ManticoreSearch)->deleteRelease($identifiers);
72
            }
73
            $badRelease->delete();
74
        }
75
        Release::query()->where('passwordstatus', '=', -2)->delete();
76
77
        $passReleases = ReleaseFile::query()->where('passworded', '=', 1)->groupBy('releases_id')->get();
78
79
        $count = 0;
80
        foreach ($passReleases as $passRelease) {
81
            Release::whereId($passRelease->releases_id)->update(['passwordstatus' => 1]);
82
            $count++;
83
        }
84
85
        $this->info('Updated '.$count.' bad releases');
86
    }
87
}
88