]> _ Git - fluidbook-toolbox.git/commitdiff
wip #4895 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Nov 2021 10:11:45 +0000 (11:11 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Nov 2021 10:11:45 +0000 (11:11 +0100)
app/Http/Controllers/Admin/Operations/FluidbookCollection/DownloadOperation.php
app/Jobs/FluidbookCollectionDownload.php [new file with mode: 0644]
app/Mail/FluidbookCollectionDownload.php [new file with mode: 0644]
app/Models/FluidbookCollection.php
app/Services/WorkshopV2.php

index 81f6e3892420db0e4e831dbc6c30abf953677ea8..eb3469ef63b17b0b933a4ca9d19f6e89eaabfe9a 100644 (file)
@@ -2,11 +2,10 @@
 
 namespace App\Http\Controllers\Admin\Operations\FluidbookCollection;
 
-
-use Cubist\Util\Files\Files;
-use Cubist\Util\Zip;
+use App\Jobs\FluidbookCollectionDownload;
+use App\Models\FluidbookCollection;
 use Illuminate\Support\Facades\Route;
-use Cubist\Util\Str;
+use Prologue\Alerts\Facades\Alert;
 
 trait DownloadOperation
 {
@@ -22,20 +21,8 @@ trait DownloadOperation
 
     protected function download($id)
     {
-        $compilepath = protected_path('collection/final/' . $id);
-        $entry = $this->crud->getEntry($id);
-        $entry->compile($compilepath);
-
-        $fname = Str::slugCase('collection-' . date('Ymdhis') . '-' . Str::slug($entry->title)) . '.zip';
-        $dest = Files::mkdir(protected_path('collection/download/')) . $fname;
-
-        Zip::archive($compilepath, $dest);
-        if(!file_exists($dest)){
-            throw new \Exception('An error occured while compiling the collection');
-        }
-
-        return response(null)->header('Content-Type', 'application/zip')
-            ->header('Content-Disposition', 'attachment; filename="' . $fname . '"')
-            ->header('X-Sendfile', $dest);
+        FluidbookCollectionDownload::dispatch(FluidbookCollection::find($id), backpack_user());
+        Alert::add('success', __('La compilation a été placée en file d\'attente. Vous recevrez un email lorsqu\'elle sera terminée.'))->flash();
+        return redirect(backpack_url('fluidbook-collection'));
     }
 }
diff --git a/app/Jobs/FluidbookCollectionDownload.php b/app/Jobs/FluidbookCollectionDownload.php
new file mode 100644 (file)
index 0000000..0a87f07
--- /dev/null
@@ -0,0 +1,206 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\FluidbookCollection;
+use App\Models\User;
+use App\Services\WorkshopV2;
+use Cubist\Util\Files\Files;
+use Cubist\Util\PHP;
+use Cubist\Util\Str;
+use Cubist\Util\Zip;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\Mail;
+
+class FluidbookCollectionDownload implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    /**
+     * @var FluidbookCollection
+     */
+    protected $collection;
+
+    /**
+     * @var User
+     */
+    protected $user;
+
+    /**
+     * @param $collection FluidbookCollection
+     * @param $user User
+     */
+    public function __construct($collection, $user)
+    {
+        $this->collection = $collection;
+        $this->user = $user;
+
+    }
+
+    /**
+     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
+     * @throws \Exception
+     */
+    public function handle()
+    {
+        try {
+            $compilepath = protected_path('collection/final/' . $this->collection->id);
+            $this->compile($compilepath);
+
+            $fname = Str::slugCase('collection-' . date('Ymdhis') . '-' . md5(rand(10000, 100000000)) . '-' . Str::slug($this->collection->title)) . '.zip';
+            $dest = Files::mkdir(storage_path('app/public/collection/download/')) . $fname;
+
+            Zip::archive($compilepath, $dest);
+            if (!file_exists($dest)) {
+                throw new \Exception('An error occured while compiling the collection');
+            }
+
+            $url = url('storage/collection/download/' . $fname);
+
+            $subject = __('Collection :nb prête au téléchargement', ['nb' => $this->collection->id]);
+            $body = __('Le fichier est disponible à l\'adresse suivante : <a href=":url">:url</a>', ['url' => $url]);
+        } catch (\Exception $e) {
+            $subject = __('Erreur lors de la compilation de la collection :nb', ['nb' => $this->collection->id]);
+            $body = __('Détails de l\'erreur :message', ['message' => $e->getMessage() . ' at line ' . $e->getLine() . ' of ' . $e->getFile()]);
+        }
+
+        $mail = new \App\Mail\FluidbookCollectionDownload();
+        $mail->to($this->user->email);
+        $mail->subject($subject);
+        $mail->html($body);
+        Mail::send($mail);
+    }
+
+    public function compile($path)
+    {
+        $data = $this->collection->getPageData();
+        $path = Files::emptyDir($path);
+
+        PHP::neverStop();
+
+        if ($data->type === 'scorm_multilang') {
+            $res = $this->compileSCORMMultilang($data, $path);
+        } elseif ($data->type === 'export') {
+            $res = $this->compileExport($data, $path);
+        }
+
+        return $res;
+    }
+
+    protected function _getws2()
+    {
+        $ws = new WorkshopV2($this->user);
+        $ws->login($this->user->email, $this->user->api_token);
+        return $ws;
+    }
+
+    /**
+     * @throws \Exception
+     */
+    protected function compileSCORMMultilang($data, $path)
+    {
+        $ws = $this->_getws2();
+        $langs = [];
+        foreach ($data->publications as $publication) {
+            $fbid = $publication['fluidbook'];
+            $metadata = $ws->getMetadata($fbid);
+            $langs[] = $metadata->lang;
+            $dir = $path . '/' . $metadata->lang;
+            $ws->installBook($fbid, $dir, 'scorm', 3);
+        }
+
+        if (in_array('en', $langs)) {
+            $default = 'en';
+        } else {
+            $default = $langs[0];
+        }
+
+        foreach ($langs as $lang) {
+            $manifest = $path . '/' . $lang . '/imsmanifest.xml';
+            if ($lang === $default) {
+                rename($manifest, $path . '/imsmanifest.xml');
+            } else {
+                unlink($manifest);
+            }
+        }
+
+        $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]);
+        }
+    }
+
+   return res;
+}
+
+var locale=guessPreferedLanguage(" . json_encode($langs) . ",'" . $default . "');
+window.location='./' + locale + '/index.html';
+</script>
+</body>
+</html>";
+
+        file_put_contents($path . '/index.html', $redirectionScript);
+    }
+
+    protected function compileExport($data, $path)
+    {
+        $ws = $this->_getws2();
+
+        $zipmerge = in_array($data->version, ['online', 'scorm', 'sharepoint', 'precompiled', 'win_exe_html', 'win_html', 'win_cd_html', 'mac_exe_html']);
+        foreach ($data->publications as $publication) {
+            $fbid = $publication['fluidbook'];
+            $metadata = $ws->getMetadata($fbid);
+
+            $dir = $path . '/';
+            if ($publication['export']) {
+                $dir .= $publication['export'];
+            } else {
+                $dir .= Str::slug($metadata->title);
+            }
+            if (!$zipmerge) {
+                $dir .= '.exe';
+            }
+            if ($zipmerge) {
+                $ws->installBook($fbid, $dir, $data->version, 3);
+            } else {
+                $ws->downloadBookExport($fbid, $dir, $data->version, 3);
+            }
+        }
+    }
+}
diff --git a/app/Mail/FluidbookCollectionDownload.php b/app/Mail/FluidbookCollectionDownload.php
new file mode 100644 (file)
index 0000000..1177495
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Mail;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Mail\Mailable;
+use Illuminate\Queue\SerializesModels;
+
+class FluidbookCollectionDownload extends Mailable
+{
+    use Queueable, SerializesModels;
+
+    public function build()
+    {
+        return $this;
+    }
+
+}
index a6dc8119e5e123900967e45134a242d63c9b2080..e95e1b31d97794c30fc88ed22c5c618e3f88cbb0 100644 (file)
@@ -12,6 +12,7 @@ use Cubist\Backpack\Magic\Fields\SelectFromArray;
 use Cubist\Backpack\Magic\Fields\Text;
 use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
 use Cubist\Util\Files\Files;
+use Cubist\Util\PHP;
 use Cubist\Util\Str;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Support\Facades\Auth;
@@ -57,130 +58,5 @@ class FluidbookCollection extends CubistMagicAbstractModel
         $this->addField('publications', BunchOfFieldsMultiple::class, __('Publications'), ['bunch' => CollectionPublication::class]);
     }
 
-    public function compile($path)
-    {
-        $data = $this->getPageData();
-
-        $path = Files::emptyDir($path);
-
-        if ($data->type === 'scorm_multilang') {
-            return $this->compileSCORMMultilang($data, $path);
-        } elseif ($data->type === 'export') {
-            return $this->compileExport($data, $path);
-        }
-    }
-
-    protected function _getws2()
-    {
-        $ws = new WorkshopV2();
-        $user = backpack_user();
-        $ws->login($user->email, $user->api_token);
-        return $ws;
-    }
-
-    /**
-     * @throws \Exception
-     */
-    protected function compileSCORMMultilang($data, $path)
-    {
-        $ws = $this->_getws2();
-        $langs = [];
-        foreach ($data->publications as $publication) {
-            $fbid = $publication['fluidbook'];
-            $metadata = $ws->getMetadata($fbid);
-            $langs[] = $metadata->lang;
-            $dir = $path . '/' . $metadata->lang;
-            $ws->installBook($fbid, $dir, 'scorm', 3);
-        }
-
-        if (in_array('en', $langs)) {
-            $default = 'en';
-        } else {
-            $default = $langs[0];
-        }
-
-        foreach ($langs as $lang) {
-            $manifest = $path . '/' . $lang . '/imsmanifest.xml';
-            if ($lang === $default) {
-                rename($manifest, $path . '/imsmanifest.xml');
-            } else {
-                unlink($manifest);
-            }
-        }
-
-        $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]);
-        }
-    }
-
-   return res;
-}
-
-var locale=guessPreferedLanguage(" . json_encode($langs) . ",'" . $default . "');
-window.location='./' + locale + '/index.html';
-</script>
-</body>
-</html>";
-
-        file_put_contents($path . '/index.html', $redirectionScript);
-    }
-
-    protected function compileExport($data, $path)
-    {
-        $ws = $this->_getws2();
-
-        $zipmerge = in_array($data->version, ['online', 'scorm', 'sharepoint', 'precompiled', 'win_exe_html', 'win_html', 'win_cd_html', 'mac_exe_html']);
-        foreach ($data->publications as $publication) {
-            $fbid = $publication['fluidbook'];
-            $metadata = $ws->getMetadata($fbid);
-
-            $dir = $path . '/';
-            if ($publication['export']) {
-                $dir .= $publication['export'];
-            } else {
-                $dir .= Str::slug($metadata->title);
-            }
-            if (!$zipmerge) {
-                $dir .= '.exe';
-            }
-            if ($zipmerge) {
-                $ws->installBook($fbid, $dir, $data->version, 3);
-            } else {
-                $ws->downloadBookExport($fbid, $dir, $data->version, 3);
-            }
-        }
-    }
 }
index 617e3eea392371760493c81339bb84bd034b1aa0..bd6f3919cbd383b94a3c9e257870a9cdba67659f 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App\Services;
 
+use App\Models\User;
 use Cubist\Util\Files\Files;
 use Cubist\Util\Zip;
 use Exception;
@@ -21,9 +22,15 @@ class WorkshopV2
     /** @var string */
     protected $_domain = 'https://workshop.fluidbook.com/';
 
-    public function __construct()
+    /**
+     * @var User
+     */
+    protected $user;
+
+    public function __construct($user)
     {
-        $this->_cookies = new FileCookieJar(Files::mkdir(protected_path('ws2cookies/')) . backpack_user()->id, true);
+        $this->user=$user;
+        $this->_cookies = new FileCookieJar(Files::mkdir(protected_path('ws2cookies/')) . $this->user->id, true);
         $this->_http = new Client(['base_uri' => $this->_domain, 'timeout' => 60000, 'read_timeout' => 60000, 'cookies' => $this->_cookies]);
     }