@@ -28,7 +28,7 @@ class Server
28
28
29
29
/**
30
30
* The cache file system.
31
- * @var FilesystemInterface|null
31
+ * @var FilesystemInterface
32
32
*/
33
33
protected $ cache ;
34
34
@@ -52,11 +52,11 @@ class Server
52
52
53
53
/**
54
54
* 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.
58
58
*/
59
- public function __construct (FilesystemInterface $ source , FilesystemInterface $ cache = null , ApiInterface $ api )
59
+ public function __construct (FilesystemInterface $ source , FilesystemInterface $ cache , ApiInterface $ api )
60
60
{
61
61
$ this ->setSource ($ source );
62
62
$ this ->setCache ($ cache );
@@ -140,16 +140,16 @@ public function sourceFileExists()
140
140
141
141
/**
142
142
* Set the cache file system.
143
- * @param FilesystemInterface|null $cache The cache file system.
143
+ * @param FilesystemInterface $cache The cache file system.
144
144
*/
145
- public function setCache (FilesystemInterface $ cache = null )
145
+ public function setCache (FilesystemInterface $ cache )
146
146
{
147
147
$ this ->cache = $ cache ;
148
148
}
149
149
150
150
/**
151
151
* Get the cache file system.
152
- * @return FilesystemInterface|null The cache file system.
152
+ * @return FilesystemInterface The cache file system.
153
153
*/
154
154
public function getCache ()
155
155
{
@@ -199,10 +199,6 @@ public function getCachePath()
199
199
*/
200
200
public function cacheFileExists ()
201
201
{
202
- if ($ this ->cache === null ) {
203
- return false ;
204
- }
205
-
206
202
$ request = (new RequestArgumentsResolver ())->getRequest (func_get_args ());
207
203
208
204
return $ this ->cache ->has ($ this ->getCachePath ($ request ));
@@ -255,12 +251,6 @@ public function outputImage()
255
251
256
252
$ this ->makeImage ($ request );
257
253
258
- if ($ this ->cache === null ) {
259
- ResponseFactory::create ($ this ->source , $ request , $ this ->getSourcePath ($ request ))->send ();
260
-
261
- return $ request ;
262
- }
263
-
264
254
ResponseFactory::create ($ this ->cache , $ request , $ this ->getCachePath ($ request ))->send ();
265
255
266
256
return $ request ;
@@ -309,28 +299,24 @@ public function makeImage()
309
299
);
310
300
}
311
301
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 ' );
315
306
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
+ }
318
312
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
- {
327
313
try {
328
- $ written = $ this ->cache ->write (
314
+ $ write = $ this ->cache ->write (
329
315
$ this ->getCachePath ($ request ),
330
- $ this ->api ->run ($ request , $ source )
316
+ $ this ->api ->run ($ request , $ tmp )
331
317
);
332
318
333
- if ($ written === false ) {
319
+ if ($ write === false ) {
334
320
throw new FilesystemException (
335
321
'Could not write the image ` ' .$ this ->getCachePath ($ request ).'`. '
336
322
);
@@ -340,5 +326,9 @@ protected function writeCache(Request $request, $source)
340
326
// because it's currently be written to disk in another
341
327
// request. It's best to just fail silently.
342
328
}
329
+
330
+ unlink ($ tmp );
331
+
332
+ return $ request ;
343
333
}
344
334
}
0 commit comments