From 2e8cf8822305aaea60d12a47d124122ed5e4c145 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Fri, 4 Feb 2022 00:56:05 +0200 Subject: add scorm dist to manage scorm storage disk operation --- src/Manager/ScormDisk.php | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/Manager/ScormDisk.php (limited to 'src') diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php new file mode 100644 index 0000000..9a03f59 --- /dev/null +++ b/src/Manager/ScormDisk.php @@ -0,0 +1,74 @@ +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; + } + $disk->putStream($destination, $zipArchive->getStream($zipEntryName)); + } + + return true; + } + + /** + * @param string $directory + * @return bool + */ + public function deleteScormFolder($folderHashedName) + { + return $this->getDisk()->deleteDirectory($folderHashedName); + } + + private function isDirectory($zipEntryName) + { + return substr($zipEntryName, -1) === '/'; + } + + private function cleanPath($path) + { + return str_replace('/', DIRECTORY_SEPARATOR, $path); + } + + /** + * @return FilesystemAdapter $disk + */ + private function getDisk() + { + if (!config()->has('filesystems.disks.' . config('scorm.disk'))) { + throw new StorageNotFoundException(); + } + return Storage::disk(config('scorm.disk')); + } +} -- cgit v1.2.3