2017-03-09 22:00:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class Media extends Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'media_endpoint';
|
|
|
|
|
2017-06-11 20:54:10 +01:00
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = ['path'];
|
|
|
|
|
2017-03-09 22:00:29 +00:00
|
|
|
/**
|
|
|
|
* Get the note that owns this media.
|
|
|
|
*/
|
|
|
|
public function note()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Note');
|
|
|
|
}
|
2017-03-10 09:58:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL for an S3 media file.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUrlAttribute()
|
|
|
|
{
|
2017-05-18 15:15:53 +01:00
|
|
|
if (starts_with($this->path, 'https://')) {
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
2017-03-10 11:27:28 +00:00
|
|
|
return config('filesystems.disks.s3.url') . '/' . $this->path;
|
2017-03-10 09:58:35 +00:00
|
|
|
}
|
2017-09-16 11:39:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL for the medium size of an S3 image file.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMediumurlAttribute()
|
|
|
|
{
|
2017-10-07 17:20:22 +01:00
|
|
|
$basename = $this->getBasename($this->path);
|
2017-11-13 09:46:54 +00:00
|
|
|
$extension = $this->getExtension($this->path);
|
2017-09-16 11:39:36 +01:00
|
|
|
|
|
|
|
return config('filesystems.disks.s3.url') . '/' . $basename . '-medium.' . $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL for the small size of an S3 image file.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSmallurlAttribute()
|
|
|
|
{
|
2017-10-07 17:20:22 +01:00
|
|
|
$basename = $this->getBasename($this->path);
|
2017-11-13 09:46:54 +00:00
|
|
|
$extension = $this->getExtension($this->path);
|
2017-10-07 17:20:22 +01:00
|
|
|
|
|
|
|
return config('filesystems.disks.s3.url') . '/' . $basename . '-small.' . $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBasename($path)
|
|
|
|
{
|
|
|
|
$filenameParts = explode('.', $path);
|
2017-11-13 09:46:54 +00:00
|
|
|
|
2017-10-07 17:20:22 +01:00
|
|
|
// the following achieves this data flow
|
2017-09-16 11:39:36 +01:00
|
|
|
// foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
|
|
|
|
$basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
|
|
|
|
return $carry . '.' . $item;
|
|
|
|
}, ''), '.');
|
2017-11-13 09:46:54 +00:00
|
|
|
|
|
|
|
return $basename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getExtension($path);
|
|
|
|
{
|
|
|
|
$parts = explode('.', $path);
|
|
|
|
|
|
|
|
return array_pop($parts);
|
2017-09-16 11:39:36 +01:00
|
|
|
}
|
2017-03-09 22:00:29 +00:00
|
|
|
}
|