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