|
1
|
|
|
<?php namespace App\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* \App\Models\File |
|
7
|
|
|
* |
|
8
|
|
|
* @mixin \Eloquent |
|
9
|
|
|
* @property string $hash |
|
10
|
|
|
* @property string $disk |
|
11
|
|
|
* @property string $path |
|
12
|
|
|
* @property string $mime |
|
13
|
|
|
* @property integer $size |
|
14
|
|
|
* @property string $metadata |
|
15
|
|
|
* @property \Carbon\Carbon $created_at |
|
16
|
|
|
* @property \Carbon\Carbon $updated_at |
|
17
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|User[] $uploaders |
|
18
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File whereHash($value) |
|
19
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File whereDisk($value) |
|
20
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File wherePath($value) |
|
21
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File whereMime($value) |
|
22
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File whereSize($value) |
|
23
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File whereMetadata($value) |
|
24
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File whereCreatedAt($value) |
|
25
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File whereUpdatedAt($value) |
|
26
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\File ofType($type = 'image') |
|
27
|
|
|
*/ |
|
28
|
|
|
class File extends Model |
|
29
|
|
|
{ |
|
30
|
|
|
protected $table = 'files'; |
|
31
|
|
|
|
|
32
|
|
|
protected $primaryKey = 'hash'; |
|
33
|
|
|
|
|
34
|
|
|
public $incrementing = false; |
|
35
|
|
|
|
|
36
|
|
|
protected $guarded = ['*']; |
|
37
|
|
|
|
|
38
|
|
|
protected $dates = ['deleted_at']; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
|
42
|
|
|
* @param string $type |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Illuminate\Database\Query\Builder |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
|
|
public function scopeOfType($query, $type = 'image') |
|
48
|
|
|
{ |
|
49
|
|
|
if (!in_array($type, ['plain', 'image', 'audio', 'video', 'application'])) { |
|
50
|
|
|
throw new \Exception(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $query->where('mime', 'like', $type . '/%'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
58
|
|
|
*/ |
|
59
|
|
|
public function uploaders() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->belongsToMany(User::class, 'files_users', 'file_hash', 'user_id')->withTimestamps()->withPivot(['uuid', 'original_client_name']); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|