From: Vincent Vanwaelscappel Date: Wed, 22 Mar 2023 09:20:59 +0000 (+0100) Subject: wip #5817 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=fe0ce86ff1f5bf66f001bccf706ffe3e09827b25;p=fluidbook-toolbox.git wip #5817 @0.5 --- diff --git a/app/Jobs/FluidbookCollectionDownload.php b/app/Jobs/FluidbookCollectionDownload.php index f4671d8cc..76974023d 100644 --- a/app/Jobs/FluidbookCollectionDownload.php +++ b/app/Jobs/FluidbookCollectionDownload.php @@ -2,8 +2,11 @@ namespace App\Jobs; +use App\Fluidbook\Packager\Packager; +use App\Models\FluidbookPublication; use App\Models\User; use App\Services\WorkshopV2; +use Cubist\Util\CommandLine\Rsync; use Cubist\Util\Files\Files; use Cubist\Util\PHP; use Cubist\Util\Str; @@ -19,7 +22,7 @@ class FluidbookCollectionDownload extends DownloadBase public function handle() { - if ($this->action === 'install_hosting') { + if ($this->action === 'install') { $url = $this->installHosting($this->entry->getPageData()); $this->sendNotification(__('Collection ":title" (#:nb) installée sur le serveur hosting', ['title' => $this->_title(), 'nb' => $this->_id()]), '', $url); } else if ($this->action === 'export' && in_array($this->entry->version, ['win_inss_html', 'win_ins_html'])) { @@ -49,19 +52,14 @@ class FluidbookCollectionDownload extends DownloadBase PHP::neverStop(); if ($data->type === 'scorm_multilang') { $res = $this->compileSCORMMultilang($data, $path); + } else if ($data->type === 'export_multilang') { + $res = $this->compileMultilang($data, $path); } elseif ($data->type === 'export') { $res = $this->compileExport($data, $path); } return $res; } - protected function _getws2() - { - $ws = new WorkshopV2(); - $ws->login($this->user->email, $this->user->api_token); - return $ws; - } - protected function getCollectionGlobalSettings() { $options = []; @@ -75,11 +73,18 @@ class FluidbookCollectionDownload extends DownloadBase return $options; } - /** * @throws \Exception */ protected function compileSCORMMultilang($data, $path) + { + return $this->compileSCORM($data, $path, true); + } + + /** + * @throws \Exception + */ + protected function compileMultilang($data, $path, $scorm = false) { $ws = $this->_getws2(); $langs = []; @@ -87,53 +92,53 @@ class FluidbookCollectionDownload extends DownloadBase $defaultFlags = ['en' => 'gb', 'sv' => 'se']; + $fluidbooks = []; + foreach ($data->publications as $publication) { $fbid = $publication['fluidbook']; - $metadata = $ws->getMetadata($fbid); - $langs[$fbid] = $metadata->lang; - $countries[$fbid] = $metadata->settings->country; + $fluidbooks[$fbid] = FluidbookPublication::find($fbid); + $langs[$fbid] = $fluidbooks[$fbid]->lang; + $countries[$fbid] = $fluidbooks[$fbid]->country; } $options = $this->getCollectionGlobalSettings(); - $options['scorm_enable'] = true; - - if (count($data->publications) >= 2) { + if ($scorm) { + $options['scorm_enable'] = true; + } - if ($data->locale_switch) { - $multilang = []; - foreach ($langs as $fbid => $lang) { - if ($countries[$fbid]) { - $flag = $countries[$fbid]; + if ($data->locale_switch) { + $multilang = []; + foreach ($langs as $fbid => $lang) { + if ($countries[$fbid]) { + $flag = $countries[$fbid]; + } else { + if (strlen($lang) === 5) { + $flag = substr($lang, 3, 2); + } else if (isset($defaultFlags[$lang])) { + $flag = $defaultFlags[$lang]; } else { - if (strlen($lang) === 5) { - $flag = substr($lang, 3, 2); - } else if (isset($defaultFlags[$lang])) { - $flag = $defaultFlags[$lang]; - } else { - $flag = $lang; - } + $flag = $lang; } - $multilang[] = $lang . ',' . strtolower($flag) . ',../' . $lang . '/index.html'; } - $options['multilang'] = implode("\n", $multilang); - $options['multilangDisplay'] = 'lang'; - } else { - $options['multilang'] = ''; + $multilang[] = $lang . ',' . strtolower($flag) . ',../' . $lang . '/index.html'; } + $options['multilang'] = implode("\n", $multilang); + $options['multilangDisplay'] = 'lang'; + } else { + $options['multilang'] = ''; + } - foreach ($data->publications as $publication) { - $fbid = $publication['fluidbook']; - $lang = $langs[$fbid]; - $dir = $path . '/' . $lang; - $ws->installBook($fbid, $dir, $options, 'scorm', 3); - } + foreach ($fluidbooks as $fbid => $publication) { + $this->package($fbid, $scorm ? 'scorm' : $data->version, $options, $path . '/' . $langs[$fbid]); + } - if (in_array('en', $langs)) { - $default = 'en'; - } else { - $default = $langs[0]; - } + if (in_array('en', $langs)) { + $default = 'en'; + } else { + $default = $langs[0]; + } + if ($scorm) { $manifestFile = $path . '/imsmanifest.xml'; foreach ($langs as $lang) { $manifest = $path . '/' . $lang . '/imsmanifest.xml'; @@ -147,107 +152,16 @@ class FluidbookCollectionDownload extends DownloadBase $manifestContent = file_get_contents($manifestFile); $manifestContent = preg_replace('/\(.*)\<\/title\>/U', '' . htmlspecialchars($this->entry->title) . '', $manifestContent); file_put_contents($manifestFile, $manifestContent); - - $redirectionScript = " - - - - -"; - - file_put_contents($path . '/index.html', $redirectionScript); - } else { - foreach ($data->publications as $publication) { - $fbid = $publication['fluidbook']; - $dir = $path; - $ws->installBook($fbid, $dir, $options, 'scorm', 3); - break; - } - } - } - - - protected function installHosting($data) + protected function package($fbid, $version, $options, $path) { - $ws = $this->_getws2(); - $options = $this->getCollectionGlobalSettings(); - - $updatedPublications = []; - $jobs = []; - foreach ($data->publications as $publication) { - $fbid = $publication['fluidbook']; - if (isset($publication['dir']) && $publication['dir']) { - $options['dir'] = $publication['dir']; - } else { - $metadata = $ws->getMetadata($fbid); - $publication['dir'] = $options['dir'] = $metadata->export->install_hosting->online->dir ?? Str::slug($metadata->title); - } - - $job = new FluidbookWS2Download(); - $job->setBookId($fbid); - $job->setVersion('online'); - $job->setOptions($options); - $job->setAction('install_hosting'); - $job->setJobName('install_hosting_' . $fbid); - $u = backpack_user() ?? User::withoutGlobalScopes()->find(5); - $job->setCredentials([$u->email, $u->api_token]); - //$job->handle(); - dispatch($job)->onQueue('ws2'); - $jobs['Fluidbook #' . $fbid] = $job; - $updatedPublications[] = $publication; - } - $this->entry->publications = $updatedPublications; - $this->entry->saveQuietly(); - - while (!$this->_checkJobs($jobs)) { - usleep(1000000 * 0.25); - } - - $res = []; - foreach ($jobs as $label => $job) { - $res[$label] = $job->getResult(); - } - return $res; + $packager = Packager::package($fbid, $version, false, true, $options); + $rsync = new Rsync($packager->getFinalPath(), $path); + $rsync->execute(); } protected function downloadList($data) @@ -339,4 +253,55 @@ window.location='./' + locale + '/index.html'; } } } + + + public static function getRedirectScript($langs, $default = 'en') + { + return " + + + + +"; + } } diff --git a/app/Jobs/FluidbookCompiler.php b/app/Jobs/FluidbookCompiler.php index 0c813fe16..2a4ec48d2 100644 --- a/app/Jobs/FluidbookCompiler.php +++ b/app/Jobs/FluidbookCompiler.php @@ -3629,16 +3629,16 @@ height="0" width="0" style="display:none;visibility:hidden"> } - public function unzipFile($file, $moveAssets = false, $baseDir = null) + public function unzipFile($file, $moveAssets = false, $baseDir = null, $junkPaths = false) { - $fdir = is_null($baseDir) ? 'data/links/' . str_replace('.', '_', $file) : commonTools::fixpath($baseDir); + $fdir = is_null($baseDir) ? 'data/links/' . str_replace('.', '_', $file) : $baseDir; $zipPath = $this->wdir . '/' . $file; - $dir = storage_path('fluidbook/cache/unzip') . Files::hashFileAttributes($zipPath); + $dir = protected_path('fluidbookpublication/cache/unzip') . '/' . Files::hashFileAttributes($zipPath) . '_' . ($moveAssets ? '1' : '0') . '_' . ($junkPaths ? '1' : '0'); if (!file_exists($dir)) { mkdir($dir, 0777, true); - Zip::extract($zipPath, $dir); + Zip::extract($zipPath, $dir, $junkPaths); if ($moveAssets) { `mv $dir/Assets/* $dir`; rmdir($dir . '/Assets'); diff --git a/app/SubForms/Link/Base.php b/app/SubForms/Link/Base.php index 6c5870d05..ba871fc6b 100644 --- a/app/SubForms/Link/Base.php +++ b/app/SubForms/Link/Base.php @@ -92,7 +92,7 @@ class Base extends Form protected $_uid = true; protected static $_acceptImage = ['.jpg', '.jpeg', '.png', '.svg', '.gif']; - protected static $_acceptImageAZip = ['.jpg', '.jpeg', '.png', '.svg', '.gif', '.zip']; + protected static $_acceptImageAndZip = ['.jpg', '.jpeg', '.png', '.svg', '.gif', '.zip']; protected static $_acceptAnimation = ['.jpg', '.jpeg', '.png', '.svg', '.gif', '.zip', '.oam', '.html', '.json', '.pdf']; protected static $_acceptVideo = ['.mp4']; protected static $_acceptFont = ['.otf', '.ttf']; @@ -100,7 +100,7 @@ class Base extends Form protected static $_acceptAudio = ['.mp3']; protected static $_acceptHtml = ['.html']; protected static $_acceptIframe = ['.oam', '.zip', '.html', '.pdf']; - protected static $_acceptSlideshow = ['.zip']; + protected static $_acceptSlideshow = ['.jpg', '.jpeg', '.png', '.svg', '.gif', '.zip']; protected static $_acceptPDF = ['.pdf'];