summaryrefslogtreecommitdiff
path: root/src/Manager/ScormDisk.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Manager/ScormDisk.php')
-rw-r--r--src/Manager/ScormDisk.php90
1 files changed, 54 insertions, 36 deletions
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'));
+ }
}