2018-04-16 01:26:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2024-06-21 09:45:14 +00:00
|
|
|
use App\Util\Media\License;
|
2018-06-14 00:52:42 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2022-03-31 07:04:15 +00:00
|
|
|
use Illuminate\Support\Str;
|
2024-06-21 09:45:14 +00:00
|
|
|
use Storage;
|
2018-04-16 01:26:48 +00:00
|
|
|
|
|
|
|
class Media extends Model
|
|
|
|
{
|
2018-06-14 00:52:42 +00:00
|
|
|
use SoftDeletes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-01-21 14:48:27 +00:00
|
|
|
protected $guarded = [];
|
|
|
|
|
2021-09-04 03:25:19 +00:00
|
|
|
protected $casts = [
|
2023-07-16 10:24:58 +00:00
|
|
|
'srcset' => 'array',
|
2024-06-21 09:45:14 +00:00
|
|
|
'deleted_at' => 'datetime',
|
2021-09-04 03:25:19 +00:00
|
|
|
];
|
|
|
|
|
2019-01-21 19:18:08 +00:00
|
|
|
public function status()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Status::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function profile()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Profile::class);
|
|
|
|
}
|
|
|
|
|
2018-05-20 02:59:59 +00:00
|
|
|
public function url()
|
|
|
|
{
|
2024-06-21 09:45:14 +00:00
|
|
|
if ($this->cdn_url) {
|
2023-02-06 05:49:32 +00:00
|
|
|
// return Storage::disk(config('filesystems.cloud'))->url($this->media_path);
|
|
|
|
return $this->cdn_url;
|
2021-01-18 05:45:40 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 09:45:14 +00:00
|
|
|
if ($this->remote_media && $this->remote_url) {
|
2021-01-18 05:45:40 +00:00
|
|
|
return $this->remote_url;
|
2018-11-04 03:19:22 +00:00
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2021-01-18 05:45:40 +00:00
|
|
|
return url(Storage::url($this->media_path));
|
2018-05-20 02:59:59 +00:00
|
|
|
}
|
2018-07-12 16:39:55 +00:00
|
|
|
|
|
|
|
public function thumbnailUrl()
|
|
|
|
{
|
2024-06-21 09:45:14 +00:00
|
|
|
if ($this->thumbnail_url) {
|
2021-01-18 05:45:40 +00:00
|
|
|
return $this->thumbnail_url;
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:45:14 +00:00
|
|
|
if (! $this->remote_media && $this->thumbnail_path) {
|
2021-01-24 20:30:31 +00:00
|
|
|
return url(Storage::url($this->thumbnail_path));
|
2021-01-18 05:45:40 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 09:45:14 +00:00
|
|
|
if (! $this->thumbnail_path && $this->cdn_url) {
|
2022-10-03 10:23:18 +00:00
|
|
|
return $this->cdn_url;
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:45:14 +00:00
|
|
|
if ($this->media_path && $this->mime && in_array($this->mime, ['image/jpeg', 'image/png'])) {
|
2023-07-16 10:24:58 +00:00
|
|
|
return $this->remote_media || Str::startsWith($this->media_path, 'http') ?
|
2022-03-31 05:50:32 +00:00
|
|
|
$this->media_path :
|
|
|
|
url(Storage::url($this->media_path));
|
2022-03-31 05:43:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 05:45:40 +00:00
|
|
|
return url(Storage::url('public/no-preview.png'));
|
2018-07-12 16:39:55 +00:00
|
|
|
}
|
2018-10-10 01:24:42 +00:00
|
|
|
|
2019-01-21 19:18:08 +00:00
|
|
|
public function thumb()
|
|
|
|
{
|
|
|
|
return $this->thumbnailUrl();
|
|
|
|
}
|
|
|
|
|
2018-10-10 01:24:42 +00:00
|
|
|
public function mimeType()
|
|
|
|
{
|
2024-06-21 09:45:14 +00:00
|
|
|
if (! $this->mime) {
|
2023-07-16 10:24:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-06-21 09:45:14 +00:00
|
|
|
|
2018-10-10 01:24:42 +00:00
|
|
|
return explode('/', $this->mime)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function activityVerb()
|
|
|
|
{
|
|
|
|
$verb = 'Image';
|
|
|
|
switch ($this->mimeType()) {
|
2018-12-12 21:24:52 +00:00
|
|
|
case 'audio':
|
|
|
|
$verb = 'Audio';
|
|
|
|
break;
|
2024-06-21 09:45:14 +00:00
|
|
|
|
2018-10-10 01:24:42 +00:00
|
|
|
case 'image':
|
2018-12-12 21:24:52 +00:00
|
|
|
$verb = 'Image';
|
2018-10-10 01:24:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'video':
|
|
|
|
$verb = 'Video';
|
|
|
|
break;
|
2024-06-21 09:45:14 +00:00
|
|
|
|
2018-10-10 01:24:42 +00:00
|
|
|
default:
|
|
|
|
$verb = 'Document';
|
|
|
|
break;
|
|
|
|
}
|
2024-06-21 09:45:14 +00:00
|
|
|
|
2018-10-10 01:24:42 +00:00
|
|
|
return $verb;
|
|
|
|
}
|
2018-10-16 18:31:29 +00:00
|
|
|
|
|
|
|
public function getMetadata()
|
|
|
|
{
|
|
|
|
return json_decode($this->metadata, true, 3);
|
|
|
|
}
|
2018-11-04 03:19:22 +00:00
|
|
|
|
|
|
|
public function getModel()
|
|
|
|
{
|
2024-06-21 09:45:14 +00:00
|
|
|
if (empty($this->metadata)) {
|
2018-11-04 03:19:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$meta = $this->getMetadata();
|
2024-06-21 09:45:14 +00:00
|
|
|
if ($meta && isset($meta['Model'])) {
|
2018-11-04 03:19:22 +00:00
|
|
|
return $meta['Model'];
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 04:08:03 +00:00
|
|
|
|
|
|
|
public function getLicense()
|
|
|
|
{
|
|
|
|
$license = $this->license;
|
|
|
|
|
2024-06-21 09:45:14 +00:00
|
|
|
if (! $license || strlen($license) > 2 || $license == 1) {
|
2021-04-01 04:08:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:45:14 +00:00
|
|
|
if (! in_array($license, License::keys())) {
|
2021-04-01 04:08:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = License::get()[$license];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $res['id'],
|
|
|
|
'title' => $res['title'],
|
2024-06-21 09:45:14 +00:00
|
|
|
'url' => $res['url'],
|
2021-04-01 04:08:03 +00:00
|
|
|
];
|
|
|
|
}
|
2018-04-16 01:26:48 +00:00
|
|
|
}
|