PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.01
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Models / Media.php

Media.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.01, at app/Models/Media.php

184 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @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
35 */
36 class Media extends Model
37 {
38 protected $table = 'fcom_media_archive';
39
40 protected $primaryKey = 'id';
41
42 protected $guarded = [ 'id' ];
43
44 protected $appends = [ 'public_url' ];
45
46 protected $fillable = [
47 'object_source',
48 'user_id',
49 'media_key',
50 'feed_id',
51 'is_active',
52 'sub_object_id',
53 'media_type',
54 'driver',
55 'media_path',
56 'media_url',
57 'settings',
58 ];
59
60 public static function boot()
61 {
62 parent::boot();
63
64 static::creating(function ($model) {
65 if (!isset($model->user_id)) {
66 $model->user_id = get_current_user_id();
67 }
68
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());
74 });
75
76 static::deleting(function ($media) {
77 $media->deleteFile();
78 });
79 }
80
81 public function scopeBySource($query, $sources = [])
82 {
83 if (!empty($sources)) {
84 $query->whereIn('object_source', $sources);
85 }
86
87 return $query;
88 }
89
90 public function scopeByMediaKey($query, $key)
91 {
92 return $query->where('media_key', $key);
93 }
94
95 public function scopeByUser($query, $userId)
96 {
97 if ($userId) {
98 return $query->where('user_id', $userId);
99 }
100
101 return $query;
102 }
103
104 public function setSettingsAttribute($value)
105 {
106 $this->attributes['settings'] = maybe_serialize($value);
107 }
108
109 public function getSettingsAttribute($value)
110 {
111 $settings = Utility::safeUnserialize($value);
112
113 if (!$settings) {
114 $settings = [];
115 }
116
117 return $settings;
118 }
119
120
121 public function feed()
122 {
123 return $this->belongsTo(Feed::class, 'feed_id');
124 }
125
126 public function user()
127 {
128 return $this->belongsTo(User::class, 'user_id');
129 }
130
131 public function xprofile()
132 {
133 return $this->belongsTo(XProfile::class, 'user_id', 'user_id');
134 }
135
136 public function getPublicUrlAttribute()
137 {
138 return apply_filters('fluent_community/media_public_url_' . $this->driver, $this->media_url, $this);
139 }
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
146 public function deleteFile()
147 {
148 if ($this->driver == 'local') {
149 if (file_exists($this->media_path)) {
150 wp_delete_file($this->media_path);
151 }
152 } else {
153 do_action('fluent_community/delete_remote_media_' . $this->driver, $this);
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 ];
182 }
183 }
184