1 | <?php |
||||||
2 | /** |
||||||
3 | * AssetPond plugin for Craft CMS 3.x |
||||||
4 | * |
||||||
5 | * Instant FilePond server that works with Craft Assets. |
||||||
6 | * |
||||||
7 | * @link https://workingconcept.com |
||||||
8 | * @copyright Copyright (c) 2019 Working Concept |
||||||
9 | */ |
||||||
10 | |||||||
11 | namespace workingconcept\assetpond\controllers; |
||||||
12 | |||||||
13 | use workingconcept\assetpond\AssetPond; |
||||||
14 | |||||||
15 | use Craft; |
||||||
16 | use craft\elements\Asset; |
||||||
17 | use craft\web\Controller; |
||||||
18 | use workingconcept\assetpond\helpers\PostHelper; |
||||||
19 | use yii\web\HttpException; |
||||||
20 | |||||||
21 | /** |
||||||
22 | * @author Working Concept |
||||||
23 | * @package AssetPond |
||||||
24 | * @since 1.0.0 |
||||||
25 | */ |
||||||
26 | class DefaultController extends Controller |
||||||
27 | { |
||||||
28 | |||||||
29 | // Protected Properties |
||||||
30 | // ========================================================================= |
||||||
31 | |||||||
32 | protected $allowAnonymous = ['index']; |
||||||
33 | |||||||
34 | |||||||
35 | // Protected Properties |
||||||
36 | // ========================================================================= |
||||||
37 | |||||||
38 | public $enableCsrfValidation = false; |
||||||
39 | |||||||
40 | |||||||
41 | // Public Methods |
||||||
42 | // ========================================================================= |
||||||
43 | |||||||
44 | /** |
||||||
45 | * @param $volumeId int optional upload target volume |
||||||
46 | * |
||||||
47 | * @return mixed |
||||||
48 | * @throws |
||||||
49 | */ |
||||||
50 | public function actionIndex($volumeId = null) |
||||||
51 | { |
||||||
52 | return $this->_handleApiRequest($volumeId); |
||||||
53 | } |
||||||
54 | |||||||
55 | |||||||
56 | // Private Methods |
||||||
57 | // ========================================================================= |
||||||
58 | |||||||
59 | /** |
||||||
60 | * @param int $volumeId |
||||||
61 | * |
||||||
62 | * @return \yii\web\Response |
||||||
63 | * @throws |
||||||
64 | */ |
||||||
65 | private function _handleApiRequest($volumeId = null) |
||||||
66 | { |
||||||
67 | $inputs = ['filepond']; // TODO: allow changing input name |
||||||
68 | |||||||
69 | // TODO: allow multiple inputs/fields |
||||||
70 | |||||||
71 | $request = Craft::$app->getRequest(); |
||||||
72 | $requestMethod = $request->getMethod(); |
||||||
73 | $server = AssetPond::$plugin->server; |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||
74 | |||||||
75 | // turn a single item into an array for uniformity |
||||||
76 | if (is_string($inputs)) |
||||||
0 ignored issues
–
show
|
|||||||
77 | { |
||||||
78 | $inputs = [ $inputs ]; |
||||||
79 | } |
||||||
80 | |||||||
81 | // loop over all set entry fields to find posted values |
||||||
82 | foreach ($inputs as $input) |
||||||
83 | { |
||||||
84 | if ($requestMethod === 'POST') |
||||||
85 | { |
||||||
86 | if ( ! $this->_hasPostContents($input)) |
||||||
87 | { |
||||||
88 | // bail on this item if we don't have anything to work with |
||||||
89 | continue; |
||||||
90 | } |
||||||
91 | |||||||
92 | if ( ! $files = $this->_getFiles($input)) |
||||||
93 | { |
||||||
94 | // bail on this item if we don't have any files |
||||||
95 | continue; |
||||||
96 | } |
||||||
97 | |||||||
98 | // create Assets |
||||||
99 | $result = $server->handleFileTransfer( |
||||||
0 ignored issues
–
show
The method
handleFileTransfer() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
100 | $files, |
||||||
101 | $this->_getMeta($input), |
||||||
102 | $volumeId |
||||||
103 | ); |
||||||
104 | |||||||
105 | if ($result) |
||||||
106 | { |
||||||
107 | // return plain text Asset ID |
||||||
108 | return $this->asRaw($result); |
||||||
109 | } |
||||||
110 | } |
||||||
111 | |||||||
112 | if ($requestMethod === 'DELETE') |
||||||
113 | { |
||||||
114 | // delete Asset |
||||||
115 | return $this->asRaw( |
||||||
116 | $server->handleRevertFileTransfer( |
||||||
117 | $request->getRawBody() |
||||||
118 | ) |
||||||
119 | ); |
||||||
120 | } |
||||||
121 | |||||||
122 | if ($requestMethod === 'GET' || $requestMethod === 'HEAD') |
||||||
123 | { |
||||||
124 | // if (isset($_GET['fetch'])) |
||||||
125 | // { |
||||||
126 | // return $server->handle_fetch_remote_file(); |
||||||
127 | // } |
||||||
128 | |||||||
129 | if ( |
||||||
130 | isset($_GET['restore']) && |
||||||
131 | $result = $server->handleRestoreFileTransfer($_GET['restore']) |
||||||
132 | ) |
||||||
133 | { |
||||||
134 | return $this->_echoAsset($result); |
||||||
0 ignored issues
–
show
|
|||||||
135 | } |
||||||
136 | |||||||
137 | if ( |
||||||
138 | isset($_GET['load']) && |
||||||
139 | $result = $server->handleLoadLocalFile($_GET['load']) |
||||||
140 | ) |
||||||
141 | { |
||||||
142 | return $this->_echoAsset($result); |
||||||
0 ignored issues
–
show
|
|||||||
143 | } |
||||||
144 | } |
||||||
145 | } |
||||||
146 | } |
||||||
147 | |||||||
148 | // TODO: all of it |
||||||
149 | private function _handleFormPost() |
||||||
0 ignored issues
–
show
|
|||||||
150 | { |
||||||
151 | // see submit.php |
||||||
152 | // 'FILE_OBJECTS' => 'handle_file_post', |
||||||
153 | // 'BASE64_ENCODED_FILE_OBJECTS' => 'handle_base64_encoded_file_post', |
||||||
154 | // 'TRANSFER_IDS' => 'handle_transfer_ids_post' |
||||||
155 | } |
||||||
156 | |||||||
157 | /** |
||||||
158 | * Returns true if either binary or modeled file data exists. |
||||||
159 | * |
||||||
160 | * @param $input |
||||||
161 | * @return bool |
||||||
162 | */ |
||||||
163 | private function _hasPostContents($input) |
||||||
164 | { |
||||||
165 | return isset($_FILES[$input]) || isset($_POST[$input]); |
||||||
166 | } |
||||||
167 | |||||||
168 | /** |
||||||
169 | * Returns an array of file objects. (See PostHelper.) |
||||||
170 | * |
||||||
171 | * @param $input |
||||||
172 | * @return array |
||||||
173 | */ |
||||||
174 | private function _getFiles($input) |
||||||
175 | { |
||||||
176 | return isset($_FILES[$input]) ? |
||||||
177 | PostHelper::toArrayOfFiles($_FILES[$input]) : |
||||||
178 | []; |
||||||
179 | } |
||||||
180 | |||||||
181 | /** |
||||||
182 | * @param $input |
||||||
183 | * @return array |
||||||
184 | */ |
||||||
185 | private function _getMeta($input) |
||||||
186 | { |
||||||
187 | return isset($_POST[$input]) ? |
||||||
188 | PostHelper::toArray($_POST[$input]) : |
||||||
189 | []; |
||||||
190 | } |
||||||
191 | |||||||
192 | /** |
||||||
193 | * Returns an HTTP response with the raw file contents. |
||||||
194 | * |
||||||
195 | * @param Asset $asset |
||||||
196 | * @return \craft\web\Response|\yii\console\Response |
||||||
197 | * @throws HttpException |
||||||
198 | * @throws \yii\web\RangeNotSatisfiableHttpException |
||||||
199 | */ |
||||||
200 | private function _echoAsset($asset) |
||||||
201 | { |
||||||
202 | return Craft::$app->getResponse()->sendContentAsFile( |
||||||
203 | $asset->getContents(), |
||||||
204 | $asset->filename, |
||||||
205 | [ |
||||||
206 | 'inline' => true, |
||||||
207 | 'mimeType' => $asset->mimeType, |
||||||
208 | 'contentLength' => $asset->size |
||||||
209 | ] |
||||||
210 | ); |
||||||
211 | } |
||||||
212 | |||||||
213 | } |
||||||
214 |