PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.5
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.5
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.7.5, at app/Models/Media.php

180 lines 4.3 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 $model->media_key = $model->media_key ? $model->media_key : md5($model->media_url . '_' . time());
70 });
71
72 static::deleting(function ($media) {
73 $media->deleteFile();
74 });
75 }
76
77 public function scopeBySource($query, $sources = [])
78 {
79 if (!empty($sources)) {
80 $query->whereIn('object_source', $sources);
81 }
82
83 return $query;
84 }
85
86 public function scopeByMediaKey($query, $key)
87 {
88 return $query->where('media_key', $key);
89 }
90
91 public function scopeByUser($query, $userId)
92 {
93 if ($userId) {
94 return $query->where('user_id', $userId);
95 }
96
97 return $query;
98 }
99
100 public function setSettingsAttribute($value)
101 {
102 $this->attributes['settings'] = maybe_serialize($value);
103 }
104
105 public function getSettingsAttribute($value)
106 {
107 $settings = Utility::safeUnserialize($value);
108
109 if (!$settings) {
110 $settings = [];
111 }
112
113 return $settings;
114 }
115
116
117 public function feed()
118 {
119 return $this->belongsTo(Feed::class, 'feed_id');
120 }
121
122 public function user()
123 {
124 return $this->belongsTo(User::class, 'user_id');
125 }
126
127 public function xprofile()
128 {
129 return $this->belongsTo(XProfile::class, 'user_id', 'user_id');
130 }
131
132 public function getPublicUrlAttribute()
133 {
134 return apply_filters('fluent_community/media_public_url_' . $this->driver, $this->media_url, $this);
135 }
136
137 public function getSignedPublicUrl($time = 3600)
138 {
139 return apply_filters('fluent_community/media_signed_public_url_' . $this->driver, $this->media_url, $this, $time);
140 }
141
142 public function deleteFile()
143 {
144 if ($this->driver == 'local') {
145 if (file_exists($this->media_path)) {
146 wp_delete_file($this->media_path);
147 }
148 } else {
149 do_action('fluent_community/delete_remote_media_' . $this->driver, $this);
150 }
151 }
152
153 public function getFileTitle()
154 {
155 $title = Arr::get($this->settings, 'title');
156
157 if (!$title) {
158 $title = Arr::get($this->settings, 'original_name') ?? basename($this->media_url);
159 }
160
161 return $title;
162 }
163
164 public function getPrivateDownloadUrl()
165 {
166 return Helper::baseUrl('?fcom_action=download_document&media_key=' . $this->media_key . '&media_id=' . $this->id);
167 }
168
169 public function getPrivateFileMeta()
170 {
171 return [
172 'id' => $this->id,
173 'url' => $this->getPrivateDownloadUrl(),
174 'media_key' => $this->media_key,
175 'title' => $this->getFileTitle(),
176 'type' => $this->media_type,
177 ];
178 }
179 }
180