Ejemplo n.º 1
0
 private function handleFileCreation($form, CreateResourceEvent $event)
 {
     $workspace = $event->getParent()->getWorkspace();
     $workspaceDir = $this->workspaceManager->getStorageDirectory($workspace);
     $isStorageLeft = $this->resourceManager->checkEnoughStorageSpaceLeft($workspace, $form->get('file')->getData());
     if (!$isStorageLeft) {
         $this->resourceManager->addStorageExceededFormError($form, filesize($form->get('file')->getData()), $workspace);
     } else {
         //check if there is enough space liedt
         //$file is the entity
         //$tmpFile is the other file
         $file = $form->getData();
         $tmpFile = $form->get('file')->getData();
         //the tmpFile may require some encoding.
         if ($encoding = $event->getEncoding() !== 'none') {
             $tmpFile = $this->encodeFile($tmpFile, $event->getEncoding());
         }
         $published = $form->get('published')->getData();
         $event->setPublished($published);
         $fileName = $tmpFile->getClientOriginalName();
         $ext = strtolower($tmpFile->getClientOriginalExtension());
         $mimeType = $this->container->get('claroline.utilities.mime_type_guesser')->guess($ext);
         if (!is_dir($workspaceDir)) {
             mkdir($workspaceDir);
         }
         if (pathinfo($fileName, PATHINFO_EXTENSION) === 'zip' && $form->get('uncompress')->getData()) {
             $roots = $this->unzip($tmpFile, $event->getParent(), $published);
             $event->setResources($roots);
             //do not process the resources afterwards because nodes have been created with the unzip function.
             $event->setProcess(false);
             $event->stopPropagation();
         } else {
             $file = $this->createFile($file, $tmpFile, $fileName, $mimeType, $workspace);
             $event->setResources(array($file));
             $event->stopPropagation();
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * @DI\Observe("create_file")
  *
  * @param CreateResourceEvent $event
  */
 public function onCreate(CreateResourceEvent $event)
 {
     $request = $this->container->get('request');
     $form = $this->container->get('form.factory')->create(new FileType(true), new File());
     $form->handleRequest($request);
     if ($form->isValid()) {
         $workspace = $event->getParent()->getWorkspace();
         $workspaceDir = $this->workspaceManager->getStorageDirectory($workspace);
         $isStorageLeft = $this->resourceManager->checkEnoughStorageSpaceLeft($workspace, $form->get('file')->getData());
         if (!$isStorageLeft) {
             $this->resourceManager->addStorageExceededFormError($form, filesize($form->get('file')->getData()), $workspace);
         } else {
             //check if there is enough space liedt
             $file = $form->getData();
             $tmpFile = $form->get('file')->getData();
             $published = $form->get('published')->getData();
             $event->setPublished($published);
             $fileName = $tmpFile->getClientOriginalName();
             $ext = strtolower($tmpFile->getClientOriginalExtension());
             $mimeType = $this->container->get('claroline.utilities.mime_type_guesser')->guess($ext);
             if (!is_dir($workspaceDir)) {
                 mkdir($workspaceDir);
             }
             if (pathinfo($fileName, PATHINFO_EXTENSION) === 'zip' && $form->get('uncompress')->getData()) {
                 $roots = $this->unzip($tmpFile, $event->getParent(), $published);
                 $event->setResources($roots);
                 //do not process the resources afterwards because nodes have been created with the unzip function.
                 $event->setProcess(false);
                 $event->stopPropagation();
             } else {
                 $file = $this->createFile($file, $tmpFile, $fileName, $mimeType, $workspace);
                 $event->setResources(array($file));
                 $event->stopPropagation();
             }
             return;
         }
     }
     $content = $this->container->get('templating')->render('ClarolineCoreBundle:Resource:createForm.html.twig', array('form' => $form->createView(), 'resourceType' => $event->getResourceType()));
     $event->setErrorFormContent($content);
     $event->stopPropagation();
 }