/** * @param mixed $object * * @return bool */ public function canHandle($object) { if (parent::canHandle($object) && ($object instanceof File || strpos($object->getContentType(), 'image') === 0)) { return true; } return false; }
/** * @param $filePath string The full filepath of the asset you want to upload. The filetype will be automatically detected. * @param $folderId integer For now you still have to manually pass the correct folder ID. * @param string $context This is needed because the Filesystem basepath differs between web & console application env. * * @return Media */ public function createFile($filePath, $folderId, $context = MediaCreatorService::CONTEXT_WEB) { $fileHandler = new FileHandler(); // Get file from FilePath. $data = new File($filePath, true); if ($context == MediaCreatorService::CONTEXT_CONSOLE) { $fileHandler->fileSystem = new Filesystem(new \Gaufrette\Adapter\Local("web/uploads/media/", true)); } /** @var $media Media */ $media = $fileHandler->createNew($data); /** @var $folder Folder */ $folder = $this->folderRepository->getFolder($folderId); $media->setFolder($folder); $fileHandler->prepareMedia($media); $fileHandler->updateMedia($media); $fileHandler->saveMedia($media); $this->em->persist($media); $this->em->flush(); return $media; }