Example #1
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $user = $this->getReference("user/{$this->creator}");
     $directory = $this->getReference("directory/{$this->directory}");
     $resourceManager = $this->container->get('claroline.manager.resource_manager');
     $filesDirectory = $this->container->getParameter('claroline.param.files_directory');
     $ut = $this->container->get('claroline.utilities.misc');
     $fileType = $manager->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('file');
     foreach ($this->files as $filePath) {
         $filePathParts = explode(DIRECTORY_SEPARATOR, $filePath);
         $fileName = array_pop($filePathParts);
         $extension = pathinfo($filePath, PATHINFO_EXTENSION);
         $hashName = "{$ut->generateGuid()}.{$extension}";
         $targetFilePath = $filesDirectory . DIRECTORY_SEPARATOR . $hashName;
         $file = new File();
         $file->setName($fileName);
         $file->setHashName($hashName);
         if (file_exists($filePath)) {
             copy($filePath, $targetFilePath);
             $file->setSize(filesize($filePath));
         } else {
             touch($targetFilePath);
             $file->setSize(0);
         }
         $mimeType = MimeTypeGuesser::getInstance()->guess($targetFilePath);
         $file->setMimeType($mimeType);
         $resourceManager->create($file, $fileType, $user, $directory->getWorkspace(), $directory);
         $this->addReference("file/{$fileName}", $file);
     }
     $manager->flush();
 }
 private function createFile(Dropzone $dropzone, Drop $drop, $tmpFile)
 {
     $em = $this->getDoctrine()->getManager();
     $parent = $this->getDropHiddenDirectory($dropzone, $drop);
     $file = new File();
     $fileName = $tmpFile->getClientOriginalName();
     $extension = pathinfo($fileName, PATHINFO_EXTENSION);
     $size = filesize($tmpFile);
     $mimeType = $tmpFile->getClientMimeType();
     $hashName = $this->container->get('claroline.utilities.misc')->generateGuid() . "." . $extension;
     $tmpFile->move($this->container->getParameter('claroline.param.files_directory'), $hashName);
     $file->setSize($size);
     $file->setName($fileName);
     $file->setHashName($hashName);
     $file->setMimeType($mimeType);
     $resourceManager = $this->get('claroline.manager.resource_manager');
     $resourceManager->create($file, $resourceManager->getResourceTypeByName('file'), $dropzone->getResourceNode()->getCreator(), $dropzone->getResourceNode()->getWorkspace(), $parent);
     $em->flush();
     return $file->getResourceNode();
 }
Example #3
0
 public function uploadContent(File $file, UploadedFile $upload)
 {
     $ds = DIRECTORY_SEPARATOR;
     $node = $file->getResourceNode();
     $workspaceId = $node->getWorkspace()->getId();
     //edit file
     $fileName = $upload->getClientOriginalName();
     $size = @filesize($upload);
     $extension = pathinfo($fileName, PATHINFO_EXTENSION);
     $mimeType = $upload->getClientMimeType();
     $hashName = 'WORKSPACE_' . $workspaceId . $ds . $this->ut->generateGuid() . "." . $extension;
     $upload->move($this->fileDir . $ds . 'WORKSPACE_' . $workspaceId, $hashName);
     $file->setSize($size);
     $file->setHashName($hashName);
     $file->setMimeType($mimeType);
     //edit node
     $node->setMimeType($mimeType);
     $node->setName($fileName);
     //edit icon
     $this->om->persist($file);
     $this->om->persist($node);
     $this->om->flush();
 }
Example #4
0
 /**
  * @EXT\Route(
  *     "/upload/{parent}",
  *     name="claro_file_upload_with_ajax",
  *     options={"expose"=true}
  * )
  * @EXT\ParamConverter("user", options={"authenticatedUser" = true})
  *
  * Creates a resource from uploaded file.
  *
  * @param integer $parentId the parent id
  *
  * @throws \Exception
  * @return Response
  */
 public function uploadWithAjaxAction(ResourceNode $parent, User $user)
 {
     $parent = $this->get('claroline.manager.resource_manager')->getById($parent);
     $collection = new ResourceCollection(array($parent));
     $collection->setAttributes(array('type' => 'file'));
     $this->checkAccess('CREATE', $collection);
     $file = new File();
     $request = $this->getRequest();
     $fileName = $request->get('fileName');
     $tmpFile = $request->files->get('file');
     $extension = pathinfo($fileName, PATHINFO_EXTENSION);
     $size = filesize($tmpFile);
     $mimeType = $tmpFile->getClientMimeType();
     $hashName = 'WORKSPACE_' . $parent->getWorkspace()->getId() . DIRECTORY_SEPARATOR . $this->container->get('claroline.utilities.misc')->generateGuid() . '.' . $extension;
     $destination = $this->container->getParameter('claroline.param.files_directory') . DIRECTORY_SEPARATOR . 'WORKSPACE_' . $parent->getWorkspace()->getId();
     $tmpFile->move($destination, $hashName);
     $file->setSize($size);
     $file->setName($fileName);
     $file->setHashName($hashName);
     $file->setMimeType($mimeType);
     $manager = $this->get('claroline.manager.resource_manager');
     $file = $manager->create($file, $manager->getResourceTypeByName('file'), $user, $parent->getWorkspace(), $parent);
     return new JsonResponse(array($manager->toArray($file->getResourceNode(), $this->get('security.token_storage')->getToken())));
 }
Example #5
0
 /**
  * Search object tag to create Claroline resource
  *
  * @access private
  *
  */
 private function objectToResource()
 {
     $elements = array();
     $objects = $this->assessmentItem->getElementsByTagName('object');
     $ws = $this->user->getPersonalWorkspace();
     $manager = $this->container->get('claroline.manager.resource_manager');
     $filesDirectory = $this->container->getParameter('claroline.param.files_directory');
     $this->getDirQTIImport($ws);
     foreach ($objects as $ob) {
         $fileName = $this->getFileName($ob);
         $tmpFile = $this->qtiRepos->getUserDir() . '/' . $fileName;
         $extension = pathinfo($fileName, PATHINFO_EXTENSION);
         $hashName = $this->container->get('claroline.utilities.misc')->generateGuid() . '.' . $extension;
         $mimeType = $ob->getAttribute('type');
         $size = filesize($tmpFile);
         $targetFilePath = $filesDirectory . DIRECTORY_SEPARATOR . $hashName;
         copy($tmpFile, $targetFilePath);
         $file = new File();
         $file->setSize($size);
         $file->setName($fileName);
         $file->setHashName($hashName);
         $file->setMimeType($mimeType);
         $abstractResource = $manager->create($file, $manager->getResourceTypeByName('file'), $this->user, $ws, $this->dirQTI);
         if ($ob->parentNode->nodeName != 'selectPointInteraction' && $ob->parentNode->nodeName != 'hotspotInteraction') {
             $elements[] = array($ob, $abstractResource->getResourceNode());
         }
     }
     $this->callReplaceNode($elements);
 }
Example #6
0
 public function createFile(File $file, SfFile $tmpFile, $fileName, $mimeType, Workspace $workspace = null)
 {
     $extension = pathinfo($fileName, PATHINFO_EXTENSION);
     $size = filesize($tmpFile);
     if (!is_null($workspace)) {
         $hashName = 'WORKSPACE_' . $workspace->getId() . DIRECTORY_SEPARATOR . $this->container->get('claroline.utilities.misc')->generateGuid() . "." . $extension;
         $tmpFile->move($this->workspaceManager->getStorageDirectory($workspace) . '/', $hashName);
     } else {
         $hashName = $this->tokenStorage->getToken()->getUsername() . DIRECTORY_SEPARATOR . $this->container->get('claroline.utilities.misc')->generateGuid() . "." . $extension;
         $tmpFile->move($this->container->getParameter('claroline.param.files_directory') . DIRECTORY_SEPARATOR . $this->tokenStorage->getToken()->getUsername(), $hashName);
     }
     $file->setSize($size);
     $file->setName($fileName);
     $file->setHashName($hashName);
     $file->setMimeType($mimeType);
     return $file;
 }
 /**
  * Copies a file (no persistence).
  *
  * @param File $resource
  *
  * @return File
  */
 private function copy(File $resource, ResourceNode $destParent)
 {
     $ds = DIRECTORY_SEPARATOR;
     $workspace = $destParent->getWorkspace();
     $newFile = new File();
     $newFile->setSize($resource->getSize());
     $newFile->setName($resource->getName());
     $newFile->setMimeType($resource->getMimeType());
     $hashName = 'WORKSPACE_' . $workspace->getId() . $ds . $this->container->get('claroline.utilities.misc')->generateGuid() . '.' . pathinfo($resource->getHashName(), PATHINFO_EXTENSION);
     $newFile->setHashName($hashName);
     $fileDir = $this->container->getParameter('claroline.param.files_directory');
     $filePath = $fileDir . $ds . $resource->getHashName();
     $newPath = $fileDir . $ds . $hashName;
     $workspaceDir = $fileDir . $ds . 'WORKSPACE_' . $workspace->getId();
     if (!is_dir($workspaceDir)) {
         mkdir($workspaceDir);
     }
     copy($filePath, $newPath);
     return $newFile;
 }
 /**
  * Copies a file (no persistence).
  *
  * @param File $resource
  *
  * @return File
  */
 private function copy(File $resource)
 {
     $hash = $this->getHash($resource);
     $file = new File();
     $file->setSize($resource->getSize());
     $file->setName($resource->getName());
     $file->setMimeType($resource->getMimeType());
     $file->setHashName($hash);
     copy($this->filesPath . $resource->getHashName(), $this->filesPath . $hash);
     $this->getZip()->open($this->filesPath . $hash);
     $this->unzip($hash);
     return $file;
 }
 /**
  * @EXT\Route(
  *     "/upload/{parent}",
  *     name="claro_file_upload_with_ajax",
  *     options={"expose"=true}
  * )
  * @EXT\ParamConverter("user", options={"authenticatedUser" = true})
  *
  * Creates a resource from uploaded file.
  *
  * @param int $parentId the parent id
  *
  * @throws \Exception
  *
  * @return Response
  */
 public function uploadWithAjaxAction(ResourceNode $parent, User $user)
 {
     $parent = $this->resourceManager->getById($parent);
     $collection = new ResourceCollection(array($parent));
     $collection->setAttributes(array('type' => 'file'));
     $this->checkAccess('CREATE', $collection);
     $file = new File();
     $fileName = $this->request->get('fileName');
     $tmpFile = $this->request->files->get('file');
     $extension = pathinfo($fileName, PATHINFO_EXTENSION);
     $size = filesize($tmpFile);
     $mimeType = $tmpFile->getClientMimeType();
     $hashName = 'WORKSPACE_' . $parent->getWorkspace()->getId() . DIRECTORY_SEPARATOR . $this->ut->generateGuid() . '.' . $extension;
     $destination = $this->fileDir . DIRECTORY_SEPARATOR . 'WORKSPACE_' . $parent->getWorkspace()->getId();
     $tmpFile->move($destination, $hashName);
     $file->setSize($size);
     $file->setName($fileName);
     $file->setHashName($hashName);
     $file->setMimeType($mimeType);
     $file = $this->resourceManager->create($file, $this->resourceManager->getResourceTypeByName('file'), $user, $parent->getWorkspace(), $parent);
     return new JsonResponse(array($this->resourceManager->toArray($file->getResourceNode(), $this->tokenStorage->getToken())));
 }
Example #10
0
 public function file($fileName, $mimeType, $withNode = false, User $creator = null)
 {
     $file = new File();
     $file->setSize(123);
     $file->setName($fileName);
     $file->setHashName(uniqid());
     $file->setMimeType($mimeType);
     $this->om->persist($file);
     if ($withNode && !$creator) {
         throw new \Exception('File requires a creator if you want to set a Resource Node.');
     }
     if ($withNode) {
         $fileType = $this->om->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('file');
         $this->container->get('claroline.manager.resource_manager')->create($file, $fileType, $creator);
     }
     return $file;
 }
 /**
  * Handle web rtc blob file upload, conversion and Claroline File resource creation.
  *
  * @param type         $postData
  * @param UploadedFile $video
  * @param Workspace    $workspace
  *
  * @return Claroline File
  */
 public function uploadFileAndCreateResource($postData, UploadedFile $video, Workspace $workspace = null)
 {
     $errors = array();
     // final file upload dir
     $targetDir = '';
     if (!is_null($workspace)) {
         $targetDir = $this->workspaceManager->getStorageDirectory($workspace);
     } else {
         $targetDir = $this->fileDir . DIRECTORY_SEPARATOR . $this->tokenStorage->getToken()->getUsername();
     }
     // if the taget dir does not exist, create it
     $fs = new Filesystem();
     if (!$fs->exists($targetDir)) {
         $fs->mkdir($targetDir);
     }
     $isFirefox = isset($postData['nav']) && $postData['nav'] === 'firefox';
     $extension = 'webm';
     $encodingExt = 'webm';
     $mimeType = 'video/' . $extension;
     if (!$this->validateParams($postData, $video)) {
         array_push($errors, 'one or more request parameters are missing.');
         return array('file' => null, 'errors' => $errors);
     }
     // the filename that will be in database (human readable)
     $fileBaseName = $postData['fileName'];
     $uniqueBaseName = $this->claroUtils->generateGuid();
     $finalFileName = $uniqueBaseName . '.' . $extension;
     $finalFilePath = $targetDir . DIRECTORY_SEPARATOR . $finalFileName;
     $baseHashName = $this->getBaseFileHashName($uniqueBaseName, $workspace);
     $hashName = $baseHashName . '.' . $extension;
     $tempVideoFileName = $fileBaseName . '.' . $extension;
     $encode = true;
     if ($encode) {
         // upload file to temp directory to allow it's conversion
         $video->move($this->tempUploadDir, $tempVideoFileName);
         $sourceFilePath = $this->tempUploadDir . DIRECTORY_SEPARATOR . $tempVideoFileName;
         $tempEncodedFilePath = $this->tempUploadDir . DIRECTORY_SEPARATOR . $finalFileName;
         // create avconv cmd
         $cmd = 'avconv -i ' . $sourceFilePath . ' -codec:v copy -codec:a opus -ac 1 ' . $tempEncodedFilePath;
         $output;
         $returnVar;
         exec($cmd, $output, $returnVar);
         // cmd error
         if ($returnVar !== 0) {
             array_push($errors, 'File conversion failed with command ' . $cmd . ' and returned ' . $returnVar);
             return array('file' => null, 'errors' => $errors);
         }
         $fs->copy($tempEncodedFilePath, $finalFilePath);
         // get encoded file size...
         $sFile = new sFile($finalFilePath);
         $size = $sFile->getSize();
         // remove temp encoded file from temp dir
         @unlink($tempEncodedFilePath);
         // remove source file from temp dir
         @unlink($sourceFilePath);
     } else {
         $size = $video->getSize();
         $video->move($targetDir, $finalFileName);
     }
     $file = new File();
     $file->setSize($size);
     $file->setName($fileBaseName . '.' . $encodingExt);
     $file->setHashName($hashName);
     $file->setMimeType($mimeType);
     return array('file' => $file, 'errors' => []);
 }
Example #12
0
 private function addFile(ResourceNode $parent, User $user)
 {
     $fileType = $this->container->get('claroline.persistence.object_manager')->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName('file');
     $this->suffixName++;
     $name = 'file_' . $this->suffixName . '.txt';
     $file = new File();
     $file->setName($name);
     $hashName = $this->container->get('claroline.utilities.misc')->generateGuid();
     $file->setHashName($hashName);
     $file->setMimeType('text/plain');
     $file->setSize(0);
     $this->log('create ' . $name);
     $file = $this->container->get('claroline.manager.resource_manager')->create($file, $fileType, $user, $this->pws, $parent);
     $this->totalResources++;
     return $file;
 }
 /**
  * Handle web rtc blob file upload, conversion and Claroline File resource creation.
  *
  * @param type         $postData
  * @param UploadedFile $blob
  * @param Workspace    $workspace
  *
  * @return File
  */
 public function uploadFileAndCreateResource($postData, UploadedFile $blob, Workspace $workspace = null)
 {
     $errors = array();
     // final file upload dir
     $targetDir = '';
     if (!is_null($workspace)) {
         $targetDir = $this->workspaceManager->getStorageDirectory($workspace);
     } else {
         $targetDir = $this->fileDir . DIRECTORY_SEPARATOR . $this->tokenStorage->getToken()->getUsername();
     }
     // if the taget dir does not exist, create it
     $fs = new Filesystem();
     if (!$fs->exists($targetDir)) {
         $fs->mkdir($targetDir);
     }
     $isFirefox = isset($postData['nav']) && $postData['nav'] === 'firefox';
     $extension = $isFirefox ? 'ogg' : 'wav';
     $encodingExt = 'mp3';
     $mimeType = 'audio/' . $encodingExt;
     if (!$this->validateParams($postData, $blob)) {
         array_push($errors, 'one or more request parameters are missing.');
         return array('file' => null, 'errors' => $errors);
     }
     $fileBaseName = $postData['fileName'];
     $uniqueBaseName = $this->claroUtils->generateGuid();
     $hashName = $this->getBaseFileHashName($uniqueBaseName, $workspace) . '.' . $encodingExt;
     $tempAudioFileName = $fileBaseName . '.' . $extension;
     $finalFileName = $uniqueBaseName . '.' . $encodingExt;
     // upload original file in temp upload (ie web/uploads) dir
     $blob->move($this->tempUploadDir, $tempAudioFileName);
     $sourceFilePath = $this->tempUploadDir . DIRECTORY_SEPARATOR . $tempAudioFileName;
     $tempEncodedFilePath = $this->tempUploadDir . DIRECTORY_SEPARATOR . $finalFileName;
     // encode original file (ogg/wav) to mp3
     $cmd = 'avconv -i ' . $sourceFilePath . ' -acodec libmp3lame -ab 128k ' . $tempEncodedFilePath;
     exec($cmd, $output, $returnVar);
     // cmd error
     if ($returnVar !== 0) {
         array_push($errors, 'File conversion failed with command ' . $cmd . ' and returned ' . $returnVar);
         return array('file' => null, 'errors' => $errors);
     }
     // copy the encoded file to user workspace directory
     $fs->copy($tempEncodedFilePath, $targetDir . DIRECTORY_SEPARATOR . $finalFileName);
     // get encoded file size...
     $sFile = new sFile($targetDir . DIRECTORY_SEPARATOR . $finalFileName);
     $size = $sFile->getSize();
     // remove temp encoded file
     @unlink($tempEncodedFilePath);
     // remove original non encoded file from temp dir
     @unlink($sourceFilePath);
     $file = new File();
     $file->setSize($size);
     $file->setName($fileBaseName . '.' . $encodingExt);
     $file->setHashName($hashName);
     $file->setMimeType($mimeType);
     return array('file' => $file, 'errors' => []);
 }
Example #14
0
 private function uploadFile(\DirectoryIterator $file, ResourceNode $parent, array $perms, $published = true)
 {
     $workspaceId = $parent->getWorkspace()->getId();
     $entityFile = new File();
     $fileName = $file->getFilename();
     $size = @filesize($file);
     $extension = pathinfo($fileName, PATHINFO_EXTENSION);
     $mimeType = $this->container->get('claroline.utilities.mime_type_guesser')->guess($extension);
     $hashName = 'WORKSPACE_' . $workspaceId . DIRECTORY_SEPARATOR . $this->container->get('claroline.utilities.misc')->generateGuid() . '.' . $extension;
     $destination = $this->container->getParameter('claroline.param.files_directory') . DIRECTORY_SEPARATOR . $hashName;
     copy($file->getPathname(), $destination);
     $entityFile->setSize($size);
     $entityFile->setName($fileName);
     $entityFile->setHashName($hashName);
     $entityFile->setMimeType($mimeType);
     return $this->resourceManager->create($entityFile, $this->resourceManager->getResourceTypeByName('file'), $this->tokenStorage->getToken()->getUser(), $parent->getWorkspace(), $parent, null, $perms, $published);
 }