/**
  * Download a picture from the embed media platform
  * to get a thumbnail.
  *
  * @return string|false File URL in document files folder.
  */
 public function downloadThumbnail()
 {
     $url = $this->getThumbnailURL();
     if (false !== $url && '' !== $url) {
         $pathinfo = basename($url);
         if ($pathinfo != "") {
             $thumbnailName = $this->embedId . '_' . $pathinfo;
             try {
                 $original = \GuzzleHttp\Stream\Stream::factory(fopen($url, 'r'));
                 $local = \GuzzleHttp\Stream\Stream::factory(fopen(Document::getFilesFolder() . '/' . $thumbnailName, 'w'));
                 $local->write($original->getContents());
                 if (file_exists(Document::getFilesFolder() . '/' . $thumbnailName) && filesize(Document::getFilesFolder() . '/' . $thumbnailName) > 0) {
                     return $thumbnailName;
                 } else {
                     return false;
                 }
             } catch (\GuzzleHttp\Exception\RequestException $e) {
                 return false;
             }
         }
     }
     return false;
 }
 /**
  * Set setting value according to its type.
  *
  * @param string  $value
  * @param Setting $setting
  */
 protected function setSettingValue($value, Setting $setting)
 {
     switch ($setting->getType()) {
         case NodeTypeField::DOCUMENTS_T:
             if ($value !== null && $value->getError() == UPLOAD_ERR_OK && $value->isValid()) {
                 $document = new Document();
                 $document->setFilename($value->getClientOriginalName());
                 $document->setMimeType($value->getMimeType());
                 $this->getService('em')->persist($document);
                 $this->getService('em')->flush();
                 $value->move(Document::getFilesFolder() . '/' . $document->getFolder(), $document->getFilename());
                 $setting->setValue($document->getId());
             }
             break;
         default:
             $setting->setValue($value);
             break;
     }
 }
 /**
  * Handle upload form data to create a Document.
  *
  * @param Symfony\Component\Form\Form $data
  *
  * @return boolean
  */
 private function uploadDocument($data, $folderId = null)
 {
     if (!empty($data['attachment'])) {
         $uploadedFile = $data['attachment']->getData();
         if ($uploadedFile !== null && $uploadedFile->getError() == UPLOAD_ERR_OK && $uploadedFile->isValid()) {
             try {
                 $document = new Document();
                 $document->setFilename($uploadedFile->getClientOriginalName());
                 $document->setMimeType($uploadedFile->getMimeType());
                 $this->getService('em')->persist($document);
                 $this->getService('em')->flush();
                 if (null !== $folderId && $folderId > 0) {
                     $folder = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Folder', (int) $folderId);
                     $document->addFolder($folder);
                     $folder->addDocument($document);
                     $this->getService('em')->flush();
                 }
                 $uploadedFile->move(Document::getFilesFolder() . '/' . $document->getFolder(), $document->getFilename());
                 return $document;
             } catch (\Exception $e) {
                 return false;
             }
         }
     }
     return false;
 }