diff --git a/app/Controller/ShadowAttributesController.php b/app/Controller/ShadowAttributesController.php index 214bd9daf..06faa8b5d 100644 --- a/app/Controller/ShadowAttributesController.php +++ b/app/Controller/ShadowAttributesController.php @@ -828,6 +828,52 @@ class ShadowAttributesController extends AppController $this->set('_serialize', array('ShadowAttribute')); } + public function viewPicture($id, $thumbnail=false) + { + $conditions['ShadowAttribute.id'] = $id; + $conditions['ShadowAttribute.type'] = 'attachment'; + $options = array( + 'conditions' => $conditions, + 'includeAllTags' => false, + 'includeAttributeUuid' => true, + 'flatten' => true, + 'deleted' => [0, 1] + ); + + + $sa = $this->ShadowAttribute->find('first', array( + 'recursive' => -1, + 'contain' => ['Event', 'Attribute'], // required because of conditions + 'fields' => array( + 'ShadowAttribute.id', 'ShadowAttribute.old_id', 'ShadowAttribute.event_id', 'ShadowAttribute.type', 'ShadowAttribute.category', 'ShadowAttribute.uuid', 'ShadowAttribute.to_ids', 'ShadowAttribute.value', 'ShadowAttribute.comment', 'ShadowAttribute.org_id', 'ShadowAttribute.first_seen', 'ShadowAttribute.last_seen', + ), + 'conditions' => $conditions, + )); + if (empty($sa)) { + throw new NotFoundException(__('Invalid proposal.')); + } + + if (!$this->ShadowAttribute->Attribute->isImage($sa['ShadowAttribute'])) { + throw new NotFoundException("ShadowAttribute is not an image."); + } + if ($this->_isRest()) { + if ($this->ShadowAttribute->typeIsAttachment($sa['ShadowAttribute']['type'])) { + $encodedFile = $this->ShadowAttribute->base64EncodeAttachment($sa['ShadowAttribute']); + $sa['ShadowAttribute']['data'] = $encodedFile; + } + } + + if ($this->_isRest()) { + return $this->RestResponse->viewData($sa['ShadowAttribute']['data'], $this->response->type()); + } else { + $width = isset($this->request->params['named']['width']) ? $this->request->params['named']['width'] : 200; + $height = isset($this->request->params['named']['height']) ? $this->request->params['named']['height'] : 200; + $imageData = $this->ShadowAttribute->getPictureData($sa, $thumbnail, $width, $height); + $extension = pathinfo($sa['ShadowAttribute']['value'], PATHINFO_EXTENSION); + return new CakeResponse(array('body' => $imageData, 'type' => strtolower($extension))); + } + } + public function index($eventId = false) { $conditions = array(); diff --git a/app/Model/Attribute.php b/app/Model/Attribute.php index 3d1166213..f4b716ba3 100644 --- a/app/Model/Attribute.php +++ b/app/Model/Attribute.php @@ -1575,7 +1575,7 @@ class Attribute extends AppModel * @return string * @throws Exception */ - private function resizeImage($data, $maxWidth, $maxHeight) + public function resizeImage($data, $maxWidth, $maxHeight) { $image = imagecreatefromstring($data); if ($image === false) { diff --git a/app/Model/ShadowAttribute.php b/app/Model/ShadowAttribute.php index be7054a03..26ca56687 100644 --- a/app/Model/ShadowAttribute.php +++ b/app/Model/ShadowAttribute.php @@ -819,4 +819,49 @@ class ShadowAttribute extends AppModel )); } } + + public function saveAttachment($shadowAttribute, $path_suffix='') + { + $result = $this->loadAttachmentTool()->saveShadow($shadowAttribute['event_id'], $shadowAttribute['id'], $shadowAttribute['data'], $path_suffix); + if ($result) { + $this->loadAttachmentScan()->backgroundScan(AttachmentScan::TYPE_SHADOW_ATTRIBUTE, $shadowAttribute); + } + return $result; + } + + /** + * @param array $shadowAttribute + * @param bool $thumbnail + * @param int $maxWidth - When $thumbnail is true + * @param int $maxHeight - When $thumbnail is true + * @return string + * @throws Exception + */ + public function getPictureData(array $shadowAttribute, $thumbnail=false, $maxWidth=200, $maxHeight=200) + { + if ($thumbnail && extension_loaded('gd')) { + if ($maxWidth == 200 && $maxHeight == 200) { + // Return thumbnail directly if already exists + try { + return $this->getAttachment($shadowAttribute['ShadowAttribute'], $path_suffix = '_thumbnail'); + } catch (NotFoundException $e) { + // pass + } + } + + // Thumbnail doesn't exists, we need to generate it + $imageData = $this->getAttachment($shadowAttribute['ShadowAttribute']); + $imageData = $this->Attribute->resizeImage($imageData, $maxWidth, $maxHeight); + + // Save just when requested default thumbnail size + if ($maxWidth == 200 && $maxHeight == 200) { + $shadowAttribute['ShadowAttribute']['data'] = $imageData; + $this->saveAttachment($shadowAttribute['ShadowAttribute'], $path_suffix='_thumbnail'); + } + } else { + $imageData = $this->getAttachment($shadowAttribute['ShadowAttribute']); + } + + return $imageData; + } } diff --git a/app/View/Elements/Events/View/value_field.ctp b/app/View/Elements/Events/View/value_field.ctp index ac2dcc3de..f4190d0ca 100644 --- a/app/View/Elements/Events/View/value_field.ctp +++ b/app/View/Elements/Events/View/value_field.ctp @@ -28,7 +28,7 @@ switch ($object['type']) { if ($object['type'] === 'attachment' && isset($object['image'])) { if ($object['image'] === true) { $img = ''; - $img .= ''; + $img .= ''; echo $img; } else { $extension = pathinfo($object['value'], PATHINFO_EXTENSION);