1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2017 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
|
|
|
|
14
|
|
|
namespace BEdita\API\Controller; |
15
|
|
|
|
16
|
|
|
use BEdita\Core\Model\Action\GetEntityAction; |
17
|
|
|
use BEdita\Core\Model\Action\SaveEntityAction; |
18
|
|
|
use Cake\Event\Event; |
19
|
|
|
use Cake\Network\Exception\ForbiddenException; |
20
|
|
|
use Cake\ORM\TableRegistry; |
21
|
|
|
use Cake\Routing\Router; |
22
|
|
|
use Zend\Diactoros\Stream; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Controller for `/streams` endpoint. |
26
|
|
|
* |
27
|
|
|
* @since 4.0.0 |
28
|
|
|
* |
29
|
|
|
* @property \BEdita\Core\Model\Table\StreamsTable $Table |
30
|
|
|
*/ |
31
|
|
|
class StreamsController extends ResourcesController |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
protected $_defaultConfig = [ |
38
|
|
|
'allowedAssociations' => [ |
39
|
|
|
'object' => [], // Descendant types of `media` are automatically added in controller initialization. |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
public $modelClass = 'Streams'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function initialize() |
52
|
|
|
{ |
53
|
|
|
/** @var \BEdita\Core\Model\Table\ObjectTypesTable $ObjectTypes */ |
54
|
|
|
$ObjectTypes = TableRegistry::get('ObjectTypes'); |
55
|
|
|
$allowed = $ObjectTypes->find('list') |
56
|
|
|
->where(['parent_id' => $ObjectTypes->get('media')->id]) |
57
|
|
|
->toList(); |
58
|
|
|
$this->setConfig('allowedAssociations.object', $allowed); |
59
|
|
|
|
60
|
|
|
parent::initialize(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function beforeFilter(Event $event) |
67
|
|
|
{ |
68
|
|
|
if ($this->request->getParam('action') !== 'upload') { |
|
|
|
|
69
|
|
|
return parent::beforeFilter($event); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// avoid that RequestHandler tries to parse body |
73
|
|
|
$this->RequestHandler->setConfig('inputTypeMap', [], false); |
74
|
|
|
|
75
|
|
|
// Decode base64-encoded body. |
76
|
|
|
if ($this->request->getHeaderLine('Content-Transfer-Encoding') === 'base64') { |
77
|
|
|
// Append filter to stream. |
78
|
|
|
$body = $this->request->getBody(); |
79
|
|
|
|
80
|
|
|
$stream = $body->detach(); |
81
|
|
|
stream_filter_append($stream, 'convert.base64-decode', STREAM_FILTER_READ); |
82
|
|
|
|
83
|
|
|
$body = new Stream($stream, 'r'); |
84
|
|
|
$this->request = $this->request->withBody($body); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return parent::beforeFilter($event); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Upload a new stream. |
92
|
|
|
* |
93
|
|
|
* @param string $fileName Original file name. |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function upload($fileName) |
97
|
|
|
{ |
98
|
|
|
$this->request->allowMethod(['post']); |
99
|
|
|
|
100
|
|
|
// Add a new entity. |
101
|
|
|
$entity = $this->Table->newEntity(); |
102
|
|
|
$action = new SaveEntityAction(['table' => $this->Table]); |
103
|
|
|
|
104
|
|
|
$data = [ |
105
|
|
|
'file_name' => $fileName, |
106
|
|
|
'mime_type' => $this->request->contentType(), |
107
|
|
|
'contents' => $this->request->getBody(), |
108
|
|
|
]; |
109
|
|
|
$data = $action(compact('entity', 'data')); |
110
|
|
|
|
111
|
|
|
$action = new GetEntityAction(['table' => $this->Table]); |
112
|
|
|
$data = $action(['primaryKey' => $data->get($this->Table->getPrimaryKey())]); |
113
|
|
|
|
114
|
|
|
$this->response = $this->response |
115
|
|
|
->withStatus(201) |
|
|
|
|
116
|
|
|
->withHeader( |
117
|
|
|
'Location', |
118
|
|
|
Router::url( |
119
|
|
|
[ |
120
|
|
|
'_name' => 'api:resources:resource', |
121
|
|
|
'controller' => $this->name, |
122
|
|
|
'id' => $data->uuid, |
123
|
|
|
], |
124
|
|
|
true |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->set(compact('data')); |
129
|
|
|
$this->set('_serialize', ['data']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
* |
135
|
|
|
* @throws \Cake\Network\Exception\ForbiddenException An exception is thrown on attempts to update existing streams. |
136
|
|
|
*/ |
137
|
|
|
public function resource($id) |
138
|
|
|
{ |
139
|
|
|
if ($this->request->is('patch')) { |
140
|
|
|
throw new ForbiddenException(__d( |
141
|
|
|
'bedita', |
142
|
|
|
'You are not allowed to update existing streams, please delete and re-upload' |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return parent::resource($id); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.