|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\App\Convert\Api\v2; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\App\Api\Controller; |
|
15
|
|
|
use Balloon\App\Convert\Converter; |
|
16
|
|
|
use Balloon\Filesystem; |
|
17
|
|
|
use Balloon\Filesystem\Node\File; |
|
18
|
|
|
use Balloon\Server; |
|
19
|
|
|
use Micro\Http\Response; |
|
20
|
|
|
use MongoDB\BSON\ObjectId; |
|
21
|
|
|
|
|
22
|
|
|
class Convert extends Controller |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Converter. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Converter |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $converter; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Filesystem. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Filesystem |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $fs; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Converter $converter |
|
42
|
|
|
* @param Server $server |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Converter $converter, Server $server) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->fs = $server->getFilesystem(); |
|
47
|
|
|
$this->converter = $converter; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @api {get} /api/v2/files/:id/convert/supported-formats Get supported formats |
|
52
|
|
|
* @apiVersion 2.0.0 |
|
53
|
|
|
* @apiName getSupportedFormats |
|
54
|
|
|
* @apiGroup App\Convert |
|
55
|
|
|
* @apiPermission none |
|
56
|
|
|
* @apiDescription Get supported file formats to convert to (formats do vary between files) |
|
57
|
|
|
* @apiUse _getNode |
|
58
|
|
|
* |
|
59
|
|
|
* @apiExample (cURL) exmaple: |
|
60
|
|
|
* curl -XGET "https://SERVER/api/v2/files/convert/supported-formats?id=544627ed3c58891f058b4686" |
|
61
|
|
|
* |
|
62
|
|
|
* @apiSuccess {string[]} - List of supported formats |
|
63
|
|
|
* @apiSuccessExample {string} Success-Response: |
|
64
|
|
|
* HTTP/1.1 200 OK |
|
65
|
|
|
* [ |
|
66
|
|
|
* "png", |
|
67
|
|
|
* "jpg", |
|
68
|
|
|
* "tiff" |
|
69
|
|
|
* ] |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $id |
|
72
|
|
|
* @param string $p |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getSupportedFormats(?string $id = null, ?string $p = null): Response |
|
75
|
|
|
{ |
|
76
|
|
|
$file = $this->fs->getNode($id, $p, File::class); |
|
77
|
|
|
$result = $this->converter->getSupportedFormats($file); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
return (new Response())->setCode(200)->setBody($result); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @api {get} /api/v2/files/:id/convert/slaves Get slaves |
|
84
|
|
|
* @apiVersion 2.0.0 |
|
85
|
|
|
* @apiName getSlaves |
|
86
|
|
|
* @apiGroup App\Convert |
|
87
|
|
|
* @apiPermission none |
|
88
|
|
|
* @apiDescription Get existing conversion slaves |
|
89
|
|
|
* @apiUse _getNode |
|
90
|
|
|
* |
|
91
|
|
|
* @apiExample (cURL) exmaple: |
|
92
|
|
|
* curl -XGET "https://SERVER/api/v2/files/convert/slaves?id=544627ed3c58891f058b4686" |
|
93
|
|
|
* |
|
94
|
|
|
* @apiSuccessExample {string} Success-Response: |
|
95
|
|
|
* HTTP/1.1 200 OK |
|
96
|
|
|
* [ |
|
97
|
|
|
* ] |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $id |
|
100
|
|
|
* @param string $p |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getSlaves(?string $id = null, ?string $p = null): Response |
|
103
|
|
|
{ |
|
104
|
|
|
$file = $this->fs->getNode($id, $p, File::class); |
|
105
|
|
|
$body = []; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($this->converter->getSlaves($file) as $slave) { |
|
|
|
|
|
|
108
|
|
|
$element = [ |
|
109
|
|
|
'id' => (string) $slave['_id'], |
|
110
|
|
|
'master' => (string) $slave['master'], |
|
111
|
|
|
'format' => $slave['format'], |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
if (isset($slave['slave'])) { |
|
115
|
|
|
$element['slave'] = (string) $slave['slave']; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$body[] = $element; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return (new Response())->setCode(200)->setBody($body); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @api {post} /api/v2/files/:id/convert/slaves Add new slave |
|
126
|
|
|
* @apiVersion 2.0.0 |
|
127
|
|
|
* @apiName postSlaves |
|
128
|
|
|
* @apiGroup App\Convert |
|
129
|
|
|
* @apiPermission none |
|
130
|
|
|
* @apiDescription Add new conversion slave |
|
131
|
|
|
* @apiUse _getNode |
|
132
|
|
|
* |
|
133
|
|
|
* @apiExample (cURL) exmaple: |
|
134
|
|
|
* curl -XPOST "https://SERVER/api/v2/files/convert/slave?id=544627ed3c58891f058b4686" |
|
135
|
|
|
* |
|
136
|
|
|
* @apiSuccessExample {string} Success-Response: |
|
137
|
|
|
* HTTP/1.1 200 OK |
|
138
|
|
|
* { |
|
139
|
|
|
* "id": "944627ed3c58891f058b468e", |
|
140
|
|
|
* "master": "944627ed3c58891f058b4686", |
|
141
|
|
|
* "format": "png", |
|
142
|
|
|
* } |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $id |
|
145
|
|
|
* @param string $p |
|
146
|
|
|
* @param string $format |
|
147
|
|
|
*/ |
|
148
|
|
|
public function postSlaves(string $format, ?string $id = null, ?string $p = null): Response |
|
149
|
|
|
{ |
|
150
|
|
|
$file = $this->fs->getNode($id, $p, File::class); |
|
151
|
|
|
$id = $this->converter->addSlave($file, $format); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
return (new Response())->setCode(200)->setBody([ |
|
154
|
|
|
'id' => (string) $id, |
|
155
|
|
|
'master' => (string) $file->getId(), |
|
156
|
|
|
'format' => $format, |
|
157
|
|
|
]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @api {delete} /api/v2/files/:id/convert/slaves Delete slave |
|
162
|
|
|
* @apiVersion 2.0.0 |
|
163
|
|
|
* @apiName deleteSlaves |
|
164
|
|
|
* @apiGroup App\Convert |
|
165
|
|
|
* @apiPermission none |
|
166
|
|
|
* @apiDescription Delete conversion slave |
|
167
|
|
|
* @apiUse _getNode |
|
168
|
|
|
* |
|
169
|
|
|
* @apiExample (cURL) exmaple: |
|
170
|
|
|
* curl -XDELETE "https://SERVER/api/v2/files/convert/slave?id=544627ed3c58891f058b4686" |
|
171
|
|
|
* |
|
172
|
|
|
* @apiSuccessExample {string} Success-Response: |
|
173
|
|
|
* HTTP/1.1 204 No Content |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $id |
|
176
|
|
|
* @param string $p |
|
|
|
|
|
|
177
|
|
|
* @param string $slave |
|
|
|
|
|
|
178
|
|
|
* @param bool $node |
|
179
|
|
|
*/ |
|
180
|
|
|
public function deleteSlaves(ObjectId $id, bool $node = false): Response |
|
181
|
|
|
{ |
|
182
|
|
|
$this->converter->deleteSlave($id, $node); |
|
183
|
|
|
|
|
184
|
|
|
return (new Response())->setCode(204); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.