/** end point for asset related data. * - get asset by id * GET http://[YOUR-DOMAIN]/webservice/rest/asset/id/1281?apikey=[API-KEY] * returns json-encoded asset data. * - delete asset by id * DELETE http://[YOUR-DOMAIN]/webservice/rest/asset/id/1281?apikey=[API-KEY] * returns json encoded success value * - create asset * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/asset?apikey=[API-KEY] * body: json-encoded asset data in the same format as returned by get asset by id * but with missing id field or id set to 0 * returns json encoded asset id * - update asset * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/asset?apikey=[API-KEY] * body: same as for create asset but with asset id * returns json encoded success value * @throws \Exception */ public function assetAction() { $id = $this->getParam("id"); $success = false; try { if ($this->isGet()) { $asset = Asset::getById($id); if (!$asset) { $this->encoder->encode(array("success" => false, "msg" => "Asset does not exist", "code" => self::ELEMENT_DOES_NOT_EXIST)); return; } $this->checkPermission($asset, "get"); if ($asset instanceof Asset\Folder) { $object = $this->service->getAssetFolderById($id); } else { $light = $this->getParam("light"); $options = array("LIGHT" => $light ? 1 : 0); $object = $this->service->getAssetFileById($id, $options); $algo = "sha1"; $thumbnailConfig = $this->getParam("thumbnail"); if ($thumbnailConfig && $asset->getType() == "image") { $checksum = $asset->getThumbnail($thumbnailConfig)->getChecksum($algo); $object->thumbnail = (string) $asset->getThumbnail($thumbnailConfig); } else { $checksum = $asset->getChecksum($algo); } $object->checksum = array("algo" => $algo, "value" => $checksum); if ($light) { unset($object->data); } } $this->encoder->encode(array("success" => true, "data" => $object)); return; } else { if ($this->isDelete()) { $asset = Asset::getById($id); if ($asset) { $this->checkPermission($asset, "delete"); } $success = $this->service->deleteAsset($id); $this->encoder->encode(array("success" => $success)); return; } else { if ($this->isPost() || $this->isPut()) { $data = file_get_contents("php://input"); $data = \Zend_Json::decode($data); $type = $data["type"]; $id = null; if ($data["id"]) { $asset = Asset::getById($data["id"]); if ($asset) { $this->checkPermission($asset, "update"); } $isUpdate = true; if ($type == "folder") { $wsData = self::fillWebserviceData("\\Pimcore\\Model\\Webservice\\Data\\Asset\\Folder\\In", $data); $success = $this->service->updateAssetFolder($wsData); } else { $wsData = self::fillWebserviceData("\\Pimcore\\Model\\Webservice\\Data\\Asset\\File\\In", $data); $success = $this->service->updateAssetFile($wsData); } } else { if ($type == "folder") { $class = "\\Pimcore\\Model\\Webservice\\Data\\Asset\\Folder\\In"; $method = "createAssetFolder"; } else { $class = "\\Pimcore\\Model\\Webservice\\Data\\Asset\\File\\In"; $method = "createAssetFile"; } $wsData = self::fillWebserviceData($class, $data); $asset = new Asset(); $asset->setId($wsData->parentId); $this->checkPermission($asset, "create"); $id = $this->service->{$method}($wsData); } if (!$isUpdate) { $success = $id != null; } if ($success && !$isUpdate) { $this->encoder->encode(array("success" => $success, "data" => array("id" => $id))); } else { $this->encoder->encode(array("success" => $success)); } return; } } } } catch (\Exception $e) { \Logger::error($e); $this->encoder->encode(array("success" => false, "msg" => (string) $e)); } $this->encoder->encode(array("success" => false)); }
/** end point for asset related data. * - get asset by id * GET http://[YOUR-DOMAIN]/webservice/rest/asset/id/1281?apikey=[API-KEY] * returns json-encoded asset data. * - delete asset by id * DELETE http://[YOUR-DOMAIN]/webservice/rest/asset/id/1281?apikey=[API-KEY] * returns json encoded success value * - create asset * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/asset?apikey=[API-KEY] * body: json-encoded asset data in the same format as returned by get asset by id * but with missing id field or id set to 0 * returns json encoded asset id * - update asset * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/asset?apikey=[API-KEY] * body: same as for create asset but with asset id * returns json encoded success value * @throws \Exception */ public function assetAction() { $id = $this->getParam("id"); $success = false; try { if ($this->isGet()) { /** * @api {get} /asset Get asset * @apiParamExample {json} Request-Example: * { * "id": 4711 * "apikey": '2132sdf2321rwefdcvvce22' * } * @apiName getAssetFileById * @apiSampleRequest off * @apiGroup Asset * @apiParam {int} id The id of asset you search * @apiParam {string} apikey your access token * @apiSuccessExample {json} Success-Response: * {"success": "true", "data":{"path":"\/crm\/inquiries\/","creationDate":1368630916,"modificationDate":1388409137,"userModification":null,"childs":null}} */ $asset = Asset::getById($id); if (!$asset) { $this->encoder->encode(["success" => false, "msg" => "Asset does not exist", "code" => self::ELEMENT_DOES_NOT_EXIST]); return; } $this->checkPermission($asset, "get"); if ($asset instanceof Asset\Folder) { $object = $this->service->getAssetFolderById($id); } else { $light = $this->getParam("light"); $options = ["LIGHT" => $light ? 1 : 0]; $object = $this->service->getAssetFileById($id, $options); $algo = "sha1"; $thumbnailConfig = $this->getParam("thumbnail"); if ($thumbnailConfig && $asset->getType() == "image") { $checksum = $asset->getThumbnail($thumbnailConfig)->getChecksum($algo); $object->thumbnail = (string) $asset->getThumbnail($thumbnailConfig); } else { $checksum = $asset->getChecksum($algo); } $object->checksum = ["algo" => $algo, "value" => $checksum]; if ($light) { unset($object->data); } } $this->encoder->encode(["success" => true, "data" => $object]); return; } elseif ($this->isDelete()) { /** * @api {delete} /asset Delete asset * @apiName deleteAsset * @apiGroup Asset * @apiParam {int} id The id of asset you delete * @apiSampleRequest off * @apiParamExample {json} Request-Example: * { * "id": 4711 * "apikey": '2132sdf2321rwefdcvvce22' * } * @apiParam {string} apikey your access token * @apiSuccess {boolean} success Returns true if finished successfully * @apiSuccessExample {json} Succes-Response: * {"success":true} * @apiError {boolean} success Returns false if failed * @apiErrorExample {json} Error-Response: * {"success":false,"msg":"exception 'Exception' with message 'Asset with given ID (712131243) does not exist.'"} */ $asset = Asset::getById($id); if ($asset) { $this->checkPermission($asset, "delete"); } $success = $this->service->deleteAsset($id); $this->encoder->encode(["success" => $success]); return; } elseif ($this->isPost() || $this->isPut()) { $data = file_get_contents("php://input"); $data = \Zend_Json::decode($data); $type = $data["type"]; $id = null; if ($data["id"]) { $asset = Asset::getById($data["id"]); if ($asset) { $this->checkPermission($asset, "update"); } $isUpdate = true; if ($type == "folder") { $wsData = self::fillWebserviceData("\\Pimcore\\Model\\Webservice\\Data\\Asset\\Folder\\In", $data); $success = $this->service->updateAssetFolder($wsData); } else { $wsData = self::fillWebserviceData("\\Pimcore\\Model\\Webservice\\Data\\Asset\\File\\In", $data); $success = $this->service->updateAssetFile($wsData); } } else { if ($type == "folder") { $class = "\\Pimcore\\Model\\Webservice\\Data\\Asset\\Folder\\In"; $method = "createAssetFolder"; } else { $class = "\\Pimcore\\Model\\Webservice\\Data\\Asset\\File\\In"; $method = "createAssetFile"; } $wsData = self::fillWebserviceData($class, $data); $asset = new Asset(); $asset->setId($wsData->parentId); $this->checkPermission($asset, "create"); $id = $this->service->{$method}($wsData); } if (!$isUpdate) { $success = $id != null; } if ($success && !$isUpdate) { $this->encoder->encode(["success" => $success, "data" => ["id" => $id]]); } else { $this->encoder->encode(["success" => $success]); } return; } } catch (\Exception $e) { Logger::error($e); $this->encoder->encode(["success" => false, "msg" => (string) $e]); } $this->encoder->encode(["success" => false]); }