175 lines
5.8 KiB
PHP
175 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Storage;
|
|
use File;
|
|
|
|
/**
|
|
* App\MediaFile
|
|
*
|
|
* @property int $id
|
|
* @property string|null $path 子資料夾路徑
|
|
* @property string $file_name 檔名
|
|
* @property string|null $ext 副檔名
|
|
* @property int|null $disk_id disk名稱
|
|
* @property int|null $media_source_id 外部來源id
|
|
* @property int|null $size 檔案大小
|
|
* @property int|null $width 圖片寬度
|
|
* @property int|null $height 圖片高度
|
|
* @property string|null $mime_type 檔案的Mime Type
|
|
* @property int $is_app_media 是否為網站的媒體檔案
|
|
* @property string|null $description 描述
|
|
* @property int|null $media_category_id 媒體分類
|
|
* @property int|null $user_id 所屬使用者
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \App\Disk|null $disk
|
|
* @property-read mixed $file_name_with_path
|
|
* @property-read mixed $file_path
|
|
* @property-read mixed $url
|
|
* @property-read mixed $youtube_embed_url
|
|
* @property-read \App\MediaCategory|null $mediaCategory
|
|
* @property-read \App\MediaSource|null $mediaSource
|
|
* @property-read \App\User|null $user
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile category($categoryId)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile isAppMedia()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile uncategorized()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile userMedia($userId)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereDescription($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereDiskId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereExt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereFileName($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereHeight($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereIsAppMedia($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereMediaCategoryId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereMediaSourceId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereMimeType($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile wherePath($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereSize($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereUserId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\MediaFile whereWidth($value)
|
|
* @mixin \Eloquent
|
|
*/
|
|
class MediaFile extends Model
|
|
{
|
|
protected $dispatchesEvents = [
|
|
'deleted' => \App\Events\MediaFileDeletedEvent::class
|
|
];
|
|
|
|
protected $fillable = [
|
|
'path',
|
|
'file_name',
|
|
'ext',
|
|
'disk_id',
|
|
'media_source_id',
|
|
'size',
|
|
'width',
|
|
'height',
|
|
'is_app_media',
|
|
'user_id',
|
|
'mime_type',
|
|
'description'
|
|
];
|
|
|
|
protected $hidden = [
|
|
'path',
|
|
'ext',
|
|
'disk_id',
|
|
'disk',
|
|
'is_app_media',
|
|
'user_id'
|
|
];
|
|
|
|
public function getUrlAttribute()
|
|
{
|
|
return Storage::disk($this->disk->name)->url(trim($this->path, '/'). '/' . trim($this->file_name, '/'));
|
|
}
|
|
|
|
public function getFilePathAttribute()
|
|
{
|
|
return Storage::disk($this->disk->name)->path(trim($this->path, '/'). '/' . trim($this->file_name, '/'));
|
|
}
|
|
|
|
public function getFileNameWithPathAttribute()
|
|
{
|
|
return trim(trim($this->path, '/') . '/' . $this->file_name, '/');
|
|
}
|
|
|
|
public function getYoutubeEmbedUrlAttribute()
|
|
{
|
|
$mediaSource = $this->mediaSource;
|
|
if($mediaSource && 'youtube' == $mediaSource->name) {
|
|
return "https://www.youtube.com/embed/$this->file_name";
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* @param \Illuminate\Database\Query\Builder $query
|
|
*
|
|
* @return \Illuminate\Database\Query\Builder
|
|
*/
|
|
public function scopeUncategorized($query)
|
|
{
|
|
return $query->where('media_category_id', null);
|
|
}
|
|
|
|
/**
|
|
* @param \Illuminate\Database\Query\Builder $query
|
|
*
|
|
* @return \Illuminate\Database\Query\Builder
|
|
*/
|
|
public function scopeCategory($query, $categoryId)
|
|
{
|
|
return $query->where('media_category_id', $categoryId);
|
|
}
|
|
|
|
/**
|
|
* @param \Illuminate\Database\Query\Builder $query
|
|
*
|
|
* @return \Illuminate\Database\Query\Builder
|
|
*/
|
|
public function scopeIsAppMedia($query)
|
|
{
|
|
return $query->where('is_app_media', true);
|
|
}
|
|
|
|
/**
|
|
* @param \Illuminate\Database\Query\Builder $query
|
|
*
|
|
* @return \Illuminate\Database\Query\Builder
|
|
*/
|
|
public function scopeUserMedia($query, $userId)
|
|
{
|
|
return $query->where('is_app_media', false)->where('user_id', $userId);
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function disk()
|
|
{
|
|
return $this->belongsTo(Disk::class);
|
|
}
|
|
|
|
public function mediaCategory()
|
|
{
|
|
return $this->belongsTo(MediaCategory::class);
|
|
}
|
|
|
|
public function mediaSource()
|
|
{
|
|
return $this->belongsTo(MediaSource::class);
|
|
}
|
|
}
|