| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\App\Models; |
| 4 | 4 | |
| 5 | +use FluentCommunity\App\Functions\Utility; | |
| 5 | 6 | use FluentCommunity\App\Models\XProfile; |
| 7 | +use FluentCommunity\App\Services\Helper; | |
| 8 | +use FluentCommunity\Framework\Support\Arr; | |
| 6 | 9 | |
| 7 | 10 | /** |
| 8 | 11 | * Meta Model - DB Model for Notifications table |
| 9 | 12 | * |
| @@ -11,8 +14,25 @@ | ||
| 11 | 14 | * |
| 12 | 15 | * @package FluentCommunity\App\Models |
| 13 | 16 | * |
| 14 | 17 | * @version 1.0.0 |
| 18 | + * | |
| 19 | + * @property int $id | |
| 20 | + * @property string $object_source | |
| 21 | + * @property int|null $user_id | |
| 22 | + * @property string $media_key | |
| 23 | + * @property int|null $feed_id | |
| 24 | + * @property int $is_active | |
| 25 | + * @property int|null $sub_object_id | |
| 26 | + * @property string|null $media_type | |
| 27 | + * @property string|null $driver | |
| 28 | + * @property string|null $media_path | |
| 29 | + * @property string|null $media_url | |
| 30 | + * @property array $settings | |
| 31 | + * @property string|null $created_at | |
| 32 | + * @property string|null $updated_at | |
| 33 | + * @property-read string $public_url | |
| 34 | + * @property string|null $share_url | |
| 15 | 35 | */ |
| 16 | 36 | class Media extends Model |
| 17 | 37 | { |
| 18 | 38 | protected $table = 'fcom_media_archive'; |
| @@ -18,11 +38,11 @@ | ||
| 18 | 38 | protected $table = 'fcom_media_archive'; |
| 19 | 39 | |
| 20 | 40 | protected $primaryKey = 'id'; |
| 21 | 41 | |
| 22 | - protected $guarded = ['id']; | |
| 42 | + protected $guarded = [ 'id' ]; | |
| 23 | 43 | |
| 24 | - protected $appends = ['public_url']; | |
| 44 | + protected $appends = [ 'public_url' ]; | |
| 25 | 45 | |
| 26 | 46 | protected $fillable = [ |
| 27 | 47 | 'object_source', |
| 28 | 48 | 'user_id', |
| @@ -33,9 +53,9 @@ | ||
| 33 | 53 | 'media_type', |
| 34 | 54 | 'driver', |
| 35 | 55 | 'media_path', |
| 36 | 56 | 'media_url', |
| 37 | - 'settings' | |
| 57 | + 'settings', | |
| 38 | 58 | ]; |
| 39 | 59 | |
| 40 | 60 | public static function boot() |
| 41 | 61 | { |
| @@ -45,9 +65,13 @@ | ||
| 45 | 65 | if (!isset($model->user_id)) { |
| 46 | 66 | $model->user_id = get_current_user_id(); |
| 47 | 67 | } |
| 48 | 68 | |
| 49 | - $model->media_key = $model->media_key ?: md5($model->media_url . '_' . time()); | |
| 69 | + if (!isset($model->object_source)) { | |
| 70 | + $model->object_source = ''; | |
| 71 | + } | |
| 72 | + | |
| 73 | + $model->media_key = $model->media_key ? $model->media_key : md5($model->media_url . '_' . time()); | |
| 50 | 74 | }); |
| 51 | 75 | |
| 52 | 76 | static::deleting(function ($media) { |
| 53 | 77 | $media->deleteFile(); |
| @@ -83,9 +107,9 @@ | ||
| 83 | 107 | } |
| 84 | 108 | |
| 85 | 109 | public function getSettingsAttribute($value) |
| 86 | 110 | { |
| 87 | - $settings = maybe_unserialize($value); | |
| 111 | + $settings = Utility::safeUnserialize($value); | |
| 88 | 112 | |
| 89 | 113 | if (!$settings) { |
| 90 | 114 | $settings = []; |
| 91 | 115 | } |
| @@ -113,8 +137,13 @@ | ||
| 113 | 137 | { |
| 114 | 138 | return apply_filters('fluent_community/media_public_url_' . $this->driver, $this->media_url, $this); |
| 115 | 139 | } |
| 116 | 140 | |
| 141 | + public function getSignedPublicUrl($time = 3600) | |
| 142 | + { | |
| 143 | + return apply_filters('fluent_community/media_signed_public_url_' . $this->driver, $this->media_url, $this, $time); | |
| 144 | + } | |
| 145 | + | |
| 117 | 146 | public function deleteFile() |
| 118 | 147 | { |
| 119 | 148 | if ($this->driver == 'local') { |
| 120 | 149 | if (file_exists($this->media_path)) { |
| @@ -122,6 +151,33 @@ | ||
| 122 | 151 | } |
| 123 | 152 | } else { |
| 124 | 153 | do_action('fluent_community/delete_remote_media_' . $this->driver, $this); |
| 125 | 154 | } |
| 155 | + } | |
| 156 | + | |
| 157 | + public function getFileTitle() | |
| 158 | + { | |
| 159 | + $title = Arr::get($this->settings, 'title'); | |
| 160 | + | |
| 161 | + if (!$title) { | |
| 162 | + $title = Arr::get($this->settings, 'original_name') ?? basename($this->media_url); | |
| 163 | + } | |
| 164 | + | |
| 165 | + return $title; | |
| 166 | + } | |
| 167 | + | |
| 168 | + public function getPrivateDownloadUrl() | |
| 169 | + { | |
| 170 | + return Helper::baseUrl('?fcom_action=download_document&media_key=' . $this->media_key . '&media_id=' . $this->id); | |
| 171 | + } | |
| 172 | + | |
| 173 | + public function getPrivateFileMeta() | |
| 174 | + { | |
| 175 | + return [ | |
| 176 | + 'id' => $this->id, | |
| 177 | + 'url' => $this->getPrivateDownloadUrl(), | |
| 178 | + 'media_key' => $this->media_key, | |
| 179 | + 'title' => $this->getFileTitle(), | |
| 180 | + 'type' => $this->media_type, | |
| 181 | + ]; | |
| 126 | 182 | } |
| 127 | 183 | } |