/** * Creates an image from a file. * * @param string $filePath * @param string $baseMime (image|video) * * @return null|string $thumnnailPath */ public function createFromFile($filePath, $baseMime, Workspace $workspace = null) { $ds = DIRECTORY_SEPARATOR; if (is_null($workspace)) { $prefix = $this->thumbDir; } else { $prefix = $this->thumbDir . $ds . $workspace->getCode(); if (!is_dir($prefix)) { @mkdir($prefix); } } $newPath = $prefix . $ds . $this->ut->generateGuid() . ".png"; $thumbnailPath = null; if ($baseMime === 'video') { try { $thumbnailPath = $this->creator->fromVideo($filePath, $newPath, 100, 100); } catch (\Exception $e) { $thumbnailPath = null; //error handling ? $thumbnailPath = null } } if ($baseMime === 'image') { try { $thumbnailPath = $this->creator->fromImage($filePath, $newPath, 100, 100); } catch (\Exception $e) { $thumbnailPath = null; //error handling ? $thumbnailPath = null } } return $thumbnailPath; }
/** * Import a workspace list from a csv data. * * @param array $workspaces */ public function importWorkspaces(array $workspaces, $logger = null) { //$this->om->clear(); $ds = DIRECTORY_SEPARATOR; $i = 0; $j = 0; $workspaceModelManager = $this->container->get('claroline.manager.workspace_model_manager'); foreach ($workspaces as $workspace) { $this->om->startFlushSuite(); $model = null; $name = $workspace[0]; $code = $workspace[1]; $isVisible = $workspace[2]; $selfRegistration = $workspace[3]; $registrationValidation = $workspace[4]; $selfUnregistration = $workspace[5]; $errors = array(); if (isset($workspace[6]) && trim($workspace[6]) !== '') { $user = $this->om->getRepository('ClarolineCoreBundle:User')->findOneByUsername($workspace[6]); } else { $user = $this->container->get('security.context')->getToken()->getUser(); } if (isset($workspace[7])) { $model = $this->om->getRepository('ClarolineCoreBundle:Model\\WorkspaceModel')->findOneByName($workspace[7]); } if ($model) { $guid = $this->ut->generateGuid(); $workspace = new Workspace(); $this->createWorkspace($workspace); $workspace->setName($name); $workspace->setCode($code); $workspace->setDisplayable($isVisible); $workspace->setSelfRegistration($selfRegistration); $workspace->setSelfUnregistration($selfUnregistration); $workspace->setRegistrationValidation($registrationValidation); $workspace->setGuid($guid); $date = new \Datetime(date('d-m-Y H:i')); $workspace->setCreationDate($date->getTimestamp()); $workspace->setCreator($user); $workspaceModelManager->addDataFromModel($model, $workspace, $user, $errors); } else { //this should be changed later $configuration = new Configuration($this->templateDir . $ds . 'default.zip'); $configuration->setWorkspaceName($name); $configuration->setWorkspaceCode($code); $configuration->setDisplayable($isVisible); $configuration->setSelfRegistration($selfRegistration); $configuration->setSelfUnregistration($registrationValidation); $this->container->get('claroline.manager.transfert_manager')->createWorkspace($configuration, $user); } $i++; $j++; if ($i % self::MAX_WORKSPACE_BATCH_SIZE === 0) { $this->om->forceFlush(); $this->om->clear(); } $this->om->endFlushSuite(); } }
/** * Returns an archive with the required content. * * @param array $nodes[] the nodes being exported * * @throws ExportResourceException * * @return array */ public function download(array $elements, $forceArchive = false) { $data = array(); if (count($elements) === 0) { throw new ExportResourceException('No resources were selected.'); } $archive = new \ZipArchive(); $pathArch = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $this->ut->generateGuid() . '.zip'; $archive->open($pathArch, \ZipArchive::CREATE); $nodes = $this->expandResources($elements); if (!$forceArchive && count($nodes) === 1) { $event = $this->dispatcher->dispatch("download_{$nodes[0]->getResourceType()->getName()}", 'DownloadResource', array($this->getResourceFromNode($nodes[0]))); $extension = $event->getExtension(); $data['name'] = empty($extension) ? $nodes[0]->getName() : $nodes[0]->getName() . '.' . $extension; $data['file'] = $event->getItem(); $data['mimeType'] = $nodes[0]->getResourceType()->getName() === 'file' ? $nodes[0]->getMimeType() : 'text/plain'; return $data; } if (isset($nodes[0])) { $currentDir = $nodes[0]; } else { $archive->addEmptyDir($elements[0]->getName()); } foreach ($nodes as $node) { //we only download is we can... if ($this->container->get('security.context')->isGranted('EXPORT', $node)) { $resource = $this->getResourceFromNode($node); if (get_class($resource) === 'Claroline\\CoreBundle\\Entity\\Resource\\ResourceShortcut') { $node = $resource->getTarget(); } $filename = $this->getRelativePath($currentDir, $node) . $node->getName(); $resource = $this->getResourceFromNode($node); //if it's a file, we may have to add the extension back in case someone removed it from the name if ($node->getResourceType()->getName() === 'file') { $extension = '.' . pathinfo($resource->getHashName(), PATHINFO_EXTENSION); if (!preg_match("#{$extension}#", $filename)) { $filename .= $extension; } } if ($node->getResourceType()->getName() !== 'directory') { $event = $this->dispatcher->dispatch("download_{$node->getResourceType()->getName()}", 'DownloadResource', array($resource)); $extension = $event->getExtension(); if (!empty($extension)) { $filename .= '.' . $extension; } $obj = $event->getItem(); if ($obj !== null) { $archive->addFile($obj, iconv(mb_detect_encoding($filename), $this->getEncoding(), $filename)); } else { $archive->addFromString(iconv(mb_detect_encoding($filename), $this->getEncoding(), $filename), ''); } } else { $archive->addEmptyDir(iconv(mb_detect_encoding($filename), $this->getEncoding(), $filename)); } $this->dispatcher->dispatch('log', 'Log\\LogResourceExport', array($node)); } } $archive->close(); $tmpList = $this->container->getParameter('claroline.param.platform_generated_archive_path'); file_put_contents($tmpList, $pathArch . "\n", FILE_APPEND); $data['name'] = 'archive.zip'; $data['file'] = $pathArch; $data['mimeType'] = 'application/zip'; return $data; }
/** * Import a workspace list from a csv data. * * @param array $workspaces */ public function importWorkspaces(array $workspaces, $logger = null, $update = false) { $i = 0; $workspaceModelManager = $this->container->get('claroline.manager.workspace_model_manager'); foreach ($workspaces as $workspace) { ++$i; $this->om->startFlushSuite(); $endDate = null; $model = null; $name = $workspace[0]; $code = $workspace[1]; $isVisible = $workspace[2]; $selfRegistration = $workspace[3]; $registrationValidation = $workspace[4]; $selfUnregistration = $workspace[5]; if (isset($workspace[6]) && trim($workspace[6]) !== '') { $user = $this->om->getRepository('ClarolineCoreBundle:User')->findOneByUsername($workspace[6]); } else { $user = $this->container->get('security.context')->getToken()->getUser(); } if (isset($workspace[7])) { $model = $this->om->getRepository('ClarolineCoreBundle:Model\\WorkspaceModel')->findOneByName($workspace[7]); } if (isset($workspace[8])) { $endDate = new \DateTime(); $endDate->setTimestamp($workspace[8]); } if ($update) { $workspace = $this->getOneByCode($code); if (!$workspace) { //if the workspace doesn't exists, just keep going... continue; } if ($logger) { $logger('Updating ' . $code . ' (' . $i . '/' . count($workspaces) . ') ...'); } } else { $workspace = new Workspace(); } $workspace->setName($name); $workspace->setCode($code); $workspace->setDisplayable($isVisible); $workspace->setSelfRegistration($selfRegistration); $workspace->setSelfUnregistration($selfUnregistration); $workspace->setRegistrationValidation($registrationValidation); $workspace->setCreator($user); if ($endDate) { $workspace->setEndDate($endDate); } if (!$update) { if ($logger) { $logger('Creating ' . $code . ' (' . $i . '/' . count($workspaces) . ') ...'); } if ($model) { $guid = $this->ut->generateGuid(); $this->createWorkspace($workspace); $workspace->setGuid($guid); $date = new \Datetime(date('d-m-Y H:i')); $workspace->setCreationDate($date->getTimestamp()); $workspaceModelManager->addDataFromModel($model, $workspace, $user); } else { $template = new File($this->container->getParameter('claroline.param.default_template')); $this->container->get('claroline.manager.transfer_manager')->createWorkspace($workspace, $template, true); } } else { if ($model) { $workspaceModelManager->updateDataFromModel($model, $workspace); } } $this->om->persist($workspace); $logger('UOW: ' . $this->om->getUnitOfWork()->size()); if ($i % 100 === 0) { $this->om->forceFlush(); $user = $this->om->getRepository('ClarolineCoreBundle:User')->find($user->getId()); $this->om->merge($user); $this->om->refresh($user); } } $logger('Final flush...'); $this->om->endFlushSuite(); }