Esempio n. 1
0
 /**
  * @param string          $dataClass Parent entity class name
  * @param File|Attachment $entity    File entity
  * @param string          $fieldName Field name where new file/image field was added
  *
  * @return \Symfony\Component\Validator\ConstraintViolationListInterface
  */
 public function validate($dataClass, $entity, $fieldName = '')
 {
     /** @var Config $entityAttachmentConfig */
     if ($fieldName === '') {
         $entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass);
         $mimeTypes = $this->getMimeArray($entityAttachmentConfig->get('mimetypes'));
         if (!$mimeTypes) {
             $mimeTypes = array_merge($this->getMimeArray($this->config->get('oro_attachment.upload_file_mime_types')), $this->getMimeArray($this->config->get('oro_attachment.upload_image_mime_types')));
         }
     } else {
         $entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass, $fieldName);
         /** @var FieldConfigId $fieldConfigId */
         $fieldConfigId = $entityAttachmentConfig->getId();
         if ($fieldConfigId->getFieldType() === 'file') {
             $configValue = 'upload_file_mime_types';
         } else {
             $configValue = 'upload_image_mime_types';
         }
         $mimeTypes = $this->getMimeArray($this->config->get('oro_attachment.' . $configValue));
     }
     $fileSize = $entityAttachmentConfig->get('maxsize') * 1024 * 1024;
     foreach ($mimeTypes as $id => $value) {
         $mimeTypes[$id] = trim($value);
     }
     return $this->validator->validate($entity->getFile(), [new FileConstraint(['maxSize' => $fileSize, 'mimeTypes' => $mimeTypes])]);
 }
 public function validationData()
 {
     $fileEntity = new File();
     $file = new \Symfony\Component\HttpFoundation\File\File(realpath(__DIR__ . '/../Fixtures/testFile/test.txt'));
     $fileEntity->setFile($file);
     return ['valid' => ['testClass', $fileEntity, 'testField', "image/*\ntext/plain", true], 'bad_mime' => ['testClass', $fileEntity, 'testField', "image/*", false]];
 }
Esempio n. 3
0
 public function testToString()
 {
     $this->assertEquals('', $this->entity->__toString());
     $file = new File();
     $file->setFilename('file.txt');
     $file->setOriginalFilename('original.txt');
     $this->entity->setFile($file);
     $this->assertEquals('file.txt (original.txt)', $this->entity->__toString());
 }
 public function testFormat()
 {
     $file = new File();
     $file->setMimeType('image/png');
     $file->setOriginalFilename('test.png');
     $expected = '<img src="data:image/png;base64,dGVzdA==" alt = "test.png"/>';
     $this->manager->expects($this->once())->method('getContent')->with($file)->willReturn('test');
     $this->assertEquals($expected, $this->formatter->format($file));
 }
 public function testFormatWithArguments()
 {
     $file = new File();
     $file->setOriginalFilename('some_name.png');
     $width = 20;
     $height = 30;
     $title = 'test title';
     $this->manager->expects($this->once())->method('getResizedImageUrl')->with($file, $width, $height)->willReturn('http://test.com/image.png');
     $this->assertEquals('<a href="http://test.com/image.png">test title</a>', $this->formatter->format($file, ['width' => $width, 'height' => $height, 'title' => $title]));
 }
 public function testValidate()
 {
     $dataClass = 'testClass';
     $entity = new File();
     $file = new \Symfony\Component\HttpFoundation\File\File(realpath(__DIR__ . '/../Fixtures/testFile/test.txt'));
     $entity->setFile($file);
     $fieldName = 'testField';
     $mimeTypes = "image/*\ntext/plain";
     $maxsize = 1;
     // 1Mb
     $entityAttachmentConfig = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\Config')->disableOriginalConstructor()->getMock();
     $this->attachmentConfigProvider->expects($this->once())->method('getConfig')->with($dataClass)->will($this->returnValue($entityAttachmentConfig));
     $fieldConfigId = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\Id\\FieldConfigId')->disableOriginalConstructor()->getMock();
     $entityAttachmentConfig->expects($this->any())->method('getId')->will($this->returnValue($fieldConfigId));
     $fieldConfigId->expects($this->once())->method('getFieldType')->will($this->returnValue('file'));
     $this->config->expects($this->once())->method('get')->with('oro_attachment.upload_file_mime_types')->will($this->returnValue($mimeTypes));
     $entityAttachmentConfig->expects($this->once())->method('get')->with('maxsize')->will($this->returnValue($maxsize));
     $violationList = $this->getMockBuilder('Symfony\\Component\\Validator\\ConstraintViolationList')->disableOriginalConstructor()->getMock();
     $this->validator->expects($this->once())->method('validate')->with($this->identicalTo($entity->getFile()), [new FileConstraint(['maxSize' => $maxsize * 1024 * 1024, 'mimeTypes' => explode("\n", $mimeTypes)])])->willReturn($violationList);
     $result = $this->configValidator->validate($dataClass, $entity, $fieldName);
     $this->assertSame($violationList, $result);
 }
Esempio n. 7
0
 /**
  * Copy attachment file object
  *
  * @param File $file
  *
  * @return File
  */
 public function copyAttachmentFile(File $file)
 {
     $fileCopy = clone $file;
     $fileCopy->setFilename($this->generateFileName($file->getExtension()));
     $sourceStream = $this->filesystem->createStream($file->getFilename());
     $this->copyStreamToStorage($sourceStream, $fileCopy->getFilename());
     return $fileCopy;
 }
 /**
  * @param EmailAttachment $emailAttachment
  *
  * @return File|null
  */
 protected function copyEmailAttachmentToFileSystem(EmailAttachment $emailAttachment)
 {
     $file = new File();
     $file->setExtension($emailAttachment->getExtension());
     $file->setOriginalFilename($emailAttachment->getFileName());
     $file->setMimeType($emailAttachment->getContentType());
     $file->setFilename(uniqid() . '.' . $file->getExtension());
     $content = ContentDecoder::decode($emailAttachment->getContent()->getContent(), $emailAttachment->getContent()->getContentTransferEncoding());
     $this->filesystem->write($file->getFilename(), $content);
     $f = new ComponentFile($this->getAttachmentFullPath($file->getFilename()));
     $file->setFile($f);
     $file->setFileSize($f->getSize());
     $file->setUploaded(false);
     $file->setOwner($this->securityFacadeLink->getService()->getLoggedUser());
     return $file;
 }
Esempio n. 9
0
 /**
  * if in form was clicked delete button and file has not file name - then delete this file record from the db
  *
  * @param File          $entity
  * @param EntityManager $em
  */
 public function checkOnDelete(File $entity, EntityManager $em)
 {
     if ($entity->isEmptyFile() && $entity->getFilename() === null) {
         $em->remove($entity);
     }
 }