/** * Update attachment entity before upload * * @param File $entity */ public function preUpload(File $entity) { if ($entity->isEmptyFile()) { if ($this->filesystem->has($entity->getFilename())) { $this->filesystem->delete($entity->getFilename()); } $entity->setFilename(null); $entity->setExtension(null); $entity->setOriginalFilename(null); } if ($entity->getFile() !== null && $entity->getFile()->isFile()) { $entity->setOwner($this->securityFacadeLink->getService()->getLoggedUser()); $file = $entity->getFile(); if ($entity->getFilename() !== null && $this->filesystem->has($entity->getFilename())) { $this->filesystem->delete($entity->getFilename()); } $entity->setExtension($file->guessExtension()); if ($file instanceof UploadedFile) { $entity->setOriginalFilename($file->getClientOriginalName()); $entity->setMimeType($file->getClientMimeType()); $entity->setFileSize($file->getClientSize()); } else { $entity->setOriginalFilename($file->getFileName()); $entity->setMimeType($file->getMimeType()); $entity->setFileSize($file->getSize()); } $entity->setFilename(uniqid() . '.' . $entity->getExtension()); if ($this->filesystem->getAdapter() instanceof MetadataSupporter) { $this->filesystem->getAdapter()->setMetadata($entity->getFilename(), ['contentType' => $entity->getMimeType()]); } } }
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])); }
/** * @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; }