1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colligator; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
|
7
|
|
|
class Cover extends Model |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Default thumbnail height. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
public $defaultThumbHeight = 600; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The attributes that should be visible in arrays. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $visible = ['url', 'cached', 'thumb', 'created', 'modified']; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The accessors to append to the model's array form. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $appends = ['cached', 'thumb', 'created', 'modified']; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The attributes that are mass assignable. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $fillable = ['document_id', 'url']; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The document the cover belongs to. |
39
|
|
|
* |
40
|
|
|
* @return Document |
41
|
|
|
*/ |
42
|
|
|
public function document() |
43
|
|
|
{ |
44
|
|
|
return $this->belongsTo('Colligator\Document'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the URL to the cached image. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getCachedAttribute() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
'url' => \CoverCache::url($this->cache_key), |
56
|
|
|
'width' => $this->width, |
57
|
|
|
'height' => $this->height, |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get date part of ISO date-time |
63
|
|
|
*/ |
64
|
|
|
public function getCreatedAttribute() |
65
|
|
|
{ |
66
|
|
|
return isset($this->created_at) ? substr($this->created_at, 0, 10) : null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get date part of ISO date-time |
71
|
|
|
*/ |
72
|
|
|
public function getModifiedAttribute() |
73
|
|
|
{ |
74
|
|
|
return isset($this->modified_at) ? substr($this->modified_at, 0, 10) : null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the URL to the cached thumb image. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getThumbAttribute() |
83
|
|
|
{ |
84
|
|
|
return !is_null($this->thumb_key) ? [ |
85
|
|
|
'url' => \CoverCache::url($this->thumb_key), |
86
|
|
|
'width' => $this->thumb_width, |
87
|
|
|
'height' => $this->thumb_height, |
88
|
|
|
] : $this->cached; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Invalidate cache. |
93
|
|
|
*/ |
94
|
|
|
public function invalidateCache() |
95
|
|
|
{ |
96
|
|
|
if (!is_null($this->width) && !is_null($this->height)) { |
97
|
|
|
\Log::debug('[Cover] Invalidating cache. Old version was ' . $this->width . ' x ' . $this->height); |
98
|
|
|
} |
99
|
|
|
$this->width = null; |
100
|
|
|
$this->height = null; |
101
|
|
|
$this->cache_key = null; |
102
|
|
|
$this->thumb_width = null; |
103
|
|
|
$this->thumb_height = null; |
104
|
|
|
$this->thumb_key = null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Mutuator for the url attribute. Invalidates cache when the paramter changes. |
109
|
|
|
* |
110
|
|
|
* @param $value |
111
|
|
|
*/ |
112
|
|
|
public function setUrlAttribute($value) |
113
|
|
|
{ |
114
|
|
|
if (array_get($this->attributes, 'url') == $value) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
$this->attributes['url'] = $value; |
118
|
|
|
$this->invalidateCache(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Checks if the cover is cached. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function isCached() |
127
|
|
|
{ |
128
|
|
|
return !is_null($this->width); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Cache the cover and create thumbnail. |
133
|
|
|
* |
134
|
|
|
* @throws \ErrorException |
135
|
|
|
*/ |
136
|
|
|
public function cache($blob = null) |
137
|
|
|
{ |
138
|
|
|
if (is_null($blob) && !isset($this->url)) { |
139
|
|
|
throw new \ErrorException('[Cover] Cannot cache when no URL or blob is set.'); |
140
|
|
|
} |
141
|
|
|
if ($this->isCached()) { |
142
|
|
|
\Log::debug('[Cover] Already cached'); |
143
|
|
|
|
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (!is_null($blob)) { |
148
|
|
|
\Log::debug('[Cover] Add to cache from blob'); |
149
|
|
|
$orig = \CoverCache::putBlob($blob); |
150
|
|
|
|
151
|
|
|
} else { |
152
|
|
|
\Log::debug('[Cover] Add to cache from url: ' . $this->url); |
153
|
|
|
$orig = \CoverCache::putUrl($this->url); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
$this->width = $orig->width(); |
158
|
|
|
$this->height = $orig->height(); |
159
|
|
|
$this->mime = $orig->mime(); |
160
|
|
|
$this->cache_key = $orig->cacheKey; |
161
|
|
|
|
162
|
|
|
if ($orig->height() > $this->defaultThumbHeight) { |
163
|
|
|
\Log::debug("[Cover] Making cover thumbnail since height exceeds {$this->defaultThumbHeight}px"); |
164
|
|
|
|
165
|
|
|
$thumb = \CoverCache::thumb($orig->cacheKey, $this->defaultThumbHeight); |
166
|
|
|
$this->thumb_width = $thumb->width(); |
167
|
|
|
$this->thumb_height = $thumb->height(); |
168
|
|
|
$this->thumb_key = $thumb->cacheKey; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|