From ab8d12f5139dbd1a69d44b4df7f9c5ce98090d88 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 22 Nov 2021 11:11:45 +0100 Subject: [PATCH] wip #4895 @1 --- .../FluidbookCollection/DownloadOperation.php | 25 +-- app/Jobs/FluidbookCollectionDownload.php | 206 ++++++++++++++++++ app/Mail/FluidbookCollectionDownload.php | 18 ++ app/Models/FluidbookCollection.php | 126 +---------- app/Services/WorkshopV2.php | 11 +- 5 files changed, 240 insertions(+), 146 deletions(-) create mode 100644 app/Jobs/FluidbookCollectionDownload.php create mode 100644 app/Mail/FluidbookCollectionDownload.php diff --git a/app/Http/Controllers/Admin/Operations/FluidbookCollection/DownloadOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookCollection/DownloadOperation.php index 81f6e3892..eb3469ef6 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookCollection/DownloadOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookCollection/DownloadOperation.php @@ -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 index 000000000..0a87f07b7 --- /dev/null +++ b/app/Jobs/FluidbookCollectionDownload.php @@ -0,0 +1,206 @@ +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 : :url', ['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 = " + + + + +"; + + 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 index 000000000..1177495d2 --- /dev/null +++ b/app/Mail/FluidbookCollectionDownload.php @@ -0,0 +1,18 @@ +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 = " - - - - -"; - - 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/Services/WorkshopV2.php b/app/Services/WorkshopV2.php index 617e3eea3..bd6f3919c 100644 --- a/app/Services/WorkshopV2.php +++ b/app/Services/WorkshopV2.php @@ -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]); } -- 2.39.5