oc-resizer-plugin/models/Attachment.php

74 lines
1.7 KiB
PHP

<?php namespace Aweos\Resizer\Models;
use Model;
use Storage;
use Cache;
/**
* Setting Model
*/
class Attachment extends Model
{
use \October\Rain\Database\Traits\Sluggable;
protected $slugs = ['slug' => 'title'];
/**
* @var string The database table used by the model.
*/
public $table = 'responsive_attachments';
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = ['id', 'file_id', 'title', 'slug', 'description'];
/**
* @var array Relations
*/
public $hasOne = [];
public $hasMany = [];
public $belongsTo = [
'source' => [SourceFile::class, 'key' => 'file_id']
];
public $belongsToMany = [];
public $morphTo = [];
public $morphOne = [];
public $morphMany = [];
public $attachOne = [];
public $attachMany = [];
public function getFilenameAttribute() {
return $this->slug.'.'.$this->source->extension;
}
public function getPathAttribute() {
return strtolower($this->slug[0]);
}
public function getVersionPath($w) {
return $this->path.'/'.$this->slug.'-'.$w.'.'.$this->source->extension;
}
public function getVersionRegexAttribute() {
$path = preg_quote($this->path, '/');
$ext = preg_quote($this->source->extension, '/');
$name = preg_quote($this->slug, '/');
return "/^{$path}\/{$name}(-[0-9]+)?(-full)?\.{$ext}$/";
}
public function getUrlAttribute() {
return Storage::disk('uploads')->url($this->source->path);
}
public function beforeDelete() {
Cache::forget('responsive-image-'.$this->id);
}
}