Issues (427)

app/Services/Par2Processor.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\Category;
6
use App\Models\Release;
7
use App\Models\ReleaseFile;
8
use App\Models\UsenetGroup;
9
use Blacklight\NameFixer;
10
use Blacklight\NNTP;
11
use dariusiii\rarinfo\Par2Info;
12
use Illuminate\Support\Carbon;
13
14
/**
15
 * Service responsible for parsing PAR2 data and applying results to releases.
16
 */
17
class Par2Processor
18
{
19
    private NameFixer $nameFixer;
20
21
    private Par2Info $par2Info;
22
23
    private bool $addPar2;
24
25
    private bool $alternateNNTP;
26
27
    public function __construct(NameFixer $nameFixer, Par2Info $par2Info, bool $addPar2, bool $alternateNNTP)
28
    {
29
        $this->nameFixer = $nameFixer;
30
        $this->par2Info = $par2Info;
31
        $this->addPar2 = $addPar2;
32
        $this->alternateNNTP = $alternateNNTP;
33
    }
34
35
    /**
36
     * Attempt to get a better name from a PAR2 file and categorize the release.
37
     *
38
     * @param  string  $messageID  MessageID from NZB file.
39
     * @param  int  $relID  ID of the release.
40
     * @param  int  $groupID  Group ID of the release.
41
     * @param  NNTP  $nntp  Class NNTP
42
     * @param  int  $show  Only show result or apply it.
43
     */
44
    public function parseFromMessage(string $messageID, int $relID, int $groupID, NNTP $nntp, int $show): bool
45
    {
46
        if ($messageID === '') {
47
            return false;
48
        }
49
50
        $query = Release::query()
51
            ->where(['isrenamed' => 0, 'id' => $relID])
52
            ->select(['id', 'groups_id', 'categories_id', 'name', 'searchname', 'postdate', 'id as releases_id'])
53
            ->first();
54
55
        if ($query === null) {
56
            return false;
57
        }
58
59
        // Only get a new name if the category is OTHER.
60
        $foundName = true;
61
        if (\in_array((int) $query['categories_id'], Category::OTHERS_GROUP, false)) {
62
            $foundName = false;
63
        }
64
65
        // Get the PAR2 file.
66
        $par2 = $nntp->getMessages(UsenetGroup::getNameByID($groupID), $messageID, $this->alternateNNTP);
67
        if ($nntp->isError($par2)) {
68
            return false;
69
        }
70
71
        // Put the PAR2 into Par2Info, check if there's an error.
72
        $this->par2Info->setData($par2);
73
        if ($this->par2Info->error) {
74
            return false;
75
        }
76
77
        // Get the file list from Par2Info.
78
        $files = $this->par2Info->getFileList();
79
        if ($files !== false && \count($files) > 0) {
80
            $filesAdded = 0;
81
82
            // Loop through the files.
83
            foreach ($files as $file) {
84
                if (! isset($file['name'])) {
85
                    continue;
86
                }
87
88
                // If we found a name and added 20 files, stop.
89
                if ($foundName === true && $filesAdded > 20) {
90
                    break;
91
                }
92
93
                if ($this->addPar2) {
94
                    // Add to release files.
95
                    if ($filesAdded < 21 && ReleaseFile::query()->where(['releases_id' => $relID, 'name' => $file['name']])->first() === null) {
96
                        // Try to add the files to the DB.
97
                        if (ReleaseFile::addReleaseFiles(
98
                            $relID,
99
                            $file['name'],
100
                            $file['size'],
101
                            $query['postdate'] !== null ? Carbon::createFromFormat('Y-m-d H:i:s', $query['postdate']) : now(),
102
                            0,
103
                            $file['hash_16K']
104
                        )) {
105
                            $filesAdded++;
106
                        }
107
                    }
108
                } else {
109
                    $filesAdded++;
110
                }
111
112
                // Try to get a new name.
113
                if ($foundName === false) {
114
                    $query['textstring'] = $file['name'];
115
                    if ($this->nameFixer->checkName($query, 1, 'PAR2, ', 1, $show)) {
0 ignored issues
show
$show of type integer is incompatible with the type boolean expected by parameter $show of Blacklight\NameFixer::checkName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
                    if ($this->nameFixer->checkName($query, 1, 'PAR2, ', 1, /** @scrutinizer ignore-type */ $show)) {
Loading history...
116
                        $foundName = true;
117
                    }
118
                }
119
            }
120
121
            // If we found some files.
122
            if ($filesAdded > 0) {
123
                // Update the file count with the new file count + old file count.
124
                Release::whereId($relID)->increment('rarinnerfilecount', $filesAdded);
125
            }
126
            if ($foundName === true) {
127
                return true;
128
            }
129
        }
130
131
        return false;
132
    }
133
}
134