From ad4a8ecc7e5613418a5dfddf83fe392492a69559 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Fri, 11 Feb 2022 16:40:35 +0200 Subject: add doc for laravel builder dynamic where handler --- src/Manager/ScormManager.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src') diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index 56c723c..935bb2d 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -60,6 +60,31 @@ class ScormManager throw new InvalidScormArchiveException('invalid_scorm_data'); } + /** + * ScormModel::whereOriginFile Query Builder style equals ScormModel::where('origin_file',$value) + * + * From Laravel doc https://laravel.com/docs/5.0/queries#advanced-wheres. + * Dynamic Where Clauses + * You may even use "dynamic" where statements to fluently build where statements using magic methods: + * + * Examples: + * + * $admin = DB::table('users')->whereId(1)->first(); + * + * $john = DB::table('users') + * ->whereIdAndEmail(2, 'john@doe.com') + * ->first(); + * + * $jane = DB::table('users') + * ->whereNameOrAge('Jane', 22) + * ->first(); + * + * + * From laravel framework https://github.com/laravel/framework/blob/9.x/src/Illuminate/Database/Query/Builder.php' + * + * Handle dynamic method calls into the method. + * return $this->dynamicWhere($method, $parameters); + **/ $scorm = ScormModel::whereOriginFile($scormData['identifier']); // Check if scom package already exists to drop old one. if (!$scorm->exists()) { -- cgit v1.2.3 From f03f57dcbbbf34feb4eb03bba7ea73dfe9b182b3 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Sat, 19 Feb 2022 21:00:00 +0200 Subject: feat(disk): add download scorm package zip. --- src/Manager/ScormDisk.php | 16 ++++++++++++++++ src/Manager/ScormManager.php | 5 +++++ 2 files changed, 21 insertions(+) (limited to 'src') diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index 249f027..9417ef3 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -4,8 +4,10 @@ namespace Peopleaps\Scorm\Manager; use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Facades\Storage; +use Peopleaps\Scorm\Entity\Scorm; use Peopleaps\Scorm\Exception\StorageNotFoundException; use ZipArchive; +use ZipStream\ZipStream; class ScormDisk { @@ -42,6 +44,20 @@ class ScormDisk return true; } + public function download(Scorm $scorm) + { + return response()->stream(function () use ($scorm) { + // enable output of HTTP headers + // $options = new ZipStream\Option\Archive(); + // $options->setSendHttpHeaders(true); + $zip = new ZipStream($scorm->title . ".zip"); + /** @var FilesystemAdapter $disk */ + $disk = $this->getDisk(); + $zip->addFileFromStream($scorm->title, $disk->readStream($scorm->uuid)); + $zip->finish(); + }); + } + /** * @param string $directory * @return bool diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index 935bb2d..aedfae3 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -667,4 +667,9 @@ class ScormManager return $formattedValue; } + + + public function download(Scorm $scorm){ + return $this->scormDisk->download($scorm); + } } -- cgit v1.2.3 From 907801d4d0526a7c1a6160b2e0baa9de89ca38e7 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Sat, 19 Feb 2022 21:49:54 +0200 Subject: support Scorm entity from model --- src/Entity/Scorm.php | 13 +++++++++++++ src/Model/ScormModel.php | 7 +++++++ 2 files changed, 20 insertions(+) (limited to 'src') diff --git a/src/Entity/Scorm.php b/src/Entity/Scorm.php index 48fd711..2a59a29 100644 --- a/src/Entity/Scorm.php +++ b/src/Entity/Scorm.php @@ -3,6 +3,8 @@ namespace Peopleaps\Scorm\Entity; +use Peopleaps\Scorm\Model\ScormModel; + class Scorm { const SCORM_12 = 'scorm_12'; @@ -17,6 +19,17 @@ class Scorm public $scos; public $scoSerializer; + public static function fromModel(ScormModel $model) + { + $instance = new self(); + $instance->setId($model->id); + $instance->setUuid($model->uuid); + $instance->setTitle($model->title); + $instance->setVersion($model->version); + $instance->setEntryUrl($model->entryUrl); + return $instance; + } + /** * @return string */ diff --git a/src/Model/ScormModel.php b/src/Model/ScormModel.php index d5c8499..2777038 100644 --- a/src/Model/ScormModel.php +++ b/src/Model/ScormModel.php @@ -6,6 +6,13 @@ namespace Peopleaps\Scorm\Model; use Illuminate\Database\Eloquent\Model; +/** + * @property int $id + * @property string $uuid + * @property string $title + * @property string $version + * @property string $entryUrl + */ class ScormModel extends Model { public function getTable() -- cgit v1.2.3 From 7303302c322e105b5095bf412e8d154971088282 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Wed, 23 Feb 2022 16:13:24 +0200 Subject: handle read scorm archive from uri, in case have two differet storage for package archive and unzipped package for serve. --- src/Manager/ScormDisk.php | 90 ++++++++++++++++++++++++++------------------ src/Manager/ScormManager.php | 74 ++++++++++++++++++++---------------- 2 files changed, 96 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index 9417ef3..cb09db5 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -3,59 +3,56 @@ namespace Peopleaps\Scorm\Manager; use Illuminate\Filesystem\FilesystemAdapter; +use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; -use Peopleaps\Scorm\Entity\Scorm; use Peopleaps\Scorm\Exception\StorageNotFoundException; -use ZipArchive; -use ZipStream\ZipStream; class ScormDisk { /** * Extract zip file into destination directory. * - * @param string $path Destination directory - * @param string $zipFilePath The path to the zip file. + * @param UploadedFile|string $file zip source + * @param string $path The path to the destination. * - * @return bool True on success, false on failure. + * @return bool true on success, false on failure. */ - public function unzip($file, $path) + function unzipper($file, $target_dir) { - $path = $this->cleanPath($path); - - $zipArchive = new ZipArchive(); - if ($zipArchive->open($file) !== true) { - return false; - } - - /** @var FilesystemAdapter $disk */ - $disk = $this->getDisk(); - - for ($i = 0; $i < $zipArchive->numFiles; ++$i) { - $zipEntryName = $zipArchive->getNameIndex($i); - $destination = $path . DIRECTORY_SEPARATOR . $this->cleanPath($zipEntryName); - if ($this->isDirectory($zipEntryName)) { - $disk->createDir($destination); - continue; + $target_dir = $this->cleanPath($target_dir); + $unzipper = resolve(\ZipArchive::class); + if ($unzipper->open($file)) { + /** @var FilesystemAdapter $disk */ + $disk = $this->getDisk(); + for ($i = 0; $i < $unzipper->numFiles; ++$i) { + $zipEntryName = $unzipper->getNameIndex($i); + $destination = $this->join($target_dir, $this->cleanPath($zipEntryName)); + if ($this->isDirectory($zipEntryName)) { + $disk->createDir($destination); + continue; + } + $disk->putStream($destination, $unzipper->getStream($zipEntryName)); } - $disk->putStream($destination, $zipArchive->getStream($zipEntryName)); + return true; } - return true; + return false; } - public function download(Scorm $scorm) + /** + * @param string $file SCORM archive uri on storage. + * @param callable $fn function run user stuff before unlink + */ + public function readScormArchive($file, callable $fn) { - return response()->stream(function () use ($scorm) { - // enable output of HTTP headers - // $options = new ZipStream\Option\Archive(); - // $options->setSendHttpHeaders(true); - $zip = new ZipStream($scorm->title . ".zip"); - /** @var FilesystemAdapter $disk */ - $disk = $this->getDisk(); - $zip->addFileFromStream($scorm->title, $disk->readStream($scorm->uuid)); - $zip->finish(); - }); + if (Storage::exists($file)) { + Storage::delete($file); + } + Storage::writeStream($file, $this->getArchiveDisk()->readStream($file)); + $path = Storage::path($file); + call_user_func($fn, $path); + unlink($path); // delete temp package + Storage::deleteDirectory(dirname($file)); // delete temp dir } /** @@ -67,6 +64,16 @@ class ScormDisk return $this->getDisk()->deleteDirectory($folderHashedName); } + /** + * + * @param array $paths + * @return string joined path + */ + private function join(...$paths) + { + return implode(DIRECTORY_SEPARATOR, $paths); + } + private function isDirectory($zipEntryName) { return substr($zipEntryName, -1) === '/'; @@ -87,4 +94,15 @@ class ScormDisk } return Storage::disk(config('scorm.disk')); } + + /** + * @return FilesystemAdapter $disk + */ + private function getArchiveDisk() + { + if (!config()->has('filesystems.disks.' . config('scorm.archive'))) { + throw new StorageNotFoundException('scorm_archive_disk_not_define'); + } + return Storage::disk(config('scorm.archive')); + } } diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index aedfae3..9de1439 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -17,7 +17,6 @@ use Peopleaps\Scorm\Model\ScormScoModel; use Peopleaps\Scorm\Model\ScormScoTrackingModel; use Illuminate\Support\Str; use Peopleaps\Scorm\Entity\Sco; -use ZipArchive; class ScormManager { @@ -38,28 +37,53 @@ class ScormManager $this->scormDisk = new ScormDisk(); } + public function uploadScormFromUri($file) + { + $scorm = null; + $this->scormDisk->readScormArchive($file, function ($path) use (&$scorm, $file) { + $this->validatePackage($path); + $uuid = dirname($file); + $filename = basename($file); + $scorm = $this->saveScorm($path, $uuid, $filename); + }); + return $scorm; + } + public function uploadScormArchive(UploadedFile $file) { - // Checks if it is a valid scorm archive - $scormData = null; - $zip = new ZipArchive(); - $openValue = $zip->open($file); + $this->validatePackage($file); + return $this->saveScorm($file, Str::uuid(), $file->getClientOriginalName()); + } + /** + * Checks if it is a valid scorm archive + * + * @param string|UploadedFile $file zip. + */ + private function validatePackage($file) + { + $zip = new \ZipArchive(); + $openValue = $zip->open($file); $isScormArchive = (true === $openValue) && $zip->getStream('imsmanifest.xml'); $zip->close(); - if (!$isScormArchive) { throw new InvalidScormArchiveException('invalid_scorm_archive_message'); - } else { - $scormData = $this->generateScorm($file); } + } + /** + * Save scom data + * + * @param string|UploadedFile $file zip. + */ + private function saveScorm($file, $uuid, $filename) + { + $scormData = $this->generateScorm($file, $uuid); // save to db if (is_null($scormData) || !is_array($scormData)) { throw new InvalidScormArchiveException('invalid_scorm_data'); } - /** * ScormModel::whereOriginFile Query Builder style equals ScormModel::where('origin_file',$value) * @@ -70,18 +94,7 @@ class ScormManager * Examples: * * $admin = DB::table('users')->whereId(1)->first(); - * - * $john = DB::table('users') - * ->whereIdAndEmail(2, 'john@doe.com') - * ->first(); - * - * $jane = DB::table('users') - * ->whereNameOrAge('Jane', 22) - * ->first(); - * - * * From laravel framework https://github.com/laravel/framework/blob/9.x/src/Illuminate/Database/Query/Builder.php' - * * Handle dynamic method calls into the method. * return $this->dynamicWhere($method, $parameters); **/ @@ -93,12 +106,12 @@ class ScormManager $scorm = $scorm->first(); $this->deleteScormData($scorm); } - $scorm->uuid = $scormData['uuid']; $scorm->title = $scormData['title']; $scorm->version = $scormData['version']; $scorm->entry_url = $scormData['entryUrl']; - $scorm->origin_file = $scormData['identifier']; + $scorm->identifier = $scormData['identifier']; + $scorm->origin_file = $filename; $scorm->save(); if (!empty($scormData['scos']) && is_array($scormData['scos'])) { @@ -145,7 +158,10 @@ class ScormManager return $sco; } - private function parseScormArchive(UploadedFile $file) + /** + * @param string|UploadedFile $file zip. + */ + private function parseScormArchive($file) { $data = []; $contents = ''; @@ -238,20 +254,19 @@ class ScormManager } /** - * @param UploadedFile $file + * @param string|UploadedFile $file zip. * @return array * @throws InvalidScormArchiveException */ - private function generateScorm(UploadedFile $file) + private function generateScorm($file, $uuid) { - $uuid = Str::uuid(); $scormData = $this->parseScormArchive($file); /** * Unzip a given ZIP file into the web resources directory. * * @param string $hashName name of the destination directory */ - $this->scormDisk->unzip($file, $uuid); + $this->scormDisk->unzipper($file, $uuid); return [ 'identifier' => $scormData['identifier'], @@ -667,9 +682,4 @@ class ScormManager return $formattedValue; } - - - public function download(Scorm $scorm){ - return $this->scormDisk->download($scorm); - } } -- cgit v1.2.3 From b518a5bbc9e18f6a2ac1827e24e733ec508ff0d5 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Wed, 23 Feb 2022 16:29:05 +0200 Subject: fix where origin file reference --- src/Manager/ScormManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index 9de1439..0ff7ed8 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -98,7 +98,7 @@ class ScormManager * Handle dynamic method calls into the method. * return $this->dynamicWhere($method, $parameters); **/ - $scorm = ScormModel::whereOriginFile($scormData['identifier']); + $scorm = ScormModel::whereOriginFile($filename); // Check if scom package already exists to drop old one. if (!$scorm->exists()) { $scorm = new ScormModel(); -- cgit v1.2.3 From 853e7ded74f589164a2ebce5fd6a0502639d54a1 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Wed, 23 Feb 2022 17:30:18 +0200 Subject: handle scorm archive ot found, update config --- src/Manager/ScormDisk.php | 19 ++++++++++++------- src/Model/ScormModel.php | 11 ++++++++++- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index cb09db5..5f15d38 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -2,6 +2,7 @@ namespace Peopleaps\Scorm\Manager; +use Exception; use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; @@ -45,14 +46,18 @@ class ScormDisk */ public function readScormArchive($file, callable $fn) { - if (Storage::exists($file)) { - Storage::delete($file); + try { + if (Storage::exists($file)) { + Storage::delete($file); + } + Storage::writeStream($file, $this->getArchiveDisk()->readStream($file)); + $path = Storage::path($file); + call_user_func($fn, $path); + unlink($path); // delete temp package + Storage::deleteDirectory(dirname($file)); // delete temp dir + } catch (Exception $ex) { + throw new StorageNotFoundException('scorm_archive_not_found'); } - Storage::writeStream($file, $this->getArchiveDisk()->readStream($file)); - $path = Storage::path($file); - call_user_func($fn, $path); - unlink($path); // delete temp package - Storage::deleteDirectory(dirname($file)); // delete temp dir } /** diff --git a/src/Model/ScormModel.php b/src/Model/ScormModel.php index 2777038..61ccb16 100644 --- a/src/Model/ScormModel.php +++ b/src/Model/ScormModel.php @@ -20,7 +20,16 @@ class ScormModel extends Model return config('scorm.table_names.scorm_table', parent::getTable()); } - public function scos() { + public function scos() + { return $this->hasMany(ScormScoModel::class, 'scorm_id', 'id'); } + + /** + * @return HasOne + */ + public function resource() + { + return $this->hasOne(config('scorm.table_names.resource_table')); + } } -- cgit v1.2.3 From c6c79fa525f03adaf0673e3571323b9635743035 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Sun, 27 Feb 2022 09:31:37 +0200 Subject: clean scorm model code --- src/Model/ScormModel.php | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src') diff --git a/src/Model/ScormModel.php b/src/Model/ScormModel.php index 61ccb16..4421d98 100644 --- a/src/Model/ScormModel.php +++ b/src/Model/ScormModel.php @@ -24,12 +24,4 @@ class ScormModel extends Model { return $this->hasMany(ScormScoModel::class, 'scorm_id', 'id'); } - - /** - * @return HasOne - */ - public function resource() - { - return $this->hasOne(config('scorm.table_names.resource_table')); - } } -- cgit v1.2.3 From f3da307a245c7d0ed59592c06ca0fbd7806e0f51 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Sun, 27 Feb 2022 21:41:47 +0200 Subject: update runtime progress data. --- src/Manager/ScormManager.php | 21 +++++++++++++++++---- src/Model/ScormScoTrackingModel.php | 7 ++++++- 2 files changed, 23 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index 0ff7ed8..61a0628 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -300,9 +300,8 @@ class ScormManager */ public function getScoByUuid($scoUuid) { - $sco = ScormScoModel::with([ - 'scorm' - ])->where('uuid', $scoUuid) + $sco = ScormScoModel::with(['scorm']) + ->where('uuid', $scoUuid) ->firstOrFail(); return $sco; @@ -313,7 +312,7 @@ class ScormManager return ScormScoTrackingModel::where('sco_id', $scoId)->where('user_id', $userId)->first(); } - public function createScoTracking($scoUuid, $userId = null) + public function createScoTracking($scoUuid, $userId = null, $userName = null) { $sco = ScormScoModel::where('uuid', $scoUuid)->firstOrFail(); @@ -321,6 +320,7 @@ class ScormManager $scoTracking = new ScoTracking(); $scoTracking->setSco($sco->toArray()); + $cmi = null; switch ($version) { case Scorm::SCORM_12: $scoTracking->setLessonStatus('not attempted'); @@ -338,16 +338,29 @@ class ScormManager } else { $scoTracking->setIsLocked(true); } + $cmi = [ + 'cmi.core.entry' => $scoTracking->getEntry(), + 'cmi.core.student_id' => $userId, + 'cmi.core.student_name' => $userName, + ]; + break; case Scorm::SCORM_2004: $scoTracking->setTotalTimeString('PT0S'); $scoTracking->setCompletionStatus('unknown'); $scoTracking->setLessonStatus('unknown'); $scoTracking->setIsLocked(false); + $cmi = [ + 'cmi.entry' => 'ab-initio', + 'cmi.learner_id' => $userId, + 'cmi.learner_name' => $userName, + 'cmi.scaled_passing_score' => 0.5, + ]; break; } $scoTracking->setUserId($userId); + $scoTracking->setDetails($cmi); // Create a new tracking model $storeTracking = ScormScoTrackingModel::firstOrCreate([ diff --git a/src/Model/ScormScoTrackingModel.php b/src/Model/ScormScoTrackingModel.php index 2c057c7..407e2da 100644 --- a/src/Model/ScormScoTrackingModel.php +++ b/src/Model/ScormScoTrackingModel.php @@ -35,12 +35,17 @@ class ScormScoTrackingModel extends Model 'updated_at' ]; + protected $casts = [ + 'details' => 'array', + ]; + public function getTable() { return config('scorm.table_names.scorm_sco_tracking_table', parent::getTable()); } - public function sco() { + public function sco() + { return $this->belongsTo(ScormScoModel::class, 'sco_id', 'id'); } } -- cgit v1.2.3 From 04fc94e0157136e6ce04c3d20edbf2845c4e0ebe Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Tue, 1 Mar 2022 11:02:49 +0200 Subject: refactor onerror clean resources and throw exception --- src/Manager/ScormDisk.php | 42 +++++++++++++++++++++++++++++++++++---- src/Manager/ScormManager.php | 47 +++++++++++++++++++++++++++----------------- 2 files changed, 67 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index 5f15d38..34e4cb6 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -53,20 +53,54 @@ class ScormDisk Storage::writeStream($file, $this->getArchiveDisk()->readStream($file)); $path = Storage::path($file); call_user_func($fn, $path); - unlink($path); // delete temp package - Storage::deleteDirectory(dirname($file)); // delete temp dir + // Clean local resources + $this->clean($file); } catch (Exception $ex) { throw new StorageNotFoundException('scorm_archive_not_found'); } } + private function clean($file) + { + try { + Storage::delete($file); + Storage::deleteDirectory(dirname($file)); // delete temp dir + } catch (Exception $ex) { + } + } + + /** + * @param string $directory + * @return bool + */ + public function deleteScorm($uuid) + { + $this->deleteScormArchive($uuid); // try to delete archive if exists. + return $this->deleteScormContent($uuid); + } + + /** + * @param string $directory + * @return bool + */ + private function deleteScormContent($folderHashedName) + { + try { + return $this->getDisk()->deleteDirectory($folderHashedName); + } catch (Exception $ex) { + } + } + /** * @param string $directory * @return bool */ - public function deleteScormFolder($folderHashedName) + private function deleteScormArchive($uuid) { - return $this->getDisk()->deleteDirectory($folderHashedName); + try { + return $this->getArchiveDisk()->deleteDirectory($uuid); + } catch (Exception $ex) { + } } /** diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index 61a0628..243036c 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -24,6 +24,8 @@ class ScormManager private $scormLib; /** @var ScormDisk */ private $scormDisk; + /** @var string $uuid */ + private $uuid; /** * Constructor. @@ -41,18 +43,17 @@ class ScormManager { $scorm = null; $this->scormDisk->readScormArchive($file, function ($path) use (&$scorm, $file) { - $this->validatePackage($path); - $uuid = dirname($file); + $this->uuid = dirname($file); $filename = basename($file); - $scorm = $this->saveScorm($path, $uuid, $filename); + $scorm = $this->saveScorm($path, $filename); }); return $scorm; } public function uploadScormArchive(UploadedFile $file) { - $this->validatePackage($file); - return $this->saveScorm($file, Str::uuid(), $file->getClientOriginalName()); + $this->uuid = Str::uuid(); + return $this->saveScorm($file, $file->getClientOriginalName()); } /** @@ -68,7 +69,7 @@ class ScormManager $zip->close(); if (!$isScormArchive) { - throw new InvalidScormArchiveException('invalid_scorm_archive_message'); + $this->onError('invalid_scorm_archive_message'); } } @@ -77,12 +78,13 @@ class ScormManager * * @param string|UploadedFile $file zip. */ - private function saveScorm($file, $uuid, $filename) + private function saveScorm($file, $filename) { - $scormData = $this->generateScorm($file, $uuid); + $this->validatePackage($file); + $scormData = $this->generateScorm($file); // save to db if (is_null($scormData) || !is_array($scormData)) { - throw new InvalidScormArchiveException('invalid_scorm_data'); + $this->onError('invalid_scorm_data'); } /** * ScormModel::whereOriginFile Query Builder style equals ScormModel::where('origin_file',$value) @@ -178,14 +180,14 @@ class ScormManager $dom = new DOMDocument(); if (!$dom->loadXML($contents)) { - throw new InvalidScormArchiveException('cannot_load_imsmanifest_message'); + $this->onError('cannot_load_imsmanifest_message'); } $manifest = $dom->getElementsByTagName('manifest')->item(0); if (!is_null($manifest->attributes->getNamedItem('identifier'))) { $data['identifier'] = $manifest->attributes->getNamedItem('identifier')->nodeValue; } else { - throw new InvalidScormArchiveException('invalid_scorm_manifest_identifier'); + $this->onError('invalid_scorm_manifest_identifier'); } $titles = $dom->getElementsByTagName('title'); if ($titles->length > 0) { @@ -204,15 +206,15 @@ class ScormManager $data['version'] = Scorm::SCORM_2004; break; default: - throw new InvalidScormArchiveException('invalid_scorm_version_message'); + $this->onError('invalid_scorm_version_message'); } } else { - throw new InvalidScormArchiveException('invalid_scorm_version_message'); + $this->onError('invalid_scorm_version_message'); } $scos = $this->scormLib->parseOrganizationsNode($dom); if (0 >= count($scos)) { - throw new InvalidScormArchiveException('no_sco_in_scorm_archive_message'); + $this->onError('no_sco_in_scorm_archive_message'); } $data['entryUrl'] = $scos[0]->entryUrl ?? $scos[0]->scoChildren[0]->entryUrl; @@ -250,7 +252,7 @@ class ScormManager */ protected function deleteScormFolder($folderHashedName) { - return $this->scormDisk->deleteScormFolder($folderHashedName); + return $this->scormDisk->deleteScorm($folderHashedName); } /** @@ -258,7 +260,7 @@ class ScormManager * @return array * @throws InvalidScormArchiveException */ - private function generateScorm($file, $uuid) + private function generateScorm($file) { $scormData = $this->parseScormArchive($file); /** @@ -266,11 +268,11 @@ class ScormManager * * @param string $hashName name of the destination directory */ - $this->scormDisk->unzipper($file, $uuid); + $this->scormDisk->unzipper($file, $this->uuid); return [ 'identifier' => $scormData['identifier'], - 'uuid' => $uuid, + 'uuid' => $this->uuid, 'title' => $scormData['title'], // to follow standard file data format 'version' => $scormData['version'], 'entryUrl' => $scormData['entryUrl'], @@ -695,4 +697,13 @@ class ScormManager return $formattedValue; } + + /** + * Clean resources and throw exception. + */ + private function onError($msg) + { + $this->scormDisk->deleteScorm($this->uuid); + throw new InvalidScormArchiveException($msg); + } } -- cgit v1.2.3 From 99e679a54328e5bfa00a8a481cacb124f261bd9e Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Mon, 22 Aug 2022 14:23:36 +0200 Subject: update scorm disk with log try catch exception message --- src/Manager/ScormDisk.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index 34e4cb6..ba0ade8 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -5,6 +5,7 @@ namespace Peopleaps\Scorm\Manager; use Exception; use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Peopleaps\Scorm\Exception\StorageNotFoundException; @@ -56,6 +57,7 @@ class ScormDisk // Clean local resources $this->clean($file); } catch (Exception $ex) { + Log::error($ex->getMessage()); throw new StorageNotFoundException('scorm_archive_not_found'); } } @@ -66,6 +68,7 @@ class ScormDisk Storage::delete($file); Storage::deleteDirectory(dirname($file)); // delete temp dir } catch (Exception $ex) { + Log::error($ex->getMessage()); } } @@ -88,6 +91,7 @@ class ScormDisk try { return $this->getDisk()->deleteDirectory($folderHashedName); } catch (Exception $ex) { + Log::error($ex->getMessage()); } } @@ -100,6 +104,7 @@ class ScormDisk try { return $this->getArchiveDisk()->deleteDirectory($uuid); } catch (Exception $ex) { + Log::error($ex->getMessage()); } } -- cgit v1.2.3