1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colligator; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Message\MessageFactory; |
7
|
|
|
use Intervention\Image\ImageManager; |
8
|
|
|
use League\Flysystem\AdapterInterface; |
9
|
|
|
use League\Flysystem\Config as FlysystemConfig; |
10
|
|
|
|
11
|
|
|
class CoverCache |
12
|
|
|
{ |
13
|
|
|
protected $filesystem; |
14
|
|
|
protected $imageManager; |
15
|
|
|
protected $fsConfig; |
16
|
|
|
protected $http; |
17
|
|
|
protected $messageFactory; |
18
|
|
|
|
19
|
|
|
public function __construct( |
20
|
|
|
AdapterInterface $filesystem, |
21
|
|
|
ImageManager $imageManager, |
22
|
|
|
FlysystemConfig $fsConfig, |
23
|
|
|
HttpClient $http, |
24
|
|
|
MessageFactory $messageFactory |
25
|
|
|
) |
26
|
|
|
{ |
27
|
|
|
$this->filesystem = $filesystem; |
28
|
|
|
$this->imageManager = $imageManager; |
29
|
|
|
$this->fsConfig = $fsConfig; |
30
|
|
|
$this->http = $http; |
31
|
|
|
$this->messageFactory = $messageFactory; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $key |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function url($key) |
40
|
|
|
{ |
41
|
|
|
return sprintf('https://s3.%s.amazonaws.com/%s/%s', |
42
|
|
|
config('filesystems.disks.s3.region'), |
43
|
|
|
config('filesystems.disks.s3.bucket'), |
44
|
|
|
$key |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $url |
50
|
|
|
* @param int $maxHeight |
51
|
|
|
* |
52
|
|
|
* @throws \ErrorException |
53
|
|
|
* |
54
|
|
|
* @return CachedImage |
55
|
|
|
*/ |
56
|
|
|
public function putUrl($url, $maxHeight = 0) |
57
|
|
|
{ |
58
|
|
|
return $this->store($url, null, $maxHeight); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $blob |
63
|
|
|
* @param int $maxHeight |
64
|
|
|
* |
65
|
|
|
* @throws \ErrorException |
66
|
|
|
* |
67
|
|
|
* @return CachedImage |
68
|
|
|
*/ |
69
|
|
|
public function putBlob($blob, $maxHeight = 0) |
70
|
|
|
{ |
71
|
|
|
return $this->store(null, $blob, $maxHeight); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retrieves the content of an URL. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function download($sourceUrl) |
80
|
|
|
{ |
81
|
|
|
$request = $this->messageFactory->createRequest('GET', $sourceUrl); |
82
|
|
|
|
83
|
|
|
return (string) $this->http->sendRequest($request)->getBody(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Store a file in cache. |
88
|
|
|
* |
89
|
|
|
* @param string $sourceUrl |
90
|
|
|
* @param string $data |
91
|
|
|
* @param int $maxHeight |
92
|
|
|
* @return CachedImage |
93
|
|
|
* @throws \ErrorException |
94
|
|
|
*/ |
95
|
|
|
protected function store($sourceUrl = null, $data = null, $maxHeight = 0) |
96
|
|
|
{ |
97
|
|
|
if (is_null($data)) { |
98
|
|
|
$data = $this->download($sourceUrl); |
99
|
|
|
if (!$data) { |
100
|
|
|
throw new \ErrorException('[CoverCache] Failed to download ' . $sourceUrl); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$cacheKey = sha1($data); |
105
|
|
|
|
106
|
|
|
$img = $this->imageManager->make($data); |
107
|
|
|
if ($maxHeight && $img->height() > $maxHeight) { |
108
|
|
|
\Log::debug('[CachedImage] Resizing from ' . $img->height() . ' to ' . $maxHeight); |
109
|
|
|
$img->heighten($maxHeight); |
110
|
|
|
$data = strval($img->encode('jpg')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($img->width() / $img->height() > 1.4) { |
114
|
|
|
throw new \ErrorException('[CoverCache] Not accepting images with w/h ratio > 1.4'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
\Log::debug('[CachedImage] Storing image as ' . $img->width() . ' x ' . $img->height() . ', ' . strlen($data) . ' bytes'); |
119
|
|
|
if (!$this->filesystem->write($cacheKey, $data, $this->fsConfig)) { |
120
|
|
|
throw new \ErrorException('[CoverCache] Failed to upload thumb to S3'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
\Log::debug('[CachedImage] Wrote cached version as ' . $cacheKey); |
124
|
|
|
|
125
|
|
|
return new CachedImage($this, $cacheKey); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getMetadata($cacheKey) { |
129
|
|
|
$data = $this->filesystem->read($cacheKey); |
130
|
|
|
$contents = strval($data['contents']); |
131
|
|
|
$img = $this->imageManager->make($contents); |
132
|
|
|
|
133
|
|
|
return [ |
134
|
|
|
'size' => strlen($contents), |
135
|
|
|
'width' => $img->width(), |
136
|
|
|
'height' => $img->height(), |
137
|
|
|
'mime' => $img->mime(), |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return a representation with height no more than $maxHeight. |
143
|
|
|
* |
144
|
|
|
* @param string $maxHeight |
145
|
|
|
* |
146
|
|
|
* @throws \ErrorException |
147
|
|
|
* |
148
|
|
|
* @return CachedImage |
149
|
|
|
*/ |
150
|
|
|
public function thumb($cacheKey, $maxHeight) |
151
|
|
|
{ |
152
|
|
|
$data = $this->filesystem->read($cacheKey); |
153
|
|
|
$blob = strval($data['contents']); |
154
|
|
|
\Log::debug('[CachedImage] Read ' .$cacheKey . ': ' . strlen($blob) . ' bytes'); |
155
|
|
|
|
156
|
|
|
return $this->putBlob($blob, $maxHeight); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|