/**
  * Save resource action, save regions and there configuration and ressource options.
  *
  * @Route("/save/{id}", requirements={"id" = "\d+"}, name="media_resource_save")
  * @Method("POST")
  */
 public function save(Workspace $workspace, MediaResource $resource)
 {
     $data = $this->container->get('request')->request->all();
     $this->get('innova_media_resource.manager.media_resource_options')->update($resource->getOptions(), $data);
     $this->get('innova_media_resource.manager.media_resource_region')->updateRegions($resource, $data);
     return new JsonResponse($resource);
 }
 /**
  * Fired when a ResourceNode of type MediaResource is duplicated.
  *
  * @DI\Observe("copy_innova_media_resource")
  *
  * @param \Claroline\CoreBundle\Event\CopyResourceEvent $event
  *
  * @throws \Exception
  */
 public function onCopy(CopyResourceEvent $event)
 {
     $toCopy = $event->getResource();
     $new = new MediaResource();
     $new->setName($toCopy->getName());
     // copy options
     $this->container->get('innova_media_resource.manager.media_resource')->copyOptions($new, $toCopy);
     // duplicate media resource media(s) (=file(s))
     $medias = $toCopy->getMedias();
     foreach ($medias as $media) {
         $this->container->get('innova_media_resource.manager.media_resource')->copyMedia($new, $media);
     }
     // duplicate regions and region config
     $regions = $toCopy->getRegions();
     foreach ($regions as $region) {
         $this->container->get('innova_media_resource.manager.media_resource_region')->copyRegion($new, $region);
     }
     $event->setCopy($new);
     $event->stopPropagation();
 }
 public function exportToZip(MediaResource $resource, $data)
 {
     $files = [];
     // get original file url
     $url = $resource->getMedias()[0]->getUrl();
     $originalFileFullPath = $this->container->getParameter('claroline.param.files_directory') . DIRECTORY_SEPARATOR . $url;
     // ensure the name is clean
     $cleanName = preg_replace('/[^A-Za-z0-9]/', '', $resource->getName());
     // create temp_dir
     $tempDir = $this->uploadDir . DIRECTORY_SEPARATOR . $cleanName . '_temp';
     $fs = new Filesystem();
     if (!$fs->exists($tempDir)) {
         $fs->mkdir($tempDir);
     }
     // copy original file
     // get original file extension
     $ext = pathinfo($originalFileFullPath, PATHINFO_EXTENSION);
     $fullFileName = $cleanName . '_full_file.' . $ext;
     $copiedFilePath = $tempDir . DIRECTORY_SEPARATOR . $fullFileName;
     // create srt file
     $vttFile = $tempDir . DIRECTORY_SEPARATOR . $cleanName . '_SRT.vtt';
     $fs->touch($vttFile);
     $vtt = '';
     // make a copy of the file
     if (copy($originalFileFullPath, $copiedFilePath)) {
         // create chuncked audio files in temp dir
         $index = 1;
         array_push($files, $copiedFilePath);
         $vtt .= 'WEBVTT' . PHP_EOL;
         foreach ($data as $region) {
             $start = $region['start'];
             $end = $region['end'];
             $duration = $end - $start;
             // create .vtt line
             $vtt .= PHP_EOL;
             $vtt .= $index . PHP_EOL;
             $vtt .= $this->secondsToSrtTime($start) . ' --> ' . $this->secondsToSrtTime($end) . PHP_EOL;
             if ($region['note'] !== '') {
                 $vtt .= strip_tags($region['note']);
             }
             $vtt .= PHP_EOL;
             $partFilePath = $tempDir . DIRECTORY_SEPARATOR . $cleanName . '_part_' . $index . '.' . $ext;
             $cmd = 'ffmpeg -i ' . $copiedFilePath . ' -ss ' . $start . ' -t ' . $duration . ' ' . $partFilePath;
             exec($cmd, $output, $returnVar);
             ++$index;
             // cmd success
             if (count($output) === 0 && $returnVar === 0) {
                 array_push($files, $partFilePath);
             } else {
                 // @TODO do something in case of cmd error
             }
         }
         file_put_contents($vttFile, $vtt);
         array_push($files, $vttFile);
     }
     $zipName = $cleanName . '.zip';
     $archive = new \ZipArchive();
     $pathToArchive = $tempDir . DIRECTORY_SEPARATOR . $zipName;
     $archive->open($pathToArchive, \ZipArchive::CREATE);
     foreach ($files as $f) {
         $archive->addFromString(basename($f), file_get_contents($f));
     }
     $archive->close();
     $fs->remove($files);
     return ['zip' => $pathToArchive, 'name' => $zipName, 'tempFolder' => $tempDir];
 }