/** * @api {post} API_DOMAIN/file/attachments Attachment * @apiVersion 1.0.0 * @apiName UploadImage * @apiGroup Attachments * * @apiHeader {String} Authorization Bearer ACCESS_TOKEN * * @apiDescription This method is used for uploading attachments to a module * * @apiParam {Object} attachemnt This is the byte array representation of the file * @apiParam {Int} attachmentType This is the type of attachment. Possible values are in /warehouse/configs under attachmentTypes * @apiParam {Int} entityId This is the entity identification for the given module * @apiParam {Int} moduleId This is the module identification. Possible values are in /warehouse/configs under moduleTypes * * @apiSuccess {String} Status Success * * @apiSuccessExample {json} Sample Response: * HTTP/1.1 200 OK * { * "Status": "Success" * } */ public function attachmentsAction() { try { $httpResponseFactory = $this->getServiceLocator()->get('Service\\HttpResponseFactory'); $file = $this->getRequest()->getFiles(); $attachmentType = $this->getRequest()->getPost('attachmentType', 0); $entityId = $this->getRequest()->getPost('entityId', 0); $moduleId = $this->getRequest()->getPost('moduleId', 0); if (isset($file['attachment']) && $attachmentType && $entityId && $moduleId) { $file = $file['attachment']; if (empty($file)) { throw new ApiException(Error::FILE_NOT_FOUND_CODE); } switch ($attachmentType) { case Files::FILE_TYPE_ALL: $validator = new IsImage(); if (!$validator->isValid($file)) { throw new ApiException(Error::FILE_TYPE_NOT_TRUE_CODE); } break; case Files::FILE_TYPE_IMAGE: $validator = new IsImage(); if (!$validator->isValid($file)) { throw new ApiException(Error::FILE_TYPE_NOT_TRUE_CODE); } break; case Files::FILE_TYPE_DOCUMENT: throw new ApiException(Error::FILE_TYPE_NOT_TRUE_CODE); break; } switch ($moduleId) { case TaskService::ENTITY_TYPE_INCIDENT: $response = $this->uploadIncidentPhoto($entityId, $file); if ($response) { return new JsonModel(['Status' => 'Success']); } break; default: throw new ApiException(Error::MODULE_NOT_FOUND_CODE); } throw new ApiException(Error::FILE_TYPE_NOT_TRUE_CODE); } throw new ApiException(Error::SERVER_SIDE_PROBLEM_CODE); } catch (\Exception $e) { return new ApiProblemResponse(ApiException::handleException($e)); } }
/** * @group ZF-11258 */ public function testZF11258() { $validator = new File\IsImage(); $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo')); $this->assertTrue(array_key_exists('fileIsImageNotReadable', $validator->getMessages())); $this->assertContains("'nofile.mo'", current($validator->getMessages())); }
public function testOptionsAtConstructor() { if (!extension_loaded('fileinfo')) { $this->markTestSkipped('This PHP Version has no finfo installed'); } $validator = new File\IsImage(array('image/gif', 'image/jpg', 'magicfile' => __DIR__ . '/_files/magic.mime', 'headerCheck' => true)); $this->assertEquals(__DIR__ . '/_files/magic.mime', $validator->getMagicFile()); $this->assertTrue($validator->getHeaderCheck()); $this->assertEquals('image/gif,image/jpg', $validator->getMimeType()); }
public function editAction() { $booking_id = (int) $this->params()->fromRoute('booking_id', 0); $id = (int) $this->params()->fromRoute('id', 0); if (!$id && !$booking_id) { return $this->redirect()->toRoute('admin/bannerbookings'); } $bu = $this->getBannerUploadTable()->getBannerUpload($id); $bannerDetails = $this->getServiceLocator()->get('Admin\\Model\\AdvertisementPlanTable')->getBannerDetails($booking_id); $form = new BannerUploadForm($this->getServiceLocator()->get('Admin\\Model\\StatusTable')); $old_image = $bu->banner_content; $form->bind($bu); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { $File = $this->params()->fromFiles('banner_content'); // Make certain to merge the files info! $post = array_merge_recursive($request->getPost()->toArray(), array('banner_content' => $File['name'])); $form->setInputFilter($bu->getInputFilter()); $form->setData($post); if ($form->isValid()) { $S3 = new ImageS3(); $formData = $form->getData(); if ($post['banner_type'] == '1') { if ($File['name'] != "") { $validator = new IsImage(); if ($validator->isValid($File['tmp_name'])) { /* Image uploading code starts */ $size = new Size(array('min' => 200, 'max' => 20000000)); $imageSize = new ImageSize(array('minWidth' => $bannerDetails->banner_width, 'minHeight' => $bannerDetails->banner_height, 'maxWidth' => $bannerDetails->banner_width + 10, 'maxHeight' => $bannerDetails->banner_height + 10)); $adapter = new \Zend\File\Transfer\Adapter\Http(); $adapter->setValidators(array($size, $imageSize), $File['name']); if (!$adapter->isValid()) { $dataError = $adapter->getMessages(); return array('form' => $form, 'file_errors' => $dataError, 'booking_id' => $booking_id, 'details' => $bannerDetails); } else { $data = $S3->uploadFiles($_FILES['banner_content'], "Banners", array()); if (is_array($data) && count($data) > 0) { $formData->banner_content = $data['Original']; // deleting old image $S3->deleteFile(str_replace('https://ovessence.s3.amazonaws.com/', '', $old_image)); } } /* Image uploading code ends */ } else { return array('form' => $form, 'file_errors' => $validator->getMessages(), 'booking_id' => $booking_id, 'details' => $bannerDetails); } } else { $formData->banner_content = $old_image; } } else { if ($post['banner_type'] == '2') { } else { // deleting old image $S3->deleteFile(str_replace('https://ovessence.s3.amazonaws.com/', '', $old_image)); $formData->banner_content = $request->getPost('banner_content'); } } $this->getBannerUploadTable()->saveBannerUpload($form->getData()); $this->flashMessenger()->addSuccessMessage('Banner updated successfully..!!'); // Redirect to list of banners return $this->redirect()->toRoute('admin/banneruploads', array('booking_id' => $booking_id)); } } return array('id' => $id, 'bannerUpload' => $bu, 'errors' => $this->errors, 'booking_id' => $booking_id, 'form' => $form, 'details' => $bannerDetails); }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('admin/sitebanner', array('action' => 'add')); } $banner = $this->getBannerTable()->getBanner($id); if ($banner == false) { $this->flashMessenger()->addErrorMessage('Banner not found..!!'); return $this->redirect()->toRoute('admin/sitebanner'); } $form = new SiteBannerForm($this->getServiceLocator()->get('Admin\\Model\\AdvertisementPageTable'), $this->getServiceLocator()->get('Admin\\Model\\StatusTable')); $old_image = $banner->banner_url; $form->bind($banner); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { $File = $this->params()->fromFiles('banner_url'); // Make certain to merge the files info! $post = array_merge_recursive($request->getPost()->toArray(), array('banner_url' => $File['name'])); $old_image != '' ? $banner->getInputFilter()->get('banner_url')->setRequired(false) : ''; $form->setInputFilter($banner->getInputFilter()); $form->setData($post); if ($form->isValid()) { $S3 = new ImageS3(); $formData = $form->getData(); if ($File['name'] != "") { $validator = new IsImage(); if ($validator->isValid($File['tmp_name'])) { /* Image uploading code starts */ $size = new Size(array('min' => 200, 'max' => 20000000)); $adapter = new \Zend\File\Transfer\Adapter\Http(); $adapter->setValidators(array($size), $File['name']); if (!$adapter->isValid()) { $dataError = $adapter->getMessages(); return array('form' => $form, 'file_errors' => $dataError); } else { $data = $S3->uploadFiles($_FILES['banner_url'], "Banners", array()); if (is_array($data) && count($data) > 0) { $formData->banner_url = $data['Original']; // deleting old image $S3->deleteFile(str_replace('https://ovessence.s3.amazonaws.com/', '', $old_image)); } } /* Image uploading code ends */ } else { return array('form' => $form, 'file_errors' => $validator->getMessages()); } } else { $formData->banner_url = $old_image; } $this->getBannerTable()->saveBanner($formData); $this->flashMessenger()->addSuccessMessage('Banner updated successfully..!!'); // Redirect to list of pages return $this->redirect()->toRoute('admin/sitebanner'); } else { $this->errors = $form->getMessages(); } } return array('id' => $id, 'form' => $form, 'errors' => $this->errors, 'siteBanner' => $banner); }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); $user_id = (int) $this->params()->fromRoute('user_id', 0); $user = $this->getServiceLocator()->get('Admin\\Model\\UsersTable')->getUser($user_id); if ($user == false) { $this->flashMessenger()->addErrorMessage('User not found..!!'); return $this->redirect()->toRoute('admin/users'); } if (!$id) { return $this->redirect()->toRoute('admin/usersmedia', array('user_id' => $user_id)); } $usersMedia = $this->getUserMediaTable()->getMedia($id); if ($usersMedia == false) { $this->flashMessenger()->addErrorMessage('User media not found..!!'); return $this->redirect()->toRoute('admin/usersmedia', array('user_id' => $user_id)); } $form = new UsersMediaForm($this->getServiceLocator()->get('Admin\\Model\\StatusTable')); $old_image = $usersMedia->media_url; $form->bind($usersMedia); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { $File = $this->params()->fromFiles('media_url'); // Make certain to merge the files info! $post = array_merge_recursive($request->getPost()->toArray(), array('media_url' => $File['name'])); $form->setInputFilter($usersMedia->getInputFilter()); $form->setData($post); $adapter = new \Zend\File\Transfer\Adapter\Http(); if ($form->isValid()) { $S3 = new ImageS3(); $formData = $form->getData(); if ($File['name'] != "") { $validator = new IsImage(); if ($validator->isValid($File['tmp_name'])) { /* Image uploading code starts */ $size = new Size(array('min' => 200, 'max' => 20000000)); $adapter->setValidators(array($size), $File['name']); if (!$adapter->isValid()) { $dataError = $adapter->getMessages(); return array('form' => $form, 'file_errors' => $dataError, 'user_id' => $user_id); } else { $data = $S3->uploadFiles($_FILES['media_url'], "Media", array(), array('Media' => 100, 'Media_thumb' => 20)); if (is_array($data) && count($data) > 0) { $formData->media_url = $data['Media']; // deleting old image $S3->deleteFile(str_replace('https://ovessence.s3.amazonaws.com/', '', $old_image)); $S3->deleteFile(str_replace('https://ovessence.s3.amazonaws.com/', '', str_replace('Media', 'Media_thumb', $old_image))); } } /* Image uploading code ends */ } else { return array('form' => $form, 'file_errors' => $validator->getMessages(), 'user_id' => $user_id); } } else { $formData->media_url = $old_image; } $this->getUserMediaTable()->saveUserMedia($form->getData()); $this->flashMessenger()->addSuccessMessage('User media updated successfully..!!'); // Redirect to list of media return $this->redirect()->toRoute('admin/usersmedia', array('user_id' => $user_id)); } } return array('id' => $id, 'usermedia' => $usersMedia, 'user_id' => $user_id, 'errors' => $this->errors, 'form' => $form); }
public function testOptionsAtConstructor() { $this->markTestSkipped('magicFile current throws error if not validate, test needs to be rewritten'); $validator = new File\IsImage(array('image/gif', 'image/jpg', 'magicfile' => __FILE__, 'headerCheck' => true)); $this->assertEquals(__FILE__, $validator->getMagicFile()); $this->assertTrue($validator->getHeaderCheck()); $this->assertEquals('image/gif,image/jpg', $validator->getMimeType()); }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('admin/media'); } $media = $this->getMediaTable()->getMedia($id); if ($media == false) { $this->flashMessenger()->addErrorMessage('Media not found..!!'); return $this->redirect()->toRoute('admin/media'); } $form = new MediaForm($this->getServiceLocator()->get('Admin\\Model\\UsersTable'), $this->getServiceLocator()->get('Admin\\Model\\StatusTable')); $old_media = $media->media_url; $form->bind($media); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { $File = $this->params()->fromFiles('media_url'); // Make certain to merge the files info! $post = array_merge_recursive($request->getPost()->toArray(), array('media_url' => $File['name'])); $form->setInputFilter($media->getInputFilter()); $form->setData($post); $adapter = new \Zend\File\Transfer\Adapter\Http(); if ($form->isValid()) { $S3 = new ImageS3(); $formData = $form->getData(); if ($post['media_type'] == '1') { if ($File['name'] != "") { $validator = new IsImage(); if ($validator->isValid($File['tmp_name'])) { /* Image uploading code starts */ $size = new Size(array('min' => 200, 'max' => 20000000)); $adapter->setValidators(array($size), $File['name']); if (!$adapter->isValid()) { $dataError = $adapter->getMessages(); return array('form' => $form, 'file_errors' => $dataError); } else { $data = $S3->uploadFiles($_FILES['media_url'], "Media", array(), array('Media' => 100, 'Media_thumb' => 20)); if (is_array($data) && count($data) > 0) { $formData->media_url = $data['Media']; // deleting old image $S3->deleteFile(str_replace('https://ovessence.s3.amazonaws.com/', '', $old_image)); $S3->deleteFile(str_replace('https://ovessence.s3.amazonaws.com/', '', str_replace('Media', 'Media_thumb', $old_image))); } } /* Image uploading code ends */ } else { return array('form' => $form, 'file_errors' => $validator->getMessages()); } } else { $formData->media_url = $old_media; } } else { if ($post['media_type'] == '2') { if ($File['name'] != "") { $renameUpload = new \Zend\Filter\File\RenameUpload(array('target' => "./public/uploads/", 'randomize' => true, 'use_upload_name' => true)); if ($fileDetails = $renameUpload->filter($_FILES['media_url'])) { $filePath = $fileDetails['tmp_name']; // check video orientation and rotate if needed /*exec("mediainfo " . $fileDetails['tmp_name'] . " | grep Rotation", $mediaInfo); if (is_array($mediaInfo) && count($mediaInfo) > 0) { $tempPath = explode("/", $fileDetails['tmp_name']); $filePath = "./public/uploads/new_" . end($tempPath); exec('ffmpeg -i ' . $fileDetails['tmp_name'] . ' -vf "transpose=1" -strict -2 ' . $filePath, $output, $response); ($response == '0') ? @unlink($fileDetails['tmp_name']) : ''; }*/ $session = new Container('vimeo'); $formData->media_url = $session->file = $filePath; $session->mode = 'write'; $session->returnUrl = $this->url()->fromRoute('admin/media'); $session->old_video = $old_media; $session->media_id = $this->getMediaTable()->saveMedia($form->getData()); if ($formData->status_id == 9) { return $this->redirect()->toRoute('admin/media', array('action' => 'vimeologin')); } } else { return array('form' => $form, 'file_errors' => $adaptor->getMessages()); } } else { $formData->media_url = $old_media; } } } $this->getMediaTable()->saveMedia($form->getData()); $this->flashMessenger()->addSuccessMessage('Media updated successfully..!!'); // Redirect to list of banners return $this->redirect()->toRoute('admin/media'); } } return array('id' => $id, 'media' => $media, 'errors' => $this->errors, 'form' => $form); }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('portal/articles'); } if (!($articles = $this->getArticlesTable()->getArticle($id))) { $this->flashMessenger()->addErrorMessage('No article found..!!'); return $this->redirect()->toRoute('portal/articles'); } $form = $this->getForm(array_keys($this->getArticlesTable()->getArticleTags($id))); $form->bind($articles); $request = $this->getRequest(); if ($request->isPost()) { $filePath = $request->getPost('oldFile'); // Adding already exist validation on runtime excluding the current record $articles->getInputFilter()->get('title')->getValidatorChain()->attach(new \Zend\Validator\Db\NoRecordExists(array('table' => 'articles', 'field' => 'title', 'adapter' => $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'), 'exclude' => array('field' => 'articleId', 'value' => $id)))); $form->setInputFilter($articles->getInputFilter()); $tags = $request->getPost('tags'); $form->setData($request->getPost()); if ($form->isValid()) { /* image upload code starts here */ $file = $request->getFiles(); $fileAdapter = new \Zend\File\Transfer\Adapter\Http(); $imageValidator = new IsImage(); if ($imageValidator->isValid($file['file_url']['tmp_name'])) { $fileParts = explode('.', $file['file_url']['name']); $filter = new \Zend\Filter\File\Rename(array("target" => $this->folderPath . "/images/articles/article." . $fileParts[1], "randomize" => true)); try { @unlink($this->folderPath . $filePath); $filePath = str_replace($this->folderPath, '', $filter->filter($file['file_url'])['tmp_name']); } catch (\Exception $e) { return new ViewModel(array('form' => $form, 'file_errors' => array($e->getMessage()))); } } else { return new ViewModel(array('form' => $form, 'file_errors' => $imageValidator->getMessages())); } /* image upload code ends here */ $loggedInUser = $this->getAdminsTable()->getAdmin($this->getServiceLocator()->get('AuthService')->getIdentity()); $this->getArticlesTable()->saveArticles($form->getData(), $tags, $filePath, '', $loggedInUser->adminId); $this->flashMessenger()->addSuccessMessage('Article updated successfully..!!'); // Redirect to listing pages return $this->redirect()->toRoute('portal/articles'); } } return new ViewModel(array('form' => $form, 'article' => $articles)); }