| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\XProfile; |
| 7 |
use FluentCommunity\App\Services\Helper; |
| 8 |
use FluentCommunity\Framework\Support\Arr; |
| 9 |
|
| 10 |
/** |
| 11 |
* Meta Model - DB Model for Notifications table |
| 12 |
* |
| 13 |
* Database Model |
| 14 |
* |
| 15 |
* @package FluentCommunity\App\Models |
| 16 |
* |
| 17 |
* @version 1.0.0 |
| 18 |
*/ |
| 19 |
class Media extends Model |
| 20 |
{ |
| 21 |
protected $table = 'fcom_media_archive'; |
| 22 |
|
| 23 |
protected $primaryKey = 'id'; |
| 24 |
|
| 25 |
protected $guarded = ['id']; |
| 26 |
|
| 27 |
protected $appends = ['public_url']; |
| 28 |
|
| 29 |
protected $fillable = [ |
| 30 |
'object_source', |
| 31 |
'user_id', |
| 32 |
'media_key', |
| 33 |
'feed_id', |
| 34 |
'is_active', |
| 35 |
'sub_object_id', |
| 36 |
'media_type', |
| 37 |
'driver', |
| 38 |
'media_path', |
| 39 |
'media_url', |
| 40 |
'settings' |
| 41 |
]; |
| 42 |
|
| 43 |
public static function boot() |
| 44 |
{ |
| 45 |
parent::boot(); |
| 46 |
|
| 47 |
static::creating(function ($model) { |
| 48 |
if (!isset($model->user_id)) { |
| 49 |
$model->user_id = get_current_user_id(); |
| 50 |
} |
| 51 |
|
| 52 |
$model->media_key = $model->media_key ?: md5($model->media_url . '_' . time()); |
| 53 |
}); |
| 54 |
|
| 55 |
static::deleting(function ($media) { |
| 56 |
$media->deleteFile(); |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
public function scopeBySource($query, $sources = []) |
| 61 |
{ |
| 62 |
if (!empty($sources)) { |
| 63 |
$query->whereIn('object_source', $sources); |
| 64 |
} |
| 65 |
|
| 66 |
return $query; |
| 67 |
} |
| 68 |
|
| 69 |
public function scopeByMediaKey($query, $key) |
| 70 |
{ |
| 71 |
return $query->where('media_key', $key); |
| 72 |
} |
| 73 |
|
| 74 |
public function scopeByUser($query, $userId) |
| 75 |
{ |
| 76 |
if ($userId) { |
| 77 |
return $query->where('user_id', $userId); |
| 78 |
} |
| 79 |
|
| 80 |
return $query; |
| 81 |
} |
| 82 |
|
| 83 |
public function setSettingsAttribute($value) |
| 84 |
{ |
| 85 |
$this->attributes['settings'] = maybe_serialize($value); |
| 86 |
} |
| 87 |
|
| 88 |
public function getSettingsAttribute($value) |
| 89 |
{ |
| 90 |
$settings = Utility::safeUnserialize($value); |
| 91 |
|
| 92 |
if (!$settings) { |
| 93 |
$settings = []; |
| 94 |
} |
| 95 |
|
| 96 |
return $settings; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
public function feed() |
| 101 |
{ |
| 102 |
return $this->belongsTo(Feed::class, 'feed_id'); |
| 103 |
} |
| 104 |
|
| 105 |
public function user() |
| 106 |
{ |
| 107 |
return $this->belongsTo(User::class, 'user_id'); |
| 108 |
} |
| 109 |
|
| 110 |
public function xprofile() |
| 111 |
{ |
| 112 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 113 |
} |
| 114 |
|
| 115 |
public function getPublicUrlAttribute() |
| 116 |
{ |
| 117 |
return apply_filters('fluent_community/media_public_url_' . $this->driver, $this->media_url, $this); |
| 118 |
} |
| 119 |
|
| 120 |
public function getSignedPublicUrl($time = 3600) |
| 121 |
{ |
| 122 |
return apply_filters('fluent_community/media_signed_public_url_' . $this->driver, $this->media_url, $this, $time); |
| 123 |
} |
| 124 |
|
| 125 |
public function deleteFile() |
| 126 |
{ |
| 127 |
if ($this->driver == 'local') { |
| 128 |
if (file_exists($this->media_path)) { |
| 129 |
wp_delete_file($this->media_path); |
| 130 |
} |
| 131 |
} else { |
| 132 |
do_action('fluent_community/delete_remote_media_' . $this->driver, $this); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
public function getFileTitle() |
| 137 |
{ |
| 138 |
$title = Arr::get($this->settings, 'title'); |
| 139 |
|
| 140 |
if (!$title) { |
| 141 |
$title = Arr::get($this->settings, 'original_name') ?? basename($this->media_url); |
| 142 |
} |
| 143 |
|
| 144 |
return $title; |
| 145 |
} |
| 146 |
|
| 147 |
public function getPrivateDownloadUrl() |
| 148 |
{ |
| 149 |
return Helper::baseUrl('?fcom_action=download_document&media_key=' . $this->media_key . '&media_id=' . $this->id); |
| 150 |
} |
| 151 |
|
| 152 |
public function getPrivateFileMeta() |
| 153 |
{ |
| 154 |
return [ |
| 155 |
'id' => $this->id, |
| 156 |
'url' => $this->getPrivateDownloadUrl(), |
| 157 |
'media_key' => $this->media_key, |
| 158 |
'title' => $this->getFileTitle(), |
| 159 |
'type' => $this->media_type |
| 160 |
]; |
| 161 |
} |
| 162 |
} |
| 163 |
|