Skip to content

Commit 57a10a4

Browse files
committed
Fix bug with orientation not always working, due to EXIF data not being read.
Setup server factory to use temp directory for cache if none is provided.
1 parent 615861a commit 57a10a4

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

src/Server.php

+24-34
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Server
2828

2929
/**
3030
* The cache file system.
31-
* @var FilesystemInterface|null
31+
* @var FilesystemInterface
3232
*/
3333
protected $cache;
3434

@@ -52,11 +52,11 @@ class Server
5252

5353
/**
5454
* Create Server instance.
55-
* @param FilesystemInterface $source The source file system.
56-
* @param FilesystemInterface|null $cache The cache file system.
57-
* @param ApiInterface $api The image manipulation API.
55+
* @param FilesystemInterface $source The source file system.
56+
* @param FilesystemInterface $cache The cache file system.
57+
* @param ApiInterface $api The image manipulation API.
5858
*/
59-
public function __construct(FilesystemInterface $source, FilesystemInterface $cache = null, ApiInterface $api)
59+
public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api)
6060
{
6161
$this->setSource($source);
6262
$this->setCache($cache);
@@ -140,16 +140,16 @@ public function sourceFileExists()
140140

141141
/**
142142
* Set the cache file system.
143-
* @param FilesystemInterface|null $cache The cache file system.
143+
* @param FilesystemInterface $cache The cache file system.
144144
*/
145-
public function setCache(FilesystemInterface $cache = null)
145+
public function setCache(FilesystemInterface $cache)
146146
{
147147
$this->cache = $cache;
148148
}
149149

150150
/**
151151
* Get the cache file system.
152-
* @return FilesystemInterface|null The cache file system.
152+
* @return FilesystemInterface The cache file system.
153153
*/
154154
public function getCache()
155155
{
@@ -199,10 +199,6 @@ public function getCachePath()
199199
*/
200200
public function cacheFileExists()
201201
{
202-
if ($this->cache === null) {
203-
return false;
204-
}
205-
206202
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());
207203

208204
return $this->cache->has($this->getCachePath($request));
@@ -255,12 +251,6 @@ public function outputImage()
255251

256252
$this->makeImage($request);
257253

258-
if ($this->cache === null) {
259-
ResponseFactory::create($this->source, $request, $this->getSourcePath($request))->send();
260-
261-
return $request;
262-
}
263-
264254
ResponseFactory::create($this->cache, $request, $this->getCachePath($request))->send();
265255

266256
return $request;
@@ -309,28 +299,24 @@ public function makeImage()
309299
);
310300
}
311301

312-
if ($this->cache) {
313-
$this->writeCache($request, $source);
314-
}
302+
// We need to write the image to the local disk before
303+
// doing any manipulations. This is because EXIF data
304+
// can only be read from an actual file.
305+
$tmp = tempnam(sys_get_temp_dir(), 'Glide');
315306

316-
return $request;
317-
}
307+
if (file_put_contents($tmp, $source) === false) {
308+
throw new FilesystemException(
309+
'Unable to write temp file for `'.$this->getSourcePath($request).'`.'
310+
);
311+
}
318312

319-
/**
320-
* Write the cache file if caching is enabled.
321-
* @param Request $request
322-
* @param string $source
323-
* @return mixed
324-
*/
325-
protected function writeCache(Request $request, $source)
326-
{
327313
try {
328-
$written = $this->cache->write(
314+
$write = $this->cache->write(
329315
$this->getCachePath($request),
330-
$this->api->run($request, $source)
316+
$this->api->run($request, $tmp)
331317
);
332318

333-
if ($written === false) {
319+
if ($write === false) {
334320
throw new FilesystemException(
335321
'Could not write the image `'.$this->getCachePath($request).'`.'
336322
);
@@ -340,5 +326,9 @@ protected function writeCache(Request $request, $source)
340326
// because it's currently be written to disk in another
341327
// request. It's best to just fail silently.
342328
}
329+
330+
unlink($tmp);
331+
332+
return $request;
343333
}
344334
}

src/ServerFactory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ public function getCache()
7979
$cache = $this->config['cache'];
8080
}
8181

82-
if ($cache === false) {
83-
return null;
84-
}
85-
8682
if (is_string($cache)) {
8783
return new Filesystem(new Local($cache));
8884
}
@@ -91,6 +87,10 @@ public function getCache()
9187
return $cache;
9288
}
9389

90+
if (is_null($cache)) {
91+
return new Filesystem(new Local(sys_get_temp_dir()));
92+
}
93+
9494
throw new InvalidArgumentException('Invalid `cache` parameter.');
9595
}
9696

tests/ServerFactoryTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ public function testGetCacheWithLocalPath()
6565
$this->assertInstanceOf('League\Flysystem\FilesystemInterface', $server->getCache());
6666
}
6767

68-
public function testGetCacheWithInvalidParam()
69-
{
70-
$this->setExpectedException('InvalidArgumentException', 'Invalid `cache` parameter.');
71-
72-
$server = new ServerFactory([]);
73-
$server->getCache();
74-
}
75-
7668
public function testGetApi()
7769
{
7870
$server = new ServerFactory([]);

0 commit comments

Comments
 (0)