Issues (378)

app/Models/ReleaseFile.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Models;
4
5
use Blacklight\ElasticSearchSiteSearch;
6
use Blacklight\ManticoreSearch;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Carbon;
9
use Illuminate\Support\Facades\Log;
10
11
/**
12
 * App\Models\ReleaseFile.
13
 *
14
 * @property int $releases_id FK to releases.id
15
 * @property string $name
16
 * @property int $size
17
 * @property bool $ishashed
18
 * @property \Carbon\Carbon|null $created_at
19
 * @property \Carbon\Carbon|null $updated_at
20
 * @property bool $passworded
21
 * @property-read Release $release
22
 *
23
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereCreatedAt($value)
24
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereIshashed($value)
25
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereName($value)
26
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile wherePassworded($value)
27
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereReleasesId($value)
28
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereSize($value)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereUpdatedAt($value)
30
 *
31
 * @mixin \Eloquent
32
 *
33
 * @property string $crc32
34
 *
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile newModelQuery()
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile newQuery()
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile query()
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereCrc32($value)
39
 */
40
class ReleaseFile extends Model
41
{
42
    /**
43
     * @var bool
44
     */
45
    protected $dateFormat = false;
46
47
    /**
48
     * @var array
49
     */
50
    protected $guarded = [];
51
52
    /**
53
     * @var string
54
     */
55
    protected $primaryKey = 'releases_id';
56
57
    public function release(): \Illuminate\Database\Eloquent\Relations\BelongsTo
58
    {
59
        return $this->belongsTo(Release::class, 'releases_id');
60
    }
61
62
    /**
63
     * Get releasefiles row by id.
64
     *
65
     *
66
     * @return \Illuminate\Database\Eloquent\Collection|static[]
67
     */
68
    public static function getReleaseFiles($id)
69
    {
70
        return self::query()->where('releases_id', $id)->orderBy('name')->get();
71
    }
72
73
    /**
74
     * @return \Illuminate\Database\Eloquent\Collection|static[]
75
     */
76
    public static function getByGuid($guid)
77
    {
78
        return self::query()
79
            ->join('releases', 'releases.id', '=', 'release_files.releases_id')
80
            ->where('releases.guid', $guid)
81
            ->orderBy('release_files.name')->get();
0 ignored issues
show
'release_files.name' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

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

81
            ->orderBy(/** @scrutinizer ignore-type */ 'release_files.name')->get();
Loading history...
82
    }
83
84
    /**
85
     * Add new files for a release ID.
86
     *
87
     *
88
     *
89
     * @throws \Exception
90
     */
91
    public static function addReleaseFiles($id, $name, $size, $createdTime, $hasPassword, string $hash = '', string $crc = ''): int
92
    {
93
        // Check if we already have this data in table
94
        $duplicateCheck = self::query()->where('releases_id', $id)->where('name', $name)->first();
95
96
        // Check if the release exists in releases table to prevent foreign key error
97
        $releaseCheck = Release::query()->where('id', $id)->first();
98
99
        if (is_int($createdTime)) {
100
            if ($createdTime === 0) {
101
                $adjustedCreatedTime = now()->format('Y-m-d H:i:s');
102
            } else {
103
                $adjustedCreatedTime = Carbon::createFromTimestamp($createdTime, date_default_timezone_get())->format('Y-m-d H:i:s');
104
            }
105
        } else {
106
            $adjustedCreatedTime = $createdTime;
107
        }
108
109
        if ($duplicateCheck === null && $releaseCheck !== null) {
110
            try {
111
                $insert = self::insertOrIgnore([
112
                    'releases_id' => $id,
113
                    'name' => $name,
114
                    'size' => $size,
115
                    'created_at' => $adjustedCreatedTime,
116
                    'updated_at' => now()->timestamp,
117
                    'passworded' => $hasPassword,
118
                    'crc32' => $crc,
119
                    'ishashed' => preg_match('/^[a-fA-F0-9]{32}\b|^[a-fA-F0-9]{40}\b|^[a-fA-F0-9]{64}\b|^[a-fA-F0-9]{96}\b|^[a-fA-F0-9]{128}\b/i', $name) ? 1 : 0,
120
                ]);
121
            } catch (\PDOException $e) {
122
                Log::alert($e->getMessage());
123
            }
124
125
            if (\strlen($hash) === 32) {
126
                ParHash::insertOrIgnore(['releases_id' => $id, 'hash' => $hash]);
127
            }
128
            if (config('nntmux.elasticsearch_enabled') === true) {
129
                (new ElasticSearchSiteSearch)->updateRelease($id);
130
            } else {
131
                (new ManticoreSearch)->updateRelease($id);
132
            }
133
        }
134
135
        return $insert ?? 0;
136
    }
137
}
138