1 <?php
2
3 /* --------------------------------------------------------------
4 CategoryImagesApiV2Controller.inc.php 2016-03-07
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2016 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 MainFactory::load_class('AbstractImagesApiV2Controller');
14
15 /**
16 * Class CategoryImagesApiV2Controller
17 *
18 * This controller handles the Category Image file operations. The clients of the API can list, upload, rename or delete
19 * files that exist in the server.
20 *
21 * @category System
22 * @package ApiV2Controllers
23 */
24 class CategoryImagesApiV2Controller extends AbstractImagesApiV2Controller
25 {
26 /**
27 * Initializes API Controller
28 */
29 protected function __initialize()
30 {
31 $this->writeService = StaticGXCoreLoader::getService('CategoryWrite');
32 }
33
34
35 /**
36 * Returns the absolute path where the image files are located.
37 *
38 * @return string Image folder name.
39 */
40 protected function _getImageFolderName()
41 {
42 return DIR_FS_CATALOG . DIR_WS_IMAGES . 'categories/';
43 }
44
45
46 /**
47 * @api {post} /category_images Upload Category Image
48 * @apiVersion 2.1.0
49 * @apiName UploadCategoryImage
50 * @apiGroup Categories
51 *
52 * @apiDescription
53 * Uploads an icon image for the categories. Make this request without the "Content-Type: application/json". Except
54 * from the file the POST request must also contain a "filename" value with the final file name.
55 *
56 * @apiSuccess (Success 201) Response-Body Contains information about the uploaded file.
57 *
58 * @apiSuccessExample {json} Success-Response
59 * {
60 * "code": 201,
61 * "status": "success",
62 * "action": "upload",
63 * "filename": "my-image-file.png"
64 * }
65 *
66 * @apiError 400-BadRequest No image file was provided or the "filename" parameter was not provided.
67 *
68 * @apiErrorExample Error-Response (Missing image file)
69 * HTTP/1.1 400 Bad Request
70 * {
71 * "code": 400,
72 * "status": "error",
73 * "message": "No image file was provided."
74 * }
75 *
76 * @apiErrorExample Error-Response (Missing filename parameter)
77 * HTTP/1.1 400 Bad Request
78 * {
79 * "code": 400,
80 * "status": "error",
81 * "message": "The 'filename' parameter is required and was not provided with the request."
82 * }
83 *
84 */
85 public function post()
86 {
87 if(!isset($_FILES) || empty($_FILES))
88 {
89 throw new HttpApiV2Exception('No image file was provided.', 400);
90 }
91
92 $file = array_shift($_FILES);
93
94 $existingFile = new ExistingFile(new NonEmptyStringType($file['tmp_name']));
95
96 if($this->api->request->post('filename') === null)
97 {
98 throw new HttpApiV2Exception('The "filename" parameter is required and was not provided with the request. '
99 . 'Check the documentation on how to properly use the API.', 400);
100 }
101
102 $filename = new FilenameStringType($this->api->request->post('filename'));
103
104 $filename = $this->writeService->importCategoryImageFile($existingFile, $filename);
105
106 // Return success response to client.
107 $response = array(
108 'code' => 201,
109 'status' => 'success',
110 'action' => 'upload',
111 'filename' => (string)$filename
112 );
113
114 $this->_writeResponse($response, 201);
115 }
116
117
118 /**
119 * @api {put} /category_images Rename Image File
120 * @apiVersion 2.1.0
121 * @apiName RenameImageFile
122 * @apiGroup Categories
123 *
124 * @apiDescription
125 * Use this method to rename an existing image file.
126 *
127 * @apiExample {json} Request-Body
128 * {
129 * "oldFilename": "my-old-image.png",
130 * "newFilename": "my-new-image.png"
131 * }
132 *
133 * @apiSuccess Response-Body Contains information about the executed operation.
134 *
135 * @apiSuccessExample {json} Response-Body
136 * {
137 * "code": 200,
138 * "status": "success",
139 * "action": "rename",
140 * "oldFilename": "my-old-image.png",
141 * "newFilename": "my-new-image.png"
142 * }
143 *
144 * @apiError 400-BadRequest The body of the request was empty or the request body did not contain the
145 * oldFilename or newFilename properties, or their values were invalid.
146 *
147 * @apiErrorExample Error-Response (Empty request body)
148 * HTTP/1.1 400 Bad Request
149 * {
150 * "code": 400,
151 * "status": "error",
152 * "message": "Category image data were not provided."
153 * }
154 *
155 * @apiErrorExample Error-Response (Missing parameters)
156 * HTTP/1.1 400 Bad Request
157 * {
158 * "code": 400,
159 * "status": "error",
160 * "message": "This operation requires a JSON object with 'oldFilename' and 'newFilename' properties set."
161 * }
162 *
163 */
164 public function put()
165 {
166 $imageJsonString = $this->api->request->getBody();
167
168 if(empty($imageJsonString))
169 {
170 throw new HttpApiV2Exception('Category image data were not provided.', 400);
171 }
172
173 $json = json_decode($imageJsonString);
174
175 if($json->oldFilename === null || $json->newFilename === null)
176 {
177 throw new HttpApiV2Exception('This operation requires a JSON object with "oldFilename" and "newFilename" '
178 . 'properties set. Check the documentation on how to properly use the API.',
179 400);
180 }
181
182 $oldFilename = new FilenameStringType($json->oldFilename);
183 $newFilename = new FilenameStringType($json->newFilename);
184
185 $this->writeService->renameCategoryImageFile($oldFilename, $newFilename);
186
187 $response = array(
188 'code' => 200,
189 'status' => 'success',
190 'action' => 'rename',
191 'oldFilename' => $json->oldFilename,
192 'newFilename' => $json->newFilename
193 );
194
195 $this->_writeResponse($response);
196 }
197
198
199 /**
200 * @api {delete} /category_images Delete Category Image
201 * @apiVersion 2.1.0
202 * @apiName DeleteCategoryImage
203 * @apiGroup Categories
204 *
205 * @apiDescription
206 * Removes the category image file from the server. This method will always provide a successful response even if
207 * the image file was not found.
208 *
209 * @apiExample {json} Request-Body
210 * {
211 * "filename": "file-to-be-deleted.png"
212 * }
213 *
214 * @apiSuccessExample {json} Success-Response
215 * {
216 * "code": 200,
217 * "status": "success",
218 * "action": "delete",
219 * "filename": "file-to-be-deleted.png"
220 * }
221 *
222 * @apiError 400-BadRequest The body of the request was empty or the request body did not contain the
223 * filename or its value was invalid.
224 *
225 * @apiErrorExample Error-Response (Empty request body)
226 * HTTP/1.1 400 Bad Request
227 * {
228 * "code": 400,
229 * "status": "error",
230 * "message": "Category image data were not provided."
231 * }
232 *
233 * @apiErrorExample Error-Response (Missing parameter)
234 * HTTP/1.1 400 Bad Request
235 * {
236 * "code": 400,
237 * "status": "error",
238 * "message": "This operation requires a JSON object with 'filename' properties set."
239 * }
240 */
241 public function delete()
242 {
243 $imageJsonString = $this->api->request->getBody();
244
245 if(empty($imageJsonString))
246 {
247 throw new HttpApiV2Exception('Category image data were not provided.', 400);
248 }
249
250 $imageJsonObject = json_decode($this->api->request->getBody());
251
252 if($imageJsonObject->filename === null)
253 {
254 throw new HttpApiV2Exception('This operation requires a JSON object with "filename" properties set. '
255 . 'Check the documentation on how to properly use the API.', 400);
256 }
257
258 $this->writeService->deleteCategoryImageFile(new FilenameStringType($imageJsonObject->filename));
259
260 $response = array(
261 'code' => 200,
262 'status' => 'success',
263 'action' => 'delete',
264 'filename' => $imageJsonObject->filename
265 );
266
267 $this->_writeResponse($response);
268 }
269
270
271 /**
272 * @api {get} /category_images
273 * @apiVersion 2.1.0
274 * @apiName GetCategoryImages
275 * @apiGroup Categories
276 *
277 * @apiDescription
278 * Get a list of all category image files which exists in the server's filesystem through a GET request.
279 *
280 * @apiExample {curl} Get All Category Images
281 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/category_images
282 */
283 public function get()
284 {
285 parent::get();
286 }
287 }
288