|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handles REQUEST file specific actions |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\FileBundle\Manager; |
|
7
|
|
|
|
|
8
|
|
|
use Riverline\MultiPartParser\Part; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
17
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
18
|
|
|
* @link http://swisscom.ch |
|
19
|
|
|
*/ |
|
20
|
|
|
class RequestManager |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var RequestStack |
|
24
|
|
|
*/ |
|
25
|
|
|
private $requestStack; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* RequestManager constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param RequestStack $requestStack To get the original request |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(RequestStack $requestStack) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->requestStack = $requestStack; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Simple RAW http request parser. |
|
40
|
|
|
* Ideal for PUT requests where PUT is streamed but you still need the data. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Request $request Sf data request |
|
43
|
|
|
* @return Request |
|
44
|
|
|
*/ |
|
45
|
|
|
public function updateFileRequest(Request $request) |
|
46
|
|
|
{ |
|
47
|
|
|
$original = $this->requestStack->getMasterRequest(); |
|
48
|
|
|
$input = $original ? $original->getContent() : false; |
|
49
|
|
|
|
|
50
|
|
|
if (!$input) { |
|
51
|
|
|
return $request; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$part = new Part((string) $request); |
|
55
|
|
|
|
|
56
|
|
|
if ($part->isMultiPart()) { |
|
57
|
|
|
// do we have metadata? |
|
58
|
|
|
$metadata = $part->getPartsByName('metadata'); |
|
59
|
|
|
if (is_array($metadata) && !empty($metadata)) { |
|
60
|
|
|
$request->request->set('metadata', $metadata[0]->getBody()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// the file itself |
|
64
|
|
|
$upload = $part->getPartsByName('upload'); |
|
65
|
|
|
if (is_array($upload) && !empty($upload)) { |
|
66
|
|
|
$uploadPart = $upload[0]; |
|
67
|
|
|
|
|
68
|
|
|
$file = $this->extractFileFromString( |
|
69
|
|
|
$uploadPart->getBody(), |
|
70
|
|
|
$uploadPart->getFileName() |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$request->files->add([$file]); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
// see if body is json or binary.. |
|
77
|
|
|
$json = json_decode($part->getBody(), true); |
|
78
|
|
|
|
|
79
|
|
|
// Check if content is binary, convert to file upload |
|
80
|
|
|
if (!$json && $request->files->count() == 0) { |
|
81
|
|
|
$file = $this->extractFileFromString($part->getBody()); |
|
82
|
|
|
if ($file) { |
|
83
|
|
|
$request->files->add([$file]); |
|
84
|
|
|
} |
|
85
|
|
|
} elseif ($json) { |
|
86
|
|
|
$request->request->set('metadata', json_encode($json)); |
|
87
|
|
|
} |
|
88
|
|
|
return $request; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $request; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Extracts file data from request content |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $fileContent the file content |
|
98
|
|
|
* @param string $originalFileName an overriding original file name |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @return false|UploadedFile |
|
101
|
|
|
*/ |
|
102
|
|
|
private function extractFileFromString($fileContent, $originalFileName = null) |
|
103
|
|
|
{ |
|
104
|
|
|
$tmpName = $fileName = uniqid(true); |
|
105
|
|
|
|
|
106
|
|
|
if (!is_null($originalFileName)) { |
|
107
|
|
|
$fileName = $originalFileName; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$dir = ini_get('upload_tmp_dir'); |
|
111
|
|
|
$dir = (empty($dir)) ? sys_get_temp_dir() : $dir; |
|
112
|
|
|
$tmpFile = $dir . DIRECTORY_SEPARATOR . $tmpName; |
|
113
|
|
|
|
|
114
|
|
|
// create temporary file; |
|
115
|
|
|
(new Filesystem())->dumpFile($tmpFile, $fileContent); |
|
116
|
|
|
$file = new File($tmpFile); |
|
117
|
|
|
|
|
118
|
|
|
return new UploadedFile( |
|
119
|
|
|
$file->getRealPath(), |
|
120
|
|
|
$fileName, |
|
121
|
|
|
$file->getMimeType(), |
|
122
|
|
|
$file->getSize() |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.