]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6186 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 21 Sep 2023 18:09:39 +0000 (20:09 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 21 Sep 2023 18:09:39 +0000 (20:09 +0200)
app/Http/Controllers/Admin/Operations/ELearningMedia/DownloadOperation.php
app/Http/Controllers/Admin/Operations/ELearningPackage/DownloadOperation.php
app/Jobs/Maintenance/CheckPublicationsHashAndCid.php
app/Models/ELearningMedia.php
app/Models/ELearningPackage.php
app/Models/FluidbookPublication.php
app/Models/Quiz.php
app/Models/Traits/CheckHash.php

index 6105a4cd7bcca3bb735303567924706d691f1639..296d2f425797dd78e4d54033fa383509bc7b9150 100644 (file)
@@ -4,8 +4,12 @@ namespace App\Http\Controllers\Admin\Operations\ELearningMedia;
 
 // __('!! e-Learning')
 
+use App\Http\Middleware\CheckIfAdmin;
 use App\Jobs\ElearningMediaDownload;
+use App\Jobs\QuizDownload;
 use App\Models\ELearningMedia;
+use App\Models\Quiz;
+use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
 use Illuminate\Support\Facades\Route;
 use Prologue\Alerts\Facades\Alert;
 
@@ -13,7 +17,8 @@ trait DownloadOperation
 {
     protected function setupDownloadRoutes($segment, $routeName, $controller)
     {
-        Route::match(['get'], $segment . '/{id}/download/{action}', $controller . '@download');
+        Route::match(['get'], $segment . '/{id}/package/{action}/{version?}', $controller . '@package');
+        Route::match(['get'], $segment . '/{id}_{hash}/download/{file}', $controller . '@download')->withoutMiddleware([CheckIfAdmin::class]);;
     }
 
     protected function setupDownloadDefaults()
@@ -22,10 +27,35 @@ trait DownloadOperation
         $this->crud->addButtonFromView('line', 'download', 'elearningmedia.download', 'end');
     }
 
-    protected function download($id, $action)
+    protected function package($id, $action, $version = null)
     {
-        ElearningMediaDownload::dispatch(ELearningMedia::find($id), $action, backpack_user())->onQueue('download');;
+        if (!ElearningMedia::hasPermission($id, 'read')) {
+            abort(401);
+        }
+        $media = ElearningMedia::withoutGlobalScopes()->find($id);
+        $version = $version ?? $media->scorm_version;
+        ElearningMediaDownload::dispatch($media, $action, $version, backpack_user())->onQueue('download');;
         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('elearning-media'));
     }
+
+    protected function download($id, $hash, $file)
+    {
+        $media = ElearningMedia::withoutGlobalScopes()->where('id', $id)->where('hash', $hash)->first();
+        if (null === $media) {
+            abort(404, __('Ce fichier est indisponible'));
+        }
+        $path = protected_path('elearning-media/download/' . $id . '/' . $file);
+        if (!file_exists($path)) {
+            abort(404, __('Le fichier n\'existe pas'));
+        }
+        return XSendFileController::sendfile($path);
+    }
+
+//    protected function download($id, $action)
+//    {
+//        ElearningMediaDownload::dispatch(ELearningMedia::find($id), $action, backpack_user())->onQueue('download');;
+//        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('elearning-media'));
+//    }
 }
index 34bc450d575c0dc26f9635ff74f04217e9aff953..ab4dca292acd6abacb0aba10aafd23dea3be8213 100644 (file)
@@ -2,8 +2,10 @@
 
 namespace App\Http\Controllers\Admin\Operations\ELearningPackage;
 
+use App\Http\Middleware\CheckIfAdmin;
 use App\Jobs\ElearningPackageDownload;
 use App\Models\ELearningPackage;
+use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
 use Illuminate\Support\Facades\Route;
 use Prologue\Alerts\Facades\Alert;
 
@@ -13,7 +15,8 @@ trait DownloadOperation
 {
     protected function setupDownloadRoutes($segment, $routeName, $controller)
     {
-        Route::match(['get'], $segment . '/{id}/download/{action}', $controller . '@download');
+        Route::match(['get'], $segment . '/{id}/package/{action}/{version?}', $controller . '@package');
+        Route::match(['get'], $segment . '/{id}_{hash}/download/{file}', $controller . '@download')->withoutMiddleware([CheckIfAdmin::class]);;
     }
 
     protected function setupDownloadDefaults()
@@ -21,10 +24,35 @@ trait DownloadOperation
         $this->crud->addButtonFromView('line', 'download', 'elearningpackage.download', 'end');
     }
 
-    protected function download($id, $action)
+    protected function package($id, $action, $version = null)
     {
-        ElearningPackageDownload::dispatch(ELearningPackage::find($id), $action, backpack_user())->onQueue('download');
+        if (!ElearningPackage::hasPermission($id, 'read')) {
+            abort(401);
+        }
+        $media = ElearningPackage::withoutGlobalScopes()->find($id);
+        $version = $version ?? $media->scorm_version;
+        ElearningPackageDownload::dispatch($media, $action, $version, backpack_user())->onQueue('download');;
         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('elearning-package'));
     }
+
+    protected function download($id, $hash, $file)
+    {
+        $media = ElearningPackage::withoutGlobalScopes()->where('id', $id)->where('hash', $hash)->first();
+        if (null === $media) {
+            abort(404, __('Ce fichier est indisponible'));
+        }
+        $path = protected_path('elearningmedia/download/' . $id . '/' . $file);
+        if (!file_exists($path)) {
+            abort(404, __('Le fichier n\'existe pas'));
+        }
+        return XSendFileController::sendfile($path);
+    }
+
+//    protected function download($id, $action)
+//    {
+//        ElearningPackageDownload::dispatch(ELearningPackage::find($id), $action, backpack_user())->onQueue('download');
+//        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('elearning-package'));
+//    }
 }
index a3ed706cde174acbcf290cb34b0dcbb562616f6a..9c038c77c18cc4ce2c793cce1ced54d6dd909149 100644 (file)
@@ -3,6 +3,8 @@
 namespace App\Jobs\Maintenance;
 
 use App\Jobs\Base;
+use App\Models\ELearningMedia;
+use App\Models\ELearningPackage;
 use App\Models\FluidbookPublication;
 use App\Models\Quiz;
 use Illuminate\Support\Facades\Log;
@@ -11,20 +13,29 @@ class CheckPublicationsHashAndCid extends Base
 {
     public function handle()
     {
+        $classes = ['Quiz' => Quiz::class, 'Elearning Media' => ELearningMedia::class, 'Elearning Package' => ELearningPackage::class];
+
+        foreach ($classes as $name => $class) {
+            $class::withoutGlobalScopes()->where('created_ok', '0')->forceDelete();
+            foreach ($class::withoutGlobalScopes()->where(function ($query) {
+                $query->whereNull('hash')->orWhere('hash', '');
+            })->get() as $instance) {
+                //Log::warning($name . ' #' . $instance->id . ' had empty hash or cid (hash: hash , cid: :cid)', ['hash' => $instance->hash]);
+                $instance->save();
+            };
+
+
+        }
+
         /** @var FluidbookPublication $fluidbook */
-        foreach (FluidbookPublication::withoutGlobalScopes()->where('created_ok', '1')->where(function ($query) {
+        FluidbookPublication::withoutGlobalScopes()->where('created_ok', '0')->forceDelete();
+        foreach (FluidbookPublication::withoutGlobalScopes()->where(function ($query) {
             $query->whereNull('hash')->orWhere('hash', '')->orWhereNull('cid')->orWhere('cid', '');
         })->get() as $fluidbook) {
-            Log::warning('Fluidbook #' . $fluidbook->id . ' had empty hash or cid (hash: hash , cid: :cid)', ['hash' => $fluidbook->hash, 'cid' => $fluidbook->cid]);
+            //Log::warning('Fluidbook #' . $fluidbook->id . ' had empty hash or cid (hash: hash , cid: :cid)', ['hash' => $fluidbook->hash, 'cid' => $fluidbook->cid]);
             $fluidbook->save();
         }
 
-        /** @var Quiz $fluidbook */
-        foreach (Quiz::withoutGlobalScopes()->where('created_ok', '1')->where(function ($query) {
-            $query->whereNull('hash')->orWhere('hash', '');
-        })->get() as $quiz) {
-            Log::warning('Quiz #' . $quiz->id . ' had empty hash or cid (hash: hash , cid: :cid)', ['hash' => $quiz->hash]);
-            $quiz->save();
-        }
+
     }
 }
index ba5cfc7a0330990933e94725975ebdeb318b625c..9b781559324ea1c98ee5a55515ff88d4f8c1dc70 100644 (file)
@@ -8,6 +8,7 @@ use App\Http\Controllers\Admin\Operations\ELearningMedia\ImportOperation;
 use App\Http\Controllers\Admin\Operations\ELearningMedia\PreviewOperation;
 use App\Http\Controllers\Admin\Operations\ELearningMedia\DownloadOperation;
 use App\Models\Base\ToolboxModel;
+use App\Models\Traits\CheckHash;
 use App\Models\Traits\SCORMVersionTrait;
 use Cubist\Backpack\Magic\Fields\Files;
 use Cubist\Backpack\Magic\Fields\Hidden;
@@ -22,6 +23,7 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media;
 class ELearningMedia extends ToolboxModel
 {
     use SCORMVersionTrait;
+    use CheckHash;
 
     protected $table = 'elearning_media';
 
@@ -39,6 +41,8 @@ class ELearningMedia extends ToolboxModel
     {
         parent::setFields();
 
+        $this->addHashField();
+
         $this->addField(['name' => 'client',
             'label' => __('Nom du client'),
             'type' => 'Text',
@@ -150,6 +154,12 @@ class ELearningMedia extends ToolboxModel
         return $title;
     }
 
+    public function onSaving(): bool
+    {
+        $this->checkHash();
+        return parent::onSaving();
+    }
+
     public function getIdTitleAttribute()
     {
         return $this->id . ' - ' . $this->title;
index 803e39fdaf13d281df22c81bd70e2fb5c822f071..b5d2ae68e7af4bf287c9f0ac8461921c79414498 100644 (file)
@@ -8,6 +8,7 @@ use App\Http\Controllers\Admin\Operations\ELearningPackage\ImportOperation;
 use App\Http\Controllers\Admin\Operations\ELearningPackage\PreviewOperation;
 use App\Http\Controllers\Admin\Operations\ELearningPackage\DownloadOperation;
 use App\Models\Base\ToolboxModel;
+use App\Models\Traits\CheckHash;
 use App\Models\Traits\SCORMVersionTrait;
 use App\Services\WorkshopV2;
 use App\SubForms\ElearningPackageContent;
@@ -20,10 +21,12 @@ use Cubist\Util\Files\Files;
 use Cubist\Util\Files\VirtualDirectory;
 use Cubist\Util\Zip;
 use DirectoryIterator;
+
 // __('!! e-Learning')
 class ELearningPackage extends ToolboxModel
 {
     use SCORMVersionTrait;
+    use CheckHash;
 
     protected $table = 'elearning_package';
 
@@ -33,12 +36,14 @@ class ELearningPackage extends ToolboxModel
 
     protected static $_permissionBase = 'elearning-package';
 
-    protected $_operations = [ImportOperation::class, PreviewOperation::class, DownloadOperation::class,ChangeownerOperation::class];
+    protected $_operations = [ImportOperation::class, PreviewOperation::class, DownloadOperation::class, ChangeownerOperation::class];
 
     public function setFields()
     {
         parent::setFields();
 
+        $this->addHashField();
+
         $this->addField(['name' => 'client',
             'label' => __('Nom du client'),
             'type' => 'Text',
@@ -158,6 +163,12 @@ class ELearningPackage extends ToolboxModel
         return $res;
     }
 
+    public function onSaving(): bool
+    {
+        $this->checkHash();
+        return parent::onSaving();
+    }
+
     /**
      * @param $id int
      * @param $vdir VirtualDirectory
index 3da16f38d9f2e5d45cfde370435c8bc634a0415c..f251e3924082b92155039f570a3c041e91e32f33 100644 (file)
@@ -137,7 +137,7 @@ class FluidbookPublication extends ToolboxSettingsModel
 
         $this->_main();
 
-        $this->addField('hash', Hidden::class);
+        $this->addHashField();
         $this->addField('cid', Hidden::class);
 
 
index cde87f9c53e0a0cd5810204d6b8df56903652652..10db4324992db18d67b2ba681129a5629329b510 100644 (file)
@@ -93,7 +93,7 @@ class Quiz extends ToolboxModel
     {
         parent::setFields();
 
-        $this->addField('hash', Hidden::class);
+        $this->addHashField();
 
         $this->addField(['name' => 'client',
             'label' => __('Nom du client'),
index b9ffa79e3ce3b4a391ecb42839a908ceb2bd35a6..98487fbc660a0ad364fe2da4d9a88e43c518e0f3 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace App\Models\Traits;
 
+use Cubist\Backpack\Magic\Fields\Hidden;
+
 trait CheckHash
 {
     protected function checkHash()
@@ -10,4 +12,8 @@ trait CheckHash
             $this->hash = md5(uniqid('fluidbook_hash'), false);
         }
     }
+
+    protected function addHashField(){
+        $this->addField('hash', Hidden::class);
+    }
 }