/** * 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()]); } } }
/** * @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; }