1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Renderer; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\Result; |
15
|
|
|
use Cake\Core\Configure; |
16
|
|
|
use Cake\Utility\Hash; |
17
|
|
|
use Cake\Http\Response; |
18
|
|
|
use Cake\Log\LogTrait; |
19
|
|
|
use Exception; |
20
|
|
|
use League\Flysystem\File; |
21
|
|
|
use League\Flysystem\FileNotFoundException; |
22
|
|
|
use League\Flysystem\Filesystem; |
23
|
|
|
use Zend\Diactoros\Stream; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class FlysystemRenderer to render file using Flysystem library |
27
|
|
|
*/ |
28
|
|
|
class FlysystemRenderer extends FileRenderer |
29
|
|
|
{ |
30
|
|
|
use LogTrait; |
31
|
|
|
/** |
32
|
|
|
* Builds the HTTP response. |
33
|
|
|
* |
34
|
|
|
* @param Result $result The result object returned by the Service. |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
2 |
|
public function response(Result $result = null) |
38
|
|
|
{ |
39
|
2 |
|
$data = $result->getData(); |
|
|
|
|
40
|
|
|
try { |
41
|
2 |
|
$file = $this->getFile( |
42
|
2 |
|
Hash::get($data, 'filesystem'), |
43
|
2 |
|
Hash::get($data, 'path') |
44
|
2 |
|
); |
45
|
|
|
|
46
|
1 |
|
$this->_service->setResponse( |
47
|
1 |
|
$this->deliverAsset($this->_service->getResponse(), $file) |
48
|
1 |
|
); |
49
|
2 |
|
} catch (FileNotFoundException $e) { |
50
|
1 |
|
$response = $this->_service->getResponse() |
51
|
1 |
|
->withStatus(404); |
52
|
|
|
|
53
|
1 |
|
$this->_service->setResponse($response); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get flystem file object |
61
|
|
|
* |
62
|
|
|
* @param Filesystem $filesystem custom filesystem |
63
|
|
|
* @param string $path of file at filesystem |
64
|
|
|
* @return File |
65
|
|
|
*/ |
66
|
2 |
|
protected function getFile(Filesystem $filesystem, $path) |
67
|
|
|
{ |
68
|
2 |
|
return $filesystem->get($path); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Deliver the asset stream in body |
73
|
|
|
* |
74
|
|
|
* @param Response $response service response |
75
|
|
|
* @param File $file file object |
76
|
|
|
* @return Response |
77
|
|
|
*/ |
78
|
1 |
|
public function deliverAsset(Response $response, File $file) |
79
|
|
|
{ |
80
|
1 |
|
$contentType = $file->getType(); |
81
|
1 |
|
$modified = $file->getTimestamp(); |
82
|
1 |
|
$expire = strtotime(Configure::write('Api.Flysystem.expire')); |
83
|
1 |
|
$maxAge = $expire - time(); |
84
|
1 |
|
$stream = new Stream($file->readStream()); |
|
|
|
|
85
|
|
|
|
86
|
1 |
|
return $response->withBody($stream) |
87
|
|
|
// Content |
88
|
1 |
|
->withHeader('Content-Type', $contentType) |
89
|
|
|
// Cache |
90
|
1 |
|
->withHeader('Cache-Control', 'public,max-age=' . $maxAge) |
91
|
1 |
|
->withHeader('Date', gmdate('D, j M Y G:i:s \G\M\T', time())) |
92
|
1 |
|
->withHeader('Last-Modified', gmdate('D, j M Y G:i:s \G\M\T', $modified)) |
93
|
1 |
|
->withHeader('Expires', gmdate('D, j M Y G:i:s \G\M\T', $expire)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Handle error setting a response status |
98
|
|
|
* |
99
|
|
|
* @param Exception $exception thrown at service or action |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
2 |
|
public function error(Exception $exception) |
104
|
|
|
{ |
105
|
2 |
|
$code = $exception->getCode(); |
106
|
2 |
|
$response = $this->_service->getResponse() |
107
|
2 |
|
->withStatus($code ? $code : 500); |
108
|
|
|
|
109
|
2 |
|
$this->log($exception); |
110
|
|
|
|
111
|
2 |
|
$this->_service->setResponse($response); |
112
|
2 |
|
} |
113
|
|
|
} |
114
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: