-
Notifications
You must be signed in to change notification settings - Fork 452
/
thumbnailer.php
170 lines (146 loc) · 5.37 KB
/
thumbnailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Concrete\Controller\Backend\File;
use Concrete\Core\Controller\AbstractController;
use Concrete\Core\Entity\File\File;
use Concrete\Core\File\Image\BasicThumbnailer;
use Concrete\Core\File\Image\Thumbnail\AtomicThumbnailStream;
use Concrete\Core\File\Image\Thumbnail\Type\CustomThumbnail;
use Concrete\Core\File\Image\Thumbnail\Type\Version;
use Concrete\Core\File\StorageLocation\StorageLocationInterface;
use Concrete\Core\Http\ResponseFactory;
use Doctrine\ORM\EntityManagerInterface;
class Thumbnailer extends \Concrete\Core\Controller\Controller
{
/** @var \Concrete\Core\Http\ResponseFactory */
private $factory;
/** @var \Concrete\Core\File\Image\Thumbnail\AtomicThumbnailStream */
private $stream;
/** @var \Concrete\Core\File\Image\BasicThumbnailer */
private $thumbnailer;
/** @var \Doctrine\ORM\EntityManagerInterface */
private $entityManager;
public function __construct(AtomicThumbnailStream $stream, ResponseFactory $factory, BasicThumbnailer $thumbnailer, EntityManagerInterface $entityManager)
{
parent::__construct();
$this->stream = $stream;
$this->factory = $factory;
$this->thumbnailer = $thumbnailer;
$this->entityManager = $entityManager;
}
public function generate()
{
$built = false;
$iterator = $this->stream->getIterator();
if ($item = $iterator->current()) {
$this->buildThumbnail($item);
$built = true;
}
return $this->factory->json(['built' => $built, 'path' => $item ? $item['path'] : null]);
}
private function buildThumbnail(array $item)
{
$fileRepository = $this->entityManager->getRepository(File::class);
if (!$file = $fileRepository->findOneBy(['fID' => array_get($item, 'fileID', 0)])) {
return;
}
if ($this->attemptBuild($file, $item)) {
$this->completeBuild($file, $item);
}
}
/**
* Try building an unbuilt thumbnail.
*
* @param \Concrete\Core\Entity\File\File $file
* @param array $thumbnail
*
* @return bool
*/
private function attemptBuild(File $file, array $thumbnail)
{
try {
// If the file is already built, return early
if ($this->isBuilt($file, $thumbnail)) {
return true;
}
// Otherwise lets attempt to build it
if ($dimensions = $this->getDimensions($thumbnail)) {
list($width, $height, $crop) = $dimensions;
$type = new CustomThumbnail($width, $height, $thumbnail['path'], $crop);
$fv = $file->getVersion($thumbnail['fileVersionID']);
if ($fv->getTypeObject()->supportsThumbnails()) {
$fv->generateThumbnail($type);
$fv->releaseImagineImage();
}
} elseif ($type = Version::getByHandle($thumbnail['thumbnailTypeHandle'])) {
// This is a predefined thumbnail type, lets just call the version->rescan
$fv = $file->getVersion($thumbnail['fileVersionID']);
if ($fv->getTypeObject()->supportsThumbnails()) {
$fv->generateThumbnail($type);
$fv->releaseImagineImage();
}
}
} catch (\Exception $e) {
// Catch any exceptions so we don't break the page and return false
return false;
}
return $this->isBuilt($file, $thumbnail);
}
/**
* Mark the build complete.
*
* @param $file
* @param $thumbnail
*/
private function completeBuild(File $file, $thumbnail)
{
// Update the database to have "1" for isBuilt
$this->entityManager->getConnection()->update('FileImageThumbnailPaths', ['isBuilt' => '1'], $thumbnail);
}
/**
* Get the dimensions out of a thumbnail array.
*
* @param array $thumbnail
*
* @return null|array [ width, height, crop ]
*/
private function getDimensions($thumbnail)
{
$matches = null;
if (preg_match('/^ccm_(\d+)x(\d+)(?:_([10]))?$/', $thumbnail['thumbnailTypeHandle'], $matches)) {
return array_pad(array_slice($matches, 1), 3, 0);
}
}
/**
* @param \Concrete\Core\Entity\File\File $file
* @param array $thumbnail
*
* @return bool
*/
private function isBuilt(File $file, $thumbnail)
{
$path = $thumbnail['path'];
$location = $this->storageLocation();
if ($path) {
return
// If the thumbnailer's storage location has the path
($location && $location->getFileSystemObject()->has($path)) ||
// Or the file's storage location has the path
$file->getFileStorageLocationObject()->getFileSystemObject()->has($path);
}
return false;
}
/**
* @return null|StorageLocationInterface
*/
private function storageLocation()
{
$thumbnailer = $this->thumbnailer;
if (method_exists($thumbnailer, 'getStorageLocation')) {
$thumbnailerLocation = $thumbnailer->getStorageLocation();
if ($thumbnailerLocation instanceof StorageLocationInterface) {
return $thumbnailerLocation;
}
}
return null;
}
}