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
{
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'));
}
}
--- /dev/null
+<?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);
+ }
+ }
+ }
+}
--- /dev/null
+<?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;
+ }
+
+}
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;
$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);
- }
- }
- }
}
namespace App\Services;
+use App\Models\User;
use Cubist\Util\Files\Files;
use Cubist\Util\Zip;
use Exception;
/** @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]);
}