1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Blacklight\SphinxSearch; |
6
|
|
|
use Illuminate\Support\Carbon; |
7
|
|
|
use Yadakhov\InsertOnDuplicateKey; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
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 \App\Models\Release $release |
22
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereCreatedAt($value) |
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereIshashed($value) |
24
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereName($value) |
25
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile wherePassworded($value) |
26
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereReleasesId($value) |
27
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereSize($value) |
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereUpdatedAt($value) |
29
|
|
|
* @mixin \Eloquent |
30
|
|
|
*/ |
31
|
|
|
class ReleaseFile extends Model |
32
|
|
|
{ |
33
|
|
|
use InsertOnDuplicateKey; |
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $dateFormat = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $guarded = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $primaryKey = 'releases_id'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
51
|
|
|
*/ |
52
|
|
|
public function release(): \Illuminate\Database\Eloquent\Relations\BelongsTo |
53
|
|
|
{ |
54
|
|
|
return $this->belongsTo(Release::class, 'releases_id'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get releasefiles row by id. |
59
|
|
|
* |
60
|
|
|
* |
61
|
|
|
* @param $id |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
63
|
|
|
*/ |
64
|
|
|
public static function getReleaseFiles($id) |
65
|
|
|
{ |
66
|
|
|
return self::query()->where('releases_id', $id)->orderBy('name')->get(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $guid |
71
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
72
|
|
|
*/ |
73
|
|
|
public static function getByGuid($guid) |
74
|
|
|
{ |
75
|
|
|
return self::query() |
76
|
|
|
->join('releases', 'releases.id', '=', 'release_files.releases_id') |
77
|
|
|
->where('releases.guid', $guid) |
78
|
|
|
->orderBy('release_files.name')->get(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete a releasefiles row. |
83
|
|
|
* |
84
|
|
|
* |
85
|
|
|
* @param $id |
86
|
|
|
* @return mixed |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public static function deleteReleaseFiles($id) |
90
|
|
|
{ |
91
|
|
|
$res = self::query()->where('releases_id', $id)->delete(); |
92
|
|
|
(new SphinxSearch())->updateRelease($id); |
93
|
|
|
|
94
|
|
|
return $res; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add new files for a release ID. |
99
|
|
|
* |
100
|
|
|
* |
101
|
|
|
* @param $id |
102
|
|
|
* @param $name |
103
|
|
|
* @param $size |
104
|
|
|
* @param $createdTime |
105
|
|
|
* @param $hasPassword |
106
|
|
|
* |
107
|
|
|
* @param string $hash |
108
|
|
|
* @param string $crc |
109
|
|
|
* @return int |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
public static function addReleaseFiles($id, $name, $size, $createdTime, $hasPassword, $hash = '', $crc = ''): int |
113
|
|
|
{ |
114
|
|
|
// Check if we already have this data in table |
115
|
|
|
$duplicateCheck = self::query()->where('releases_id', $id)->where('name', utf8_encode($name))->first(); |
116
|
|
|
|
117
|
|
|
// Check if the release exists in releases table to prevent foreign key error |
118
|
|
|
$releaseCheck = Release::query()->where('id', $id)->first(); |
119
|
|
|
|
120
|
|
|
if (is_int($createdTime)) { |
121
|
|
|
if ($createdTime === 0) { |
122
|
|
|
$adjustedCreatedTime = now()->format('Y-m-d H:i:s'); |
123
|
|
|
} else { |
124
|
|
|
$adjustedCreatedTime = Carbon::createFromTimestamp($createdTime)->format('Y-m-d H:i:s'); |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
$adjustedCreatedTime = $createdTime; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($duplicateCheck === null && $releaseCheck !== null) { |
131
|
|
|
try { |
132
|
|
|
$insert = self::insertOnDuplicateKey([ |
133
|
|
|
'releases_id' => $id, |
134
|
|
|
'name' => escapeString(utf8_encode($name)), |
135
|
|
|
'size' => $size, |
136
|
|
|
'created_at' => $adjustedCreatedTime, |
137
|
|
|
'updated_at' => now()->timestamp, |
138
|
|
|
'passworded' => $hasPassword, |
139
|
|
|
'crc32' => escapeString($crc), |
140
|
|
|
], ['updated_at' => now()->timestamp]); |
141
|
|
|
} catch (\PDOException $e) { |
142
|
|
|
Log::alert($e->getMessage()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (\strlen($hash) === 32) { |
146
|
|
|
ParHash::insertIgnore(['releases_id' => $id, 'hash' => $hash]); |
147
|
|
|
} |
148
|
|
|
(new SphinxSearch())->updateRelease($id); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $insert ?? 0; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|