diff options
author | devianl2 <devianleong@gmail.com> | 2022-09-21 14:29:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-21 14:29:32 +0800 |
commit | 587072a458c2d76410d13d12754bb64ad12abb8d (patch) | |
tree | 1a832393b54434cf37c5223e4bfcabd050731eae /src/Manager/ScormDisk.php | |
parent | cdad6eec2d4845477b00353c3af97591f56b460a (diff) | |
parent | 1764d938b4c7136738577d452f201bc3078156e9 (diff) |
Merge pull request #21 from KhaledLela/main
Enhances from my working version.
Diffstat (limited to 'src/Manager/ScormDisk.php')
-rw-r--r-- | src/Manager/ScormDisk.php | 128 |
1 files changed, 99 insertions, 29 deletions
diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index ef82d1c..a280142 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -2,60 +2,119 @@ 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; -use ZipArchive; 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; + $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)); + } + return true; } + return false; + } - /** @var FilesystemAdapter $disk */ - $disk = $this->getDisk(); - $createDir = 'createDir'; - $putStream = 'putStream'; - - if (!method_exists($disk, $createDir)) { - $createDir = 'createDirectory'; - $putStream = 'writeStream'; + /** + * @param string $file SCORM archive uri on storage. + * @param callable $fn function run user stuff before unlink + */ + public function readScormArchive($file, callable $fn) + { + try { + if (Storage::exists($file)) { + Storage::delete($file); + } + Storage::writeStream($file, $this->getArchiveDisk()->readStream($file)); + $path = Storage::path($file); + call_user_func($fn, $path); + // Clean local resources + $this->clean($file); + } catch (Exception $ex) { + Log::error($ex->getMessage()); + throw new StorageNotFoundException('scorm_archive_not_found'); } + } - 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)); + private function clean($file) + { + try { + Storage::delete($file); + Storage::deleteDirectory(dirname($file)); // delete temp dir + } catch (Exception $ex) { + Log::error($ex->getMessage()); } + } + + /** + * @param string $directory + * @return bool + */ + public function deleteScorm($uuid) + { + $this->deleteScormArchive($uuid); // try to delete archive if exists. + return $this->deleteScormContent($uuid); + } - return true; + /** + * @param string $directory + * @return bool + */ + private function deleteScormContent($folderHashedName) + { + try { + return $this->getDisk()->deleteDirectory($folderHashedName); + } catch (Exception $ex) { + Log::error($ex->getMessage()); + } } /** * @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) { + Log::error($ex->getMessage()); + } + } + + /** + * + * @param array $paths + * @return string joined path + */ + private function join(...$paths) + { + return implode(DIRECTORY_SEPARATOR, $paths); } private function isDirectory($zipEntryName) @@ -78,4 +137,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')); + } } |