]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5817 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 Mar 2023 09:20:59 +0000 (10:20 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 Mar 2023 09:20:59 +0000 (10:20 +0100)
app/Jobs/FluidbookCollectionDownload.php
app/Jobs/FluidbookCompiler.php
app/SubForms/Link/Base.php

index f4671d8cc93f263ac3350f7848c441ce62f710ab..76974023d0f571d83e915259c85fd0466946eb04 100644 (file)
@@ -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\>(.*)\<\/title\>/U', '<title>' . htmlspecialchars($this->entry->title) . '</title>', $manifestContent);
             file_put_contents($manifestFile, $manifestContent);
-
-            $redirectionScript = "<html>
-<head></head>
-<body>
-<script type=\"text/javascript\">
-function guessPreferedLanguage(available, defaultLanguage) {
-    if (defaultLanguage == undefined) {
-        defaultLanguage = available[0];
-    }
-
-   var browserLanguages = getLanguagesFromBrowser();
-    var res = null;
-
-   for (var i = 0; i < browserLanguages.length; i++) {
-        var bl = browserLanguages[i];
-        if (available.indexOf(bl) >= 0) {
-            res = bl;
-            break;
         }
-    }
-    if (res == null) {
-        return defaultLanguage;
-    }
 
-   return res;
-}
-
-function getLanguagesFromBrowser() {
-    var res = [];
-
-   var navigatorLanguages = navigator.languages || [navigator.language || navigator.userLanguage];
-    for(var i in navigatorLanguages){
-    var v=navigatorLanguages[i];
-    var e = v.split('-');
-        if (res.indexOf(e[0]) === -1) {
-            res.push(e[0]);
-        }
+        file_put_contents($path . '/index.html', self::getRedirectScript($langs, $default));
     }
 
-   return res;
-}
-
-var locale=guessPreferedLanguage(" . json_encode(array_values($langs)) . ",'" . $default . "');
-window.location='./' + locale + '/index.html';
-</script>
-</body>
-</html>";
-
-            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 "<html>
+<head></head>
+<body>
+<script type=\"text/javascript\">
+function guessPreferedLanguage(available, defaultLanguage) {
+    if (defaultLanguage == undefined) {
+        defaultLanguage = available[0];
+    }
+
+   var browserLanguages = getLanguagesFromBrowser();
+    var res = null;
+
+   for (var i = 0; i < browserLanguages.length; i++) {
+        var bl = browserLanguages[i];
+        if (available.indexOf(bl) >= 0) {
+            res = bl;
+            break;
+        }
+    }
+    if (res == null) {
+        return defaultLanguage;
+    }
+
+   return res;
+}
+
+function getLanguagesFromBrowser() {
+    var res = [];
+
+   var navigatorLanguages = navigator.languages || [navigator.language || navigator.userLanguage];
+    for(var i in navigatorLanguages){
+    var v=navigatorLanguages[i];
+    var e = v.split('-');
+        if (res.indexOf(e[0]) === -1) {
+            res.push(e[0]);
+        }
+    }
+
+   return res;
+}
+
+var locale=guessPreferedLanguage(" . json_encode(array_values($langs)) . ",'" . $default . "');
+window.location='./' + locale + '/index.html';
+</script>
+</body>
+</html>";
+    }
 }
index 0c813fe1629f2bb03f9c8d6437fb5589bb5e9089..2a4ec48d2ca07ee89dcf14dd861498f0bc2609a2 100644 (file)
@@ -3629,16 +3629,16 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
 
     }
 
-    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');
index 6c5870d05e65a1412d48fec4c1292cf6396e1d32..ba871fc6b40ed035a1b0370c792c5b138b8cf8ec 100644 (file)
@@ -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'];