1 <?php
2
3 /* --------------------------------------------------------------
4 ProductImagesApiV2Controller.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 ProductImagesApiV2Controller
17 *
18 * Provides an API interface for managing product images through the ProductWriteService.
19 *
20 * Notice: This controller IS NOT a subresource of the ProductsApiV2Controller.
21 *
22 * This controller can be reached by using one of the following URIs:
23 *
24 * - http://shop.de/api.php/v2/product_images
25 *
26 * or
27 *
28 * - http://shop.de/api.php/v2/ProductImages
29 *
30 * Using "productimages" as a resource name will not resolve to this controller.
31 *
32 * @category System
33 * @package ApiV2Controllers
34 */
35 class ProductImagesApiV2Controller extends AbstractImagesApiV2Controller
36 {
37 /**
38 * Initializes API Controller
39 */
40 protected function __initialize()
41 {
42 $this->writeService = StaticGXCoreLoader::getService('ProductWrite');
43 }
44
45
46 /**
47 * Returns the absolute path where the image files are located.
48 *
49 * @return string
50 */
51 protected function _getImageFolderName()
52 {
53 return DIR_FS_CATALOG . DIR_WS_IMAGES . 'product_images/original_images/';
54 }
55
56
57 /**
58 * @api {post} /product_images Upload Product Image
59 * @apiVersion 2.1.0
60 * @apiName UploadProductImage
61 * @apiGroup Products
62 *
63 * @apiDescription
64 * Uploads an image file for the products. Make this request without the "Content-Type: application/json". Except
65 * from the file the POST request must also contain a "filename" value with the final file name.
66 *
67 * @apiSuccess (Success 201) Response-Body Contains information about the uploaded file.
68 *
69 * @apiSuccessExample {json} Success-Response
70 * {
71 * "code": 201,
72 * "status": "success",
73 * "action": "upload",
74 * "filename": "my-image-file.png"
75 * }
76 *
77 * @apiError 400-BadRequest No image file was provided.
78 * @apiErrorExample Error-Response
79 * HTTP/1.1 400 Bad Request
80 * {
81 * "code": 400,
82 * "status": "error",
83 * "message": "No image file was provided."
84 * }
85 */
86 public function post()
87 {
88 if(!isset($_FILES) || empty($_FILES))
89 {
90 throw new HttpApiV2Exception('No image file was provided.', 400);
91 }
92
93 $file = array_shift($_FILES);
94
95 $existingFile = new ExistingFile(new NonEmptyStringType($file['tmp_name']));
96
97 if($this->api->request->post('filename') === null)
98 {
99 throw new HttpApiV2Exception('The "filename" parameter is required and was not provided with the request. '
100 . 'Check the documentation on how to properly use the API.', 400);
101 }
102
103 $filename = new FilenameStringType($this->api->request->post('filename'));
104
105 $filename = $this->writeService->importProductImageFile($existingFile, $filename);
106
107 // Return success response to client.
108 $response = array(
109 'code' => 201,
110 'status' => 'success',
111 'action' => 'upload',
112 'filename' => $filename
113 );
114
115 $this->_writeResponse($response, 201);
116 }
117
118
119 /**
120 * @api {put} /product_images Rename Image File
121 * @apiVersion 2.1.0
122 * @apiName RenameImageFile
123 * @apiGroup Products
124 *
125 * @apiDescription
126 * Use this method to rename an existing image file.
127 *
128 * @apiExample {json} Request-Body
129 * {
130 * "oldFilename": "my-old-image.png",
131 * "newFilename": "my-new-image.png"
132 * }
133 *
134 * @apiSuccess Response-Body Contains information about the executed operation.
135 *
136 * @apiSuccessExample {json} Response-Body
137 * {
138 * "code": 200,
139 * "status": "success",
140 * "action": "rename",
141 * "oldFilename": "my-old-image.png",
142 * "newFilename": "my-new-image.png"
143 * }
144 *
145 * @apiError 400-BadRequest This operation requires a JSON object with "oldFilename" and "newFilename" properties set.
146 * @apiErrorExample Error-Response
147 * HTTP/1.1 400 Bad Request
148 * {
149 * "code": 400,
150 * "status": "error",
151 * "message": "This operation requires a JSON object with "oldFilename" and "newFilename" properties set. Check the documentation on how to properly use the API."
152 * }
153 */
154 public function put()
155 {
156 $json = json_decode($this->api->request->getBody());
157
158 if($json->oldFilename === null || $json->newFilename === null)
159 {
160 throw new HttpApiV2Exception('This operation requires a JSON object with "oldFilename" and "newFilename" '
161 . 'properties set. Check the documentation on how to properly use the API.',
162 400);
163 }
164
165 $oldFilename = new FilenameStringType($json->oldFilename);
166 $newFilename = new FilenameStringType($json->newFilename);
167
168 $this->writeService->renameProductImage($oldFilename, $newFilename);
169
170 $response = array(
171 'code' => 200,
172 'status' => 'success',
173 'action' => 'rename',
174 'oldFilename' => $json->oldFilename,
175 'newFilename' => $json->newFilename
176 );
177
178 $this->_writeResponse($response);
179 }
180
181
182 /**
183 * @api {delete} /product_images Delete Product Image
184 * @apiVersion 2.1.0
185 * @apiName DeleteProductImage
186 * @apiGroup Products
187 *
188 * @apiDescription
189 * Remove the product image file from the server. This method will always provide a successful response even if
190 * the image file was not found.
191 *
192 * @apiExample {json} Delete Image
193 * {
194 * "filename": "file-to-be-deleted.png"
195 * }
196 *
197 * @apiSuccessExample {json} Success-Response
198 * {
199 * "code": 200,
200 * "status": "success",
201 * "action": "delete",
202 * "filename": "file-to-be-deleted.png"
203 * }
204 *
205 * @apiError 400-BadRequest This operation requires a JSON object with "filename" properties set.
206 * @apiErrorExample Error-Response
207 * HTTP/1.1 400 Bad Request
208 * {
209 * "code": 400,
210 * "status": "error",
211 * "message": "This operation requires a JSON object with "filename" properties set. Check the documentation on how to properly use the API."
212 * }
213 *
214 */
215 public function delete()
216 {
217 $json = json_decode($this->api->request->getBody());
218
219 if($json->filename === null)
220 {
221 throw new HttpApiV2Exception('This operation requires a JSON object with "filename" properties set. '
222 . 'Check the documentation on how to properly use the API.', 400);
223 }
224
225 $this->writeService->deleteProductImage(new FilenameStringType($json->filename));
226
227 $response = array(
228 'code' => 200,
229 'status' => 'success',
230 'action' => 'delete',
231 'filename' => $json->filename
232 );
233
234 $this->_writeResponse($response);
235 }
236
237
238 /**
239 * @api {get} /product_images
240 * @apiVersion 2.1.0
241 * @apiName GetProductImages
242 * @apiGroup Products
243 *
244 * @apiDescription
245 * Get a list of all product image files which exists in the server's filesystem through a GET request.
246 *
247 * @apiExample {curl} Get All Product Images
248 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/product_images
249 */
250 public function get()
251 {
252 parent::get();
253 }
254 }
255