Newscoop\Services\AttachmentService::upload PHP Method

upload() public method

Upload new attachment
public upload ( Symfony\Component\HttpFoundation\File\UploadedFile $file, string $descriptionText, Language $language, array $attributes, Attachment $attachment = null ) : Attachment
$file Symfony\Component\HttpFoundation\File\UploadedFile
$descriptionText string
$language Newscoop\Entity\Language
$attributes array
$attachment Newscoop\Entity\Attachment
return Newscoop\Entity\Attachment
    public function upload(UploadedFile $file, $descriptionText, Language $language, array $attributes, Attachment $attachment = null)
    {
        $filesystem = new Filesystem();
        $filesize = $file->getClientSize();
        if ($filesize == false) {
            throw new FileException('File size is not valid');
        }
        if (!file_exists($this->config['file_directory']) || !is_writable($this->config['file_directory'])) {
            throw new FileException('Directory ' . $this->config['file_directory'] . ' is not writable');
        }
        if (!is_null($attachment)) {
            if ($filesystem->exists($this->getStorageLocation($attachment))) {
                $filesystem->remove($this->getStorageLocation($attachment));
            }
            if ($descriptionText != $attachment->getDescription()->getTranslationText()) {
                $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId();
                $description = new Translation($nextTranslationPhraseId);
                $description->setLanguage($language);
                $description->setTranslationText($descriptionText);
                $this->em->persist($description);
            }
            unset($attributes['description']);
        } else {
            $attachment = new Attachment();
            $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId();
            $description = new Translation($nextTranslationPhraseId);
            $description->setLanguage($language);
            $description->setTranslationText($descriptionText);
            unset($attributes['description']);
            $attachment->setCreated(new \DateTime());
            $this->em->persist($description);
            $this->em->persist($attachment);
        }
        $attributes = array_merge(array('language' => $language, 'name' => $file->getClientOriginalName(), 'extension' => $file->getClientOriginalExtension(), 'mimeType' => $file->getClientMimeType(), 'contentDisposition' => Attachment::CONTENT_DISPOSITION, 'sizeInBytes' => $file->getClientSize(), 'description' => $description), $attributes);
        $this->fillAttachment($attachment, $attributes);
        if (is_null($attributes['name'])) {
            $attachment->setName($file->getClientOriginalName());
        }
        $this->em->flush();
        $target = $this->makeDirectories($attachment);
        try {
            $file->move($target, $this->getFileName($attachment));
            $filesystem->chmod($target . '/' . $this->getFileName($attachment), 0644);
        } catch (\Exceptiom $e) {
            $filesystem->remove($target);
            $this->em->remove($attachment);
            $this->em->flush();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
        return $attachment;
    }