summaryrefslogtreecommitdiff
path: root/src/Manager/ScormDisk.php
diff options
context:
space:
mode:
authordevianl2 <devianleong@gmail.com>2022-02-11 13:04:26 +0800
committerGitHub <noreply@github.com>2022-02-11 13:04:26 +0800
commit88d7940d4db9519c046f8c5375ef092da1daab35 (patch)
tree0a0d29e08313068c758a48f496b7b58abf0a462d /src/Manager/ScormDisk.php
parent3da455045032735b1cab91a575c43fa064bc022f (diff)
parent8a88eed9cab825d297f0d738efd5e0752b675f61 (diff)
Merge pull request #9 from devianl2/revert-8-revert-7-main3.0.13.0
Revert "Revert "Improve SCORM disk storage handler""
Diffstat (limited to 'src/Manager/ScormDisk.php')
-rw-r--r--src/Manager/ScormDisk.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php
new file mode 100644
index 0000000..249f027
--- /dev/null
+++ b/src/Manager/ScormDisk.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Peopleaps\Scorm\Manager;
+
+use Illuminate\Filesystem\FilesystemAdapter;
+use Illuminate\Support\Facades\Storage;
+use Peopleaps\Scorm\Exception\StorageNotFoundException;
+use ZipArchive;
+
+class ScormDisk
+{
+ /**
+ * Extract zip file into destination directory.
+ *
+ * @param string $path Destination directory
+ * @param string $zipFilePath The path to the zip file.
+ *
+ * @return bool True on success, false on failure.
+ */
+ public function unzip($file, $path)
+ {
+ $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;
+ }
+ $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('scorm_disk_not_define');
+ }
+ return Storage::disk(config('scorm.disk'));
+ }
+}