1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2020 ChannelWeb Srl, Chialab Srl |
5
|
|
|
* |
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
12
|
|
|
*/ |
13
|
|
|
namespace BEdita\API\Controller\Component; |
14
|
|
|
|
15
|
|
|
use BEdita\Core\Model\Action\GetEntityAction; |
16
|
|
|
use BEdita\Core\Model\Action\SaveEntityAction; |
17
|
|
|
use Cake\Controller\Component; |
18
|
|
|
use Cake\Datasource\EntityInterface; |
19
|
|
|
use Cake\Datasource\ModelAwareTrait; |
20
|
|
|
use Cake\Event\Event; |
21
|
|
|
use Laminas\Diactoros\Stream; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Handles file upload actions |
25
|
|
|
* |
26
|
|
|
* @since 4.2.0 |
27
|
|
|
* @property \BEdita\Core\Model\Table\StreamsTable $Streams |
28
|
|
|
*/ |
29
|
|
|
class UploadComponent extends Component |
30
|
|
|
{ |
31
|
|
|
use ModelAwareTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
|
|
public function beforeFilter(Event $event): void |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
// avoid that RequestHandler tries to parse body |
39
|
|
|
$this->getController()->RequestHandler->setConfig('inputTypeMap', [], false); |
40
|
|
|
|
41
|
|
|
$request = $this->getController()->getRequest(); |
42
|
|
|
// Decode base64-encoded body. |
43
|
|
|
if ($request->getHeaderLine('Content-Transfer-Encoding') === 'base64') { |
44
|
|
|
// Append filter to stream. |
45
|
|
|
$body = $request->getBody(); |
46
|
|
|
|
47
|
|
|
$stream = $body->detach(); |
48
|
|
|
stream_filter_append($stream, 'convert.base64-decode', STREAM_FILTER_READ); |
49
|
|
|
|
50
|
|
|
$body = new Stream($stream, 'r'); |
51
|
|
|
$this->getController()->setRequest($request->withBody($body)); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Upload a new stream and return entity. |
57
|
|
|
* |
58
|
|
|
* @param string $fileName Original file name. |
59
|
|
|
* @param int|null $objectId Object id. |
60
|
|
|
* @return \Cake\Datasource\EntityInterface |
61
|
|
|
*/ |
62
|
|
|
public function upload($fileName, ?int $objectId = null): EntityInterface |
63
|
|
|
{ |
64
|
|
|
$request = $this->getController()->getRequest(); |
65
|
|
|
$request->allowMethod(['post']); |
66
|
|
|
|
67
|
|
|
$this->loadModel('Streams'); |
68
|
|
|
// Add a new entity. |
69
|
|
|
$entity = $this->Streams->newEntity([]); |
70
|
|
|
$action = new SaveEntityAction(['table' => $this->Streams]); |
71
|
|
|
|
72
|
|
|
$data = [ |
73
|
|
|
'file_name' => $fileName, |
74
|
|
|
'mime_type' => $request->contentType(), |
75
|
|
|
'contents' => $request->getBody(), |
76
|
|
|
]; |
77
|
|
|
$entity->set('object_id', $objectId); |
78
|
|
|
$private = filter_var($request->getQuery('private_url', false), FILTER_VALIDATE_BOOLEAN); |
79
|
|
|
$entity->set('private_url', $private); |
80
|
|
|
$data = $action(compact('entity', 'data')); |
81
|
|
|
$action = new GetEntityAction(['table' => $this->Streams]); |
82
|
|
|
|
83
|
|
|
return $action(['primaryKey' => $data->get($this->Streams->getPrimaryKey())]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.