Example #1
0
 public function testAddRemoveAttachment()
 {
     $fh = fopen(__DIR__ . '/_files/logo.jpg', 'r');
     $user = $this->dm->find('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'user_with_attachment');
     $user->attachments['logo.jpg'] = Attachment::createFromBinaryData($fh, 'image/jpeg');
     $this->dm->flush();
     $this->dm->clear();
     // dont re-use identity map
     $user = $this->dm->find('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'user_with_attachment');
     unset($user->attachments['foo.txt']);
     $this->dm->flush();
     $this->dm->clear();
     // dont re-use identity map
     $user = $this->dm->find('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'user_with_attachment');
     $this->assertArrayHasKey('logo.jpg', $user->attachments);
     $this->assertArrayNotHasKey('foo.txt', $user->attachments);
 }
Example #2
0
 public function testCreateFromBinaryFileHandle()
 {
     $fh = fopen(__DIR__ . "/_files/foo.txt", "r");
     $attachment = Attachment::createFromBinaryData($fh);
     $this->assertEquals('Hello i am a string!', $attachment->getRawData());
 }
Example #3
0
 /**
  * @param  string $documentId
  * @param  array $data
  * @return array
  */
 private function createDocumentAttachments($documentId, $data)
 {
     $attachments = array();
     $client = $this->dm->getHttpClient();
     $basePath = '/' . $this->dm->getCouchDBClient()->getDatabase() . '/' . $documentId . '/';
     foreach ($data as $filename => $attachment) {
         if (isset($attachment['stub']) && $attachment['stub']) {
             $instance = Attachment::createStub($attachment['content_type'], $attachment['length'], $attachment['revpos'], $client, $basePath . $filename);
         } else {
             if (isset($attachment['data'])) {
                 $instance = Attachment::createFromBase64Data($attachment['data'], $attachment['content_type'], $attachment['revpos']);
             }
         }
         $attachments[$filename] = $instance;
     }
     return $attachments;
 }
 /**
  * Convert an object from $source to an entity or a value object.
  *
  * @param mixed $source
  * @param string $targetType
  * @param array $convertedChildProperties
  * @param \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration
  * @return object the target type
  * @throws \TYPO3\Flow\Property\Exception\InvalidTargetException
  * @throws \InvalidArgumentException
  */
 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration = NULL)
 {
     if (is_array($source)) {
         $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration);
     } elseif (is_string($source)) {
         if ($source === '') {
             return NULL;
         }
         $object = $this->fetchObjectFromPersistence($source, $targetType);
     } else {
         throw new \InvalidArgumentException('Only strings and arrays are accepted.', 1305630314);
     }
     foreach ($convertedChildProperties as $propertyName => $propertyValue) {
         if ($this->reflectionService->isPropertyAnnotatedWith($targetType, $propertyName, 'Doctrine\\ODM\\CouchDB\\Mapping\\Annotations\\Attachments')) {
             $attachments = array();
             foreach ($propertyValue as $version => $value) {
                 $safeFileName = preg_replace('/[^a-zA-Z-_0-9\\.]*/', '', $value['name']);
                 $attachments[$safeFileName] = \Doctrine\CouchDB\Attachment::createFromBinaryData(\TYPO3\Flow\Utility\Files::getFileContents($value['tmp_name']));
             }
             $propertyValue = $attachments;
         }
         $result = \TYPO3\Flow\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue);
         if ($result === FALSE) {
             $exceptionMessage = sprintf('Property "%s" having a value of type "%s" could not be set in target object of type "%s". Make sure that the property is accessible properly, for example via an appropriate setter method.', $propertyName, is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue), $targetType);
             throw new \TYPO3\Flow\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345);
         }
     }
     return $object;
 }
Example #5
0
 public function setOriginalData($data, $type)
 {
     $attachment = Attachment::createFromBinaryData($data, $type);
     return $this->setAttachment('original', $attachment);
 }