Skip to content

Commit 587072a

Browse files
authored
Merge pull request #21 from KhaledLela/main
Enhances from my working version.
2 parents cdad6ee + 1764d93 commit 587072a

File tree

7 files changed

+231
-69
lines changed

7 files changed

+231
-69
lines changed

config/scorm.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
return [
44

55
'table_names' => [
6-
'user_table' => 'users', // user table name on main LMS app.
7-
'scorm_table' => 'scorm',
8-
'scorm_sco_table' => 'scorm_sco',
9-
'scorm_sco_tracking_table' => 'scorm_sco_tracking',
6+
'user_table' => 'users', // user table name on main LMS app.
7+
'resource_table' => 'resource', // resource table on LMS app.
8+
'scorm_table' => 'scorm',
9+
'scorm_sco_table' => 'scorm_sco',
10+
'scorm_sco_tracking_table' => 'scorm_sco_tracking',
1011
],
1112
/**
1213
* Scorm directory. You may create a custom path in file system
@@ -23,5 +24,6 @@
2324
* 'bucket' => env('AWS_SCORM_BUCKET'),
2425
* ],
2526
*/
26-
'disk' => 'local',
27+
'disk' => 'local',
28+
'archive' => 'local',
2729
];

database/migrations/create_scorm_tables.php.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class CreateScormTables extends Migration
2020
}
2121

2222
// scorm_model
23-
Schema::create($tableNames['scorm_table'], function (Blueprint $table) {
23+
Schema::create($tableNames['scorm_table'], function (Blueprint $table) use ($tableNames){
2424
$table->bigIncrements('id');
25-
$table->nullableMorphs('resource');
25+
$table->morphs($tableNames['resource_table']);
2626
$table->string('title');
2727
$table->string('origin_file')->nullable();
2828
$table->string('version');

src/Entity/Scorm.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Peopleaps\Scorm\Entity;
55

6+
use Peopleaps\Scorm\Model\ScormModel;
7+
68
class Scorm
79
{
810
const SCORM_12 = 'scorm_12';
@@ -17,6 +19,17 @@ class Scorm
1719
public $scos;
1820
public $scoSerializer;
1921

22+
public static function fromModel(ScormModel $model)
23+
{
24+
$instance = new self();
25+
$instance->setId($model->id);
26+
$instance->setUuid($model->uuid);
27+
$instance->setTitle($model->title);
28+
$instance->setVersion($model->version);
29+
$instance->setEntryUrl($model->entryUrl);
30+
return $instance;
31+
}
32+
2033
/**
2134
* @return string
2235
*/

src/Manager/ScormDisk.php

Lines changed: 99 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,119 @@
22

33
namespace Peopleaps\Scorm\Manager;
44

5+
use Exception;
56
use Illuminate\Filesystem\FilesystemAdapter;
7+
use Illuminate\Http\UploadedFile;
8+
use Illuminate\Support\Facades\Log;
69
use Illuminate\Support\Facades\Storage;
710
use Peopleaps\Scorm\Exception\StorageNotFoundException;
8-
use ZipArchive;
911

1012
class ScormDisk
1113
{
1214
/**
1315
* Extract zip file into destination directory.
1416
*
15-
* @param string $path Destination directory
16-
* @param string $zipFilePath The path to the zip file.
17+
* @param UploadedFile|string $file zip source
18+
* @param string $path The path to the destination.
1719
*
18-
* @return bool True on success, false on failure.
20+
* @return bool true on success, false on failure.
1921
*/
20-
public function unzip($file, $path)
22+
function unzipper($file, $target_dir)
2123
{
22-
$path = $this->cleanPath($path);
23-
24-
$zipArchive = new ZipArchive();
25-
if ($zipArchive->open($file) !== true) {
26-
return false;
24+
$target_dir = $this->cleanPath($target_dir);
25+
$unzipper = resolve(\ZipArchive::class);
26+
if ($unzipper->open($file)) {
27+
/** @var FilesystemAdapter $disk */
28+
$disk = $this->getDisk();
29+
for ($i = 0; $i < $unzipper->numFiles; ++$i) {
30+
$zipEntryName = $unzipper->getNameIndex($i);
31+
$destination = $this->join($target_dir, $this->cleanPath($zipEntryName));
32+
if ($this->isDirectory($zipEntryName)) {
33+
$disk->createDir($destination);
34+
continue;
35+
}
36+
$disk->putStream($destination, $unzipper->getStream($zipEntryName));
37+
}
38+
return true;
2739
}
40+
return false;
41+
}
2842

29-
/** @var FilesystemAdapter $disk */
30-
$disk = $this->getDisk();
31-
$createDir = 'createDir';
32-
$putStream = 'putStream';
33-
34-
if (!method_exists($disk, $createDir)) {
35-
$createDir = 'createDirectory';
36-
$putStream = 'writeStream';
43+
/**
44+
* @param string $file SCORM archive uri on storage.
45+
* @param callable $fn function run user stuff before unlink
46+
*/
47+
public function readScormArchive($file, callable $fn)
48+
{
49+
try {
50+
if (Storage::exists($file)) {
51+
Storage::delete($file);
52+
}
53+
Storage::writeStream($file, $this->getArchiveDisk()->readStream($file));
54+
$path = Storage::path($file);
55+
call_user_func($fn, $path);
56+
// Clean local resources
57+
$this->clean($file);
58+
} catch (Exception $ex) {
59+
Log::error($ex->getMessage());
60+
throw new StorageNotFoundException('scorm_archive_not_found');
3761
}
62+
}
3863

39-
for ($i = 0; $i < $zipArchive->numFiles; ++$i) {
40-
$zipEntryName = $zipArchive->getNameIndex($i);
41-
$destination = $path . DIRECTORY_SEPARATOR . $this->cleanPath($zipEntryName);
42-
if ($this->isDirectory($zipEntryName)) {
43-
$disk->$createDir($destination);
44-
continue;
45-
}
46-
$disk->$putStream($destination, $zipArchive->getStream($zipEntryName));
64+
private function clean($file)
65+
{
66+
try {
67+
Storage::delete($file);
68+
Storage::deleteDirectory(dirname($file)); // delete temp dir
69+
} catch (Exception $ex) {
70+
Log::error($ex->getMessage());
4771
}
72+
}
73+
74+
/**
75+
* @param string $directory
76+
* @return bool
77+
*/
78+
public function deleteScorm($uuid)
79+
{
80+
$this->deleteScormArchive($uuid); // try to delete archive if exists.
81+
return $this->deleteScormContent($uuid);
82+
}
4883

49-
return true;
84+
/**
85+
* @param string $directory
86+
* @return bool
87+
*/
88+
private function deleteScormContent($folderHashedName)
89+
{
90+
try {
91+
return $this->getDisk()->deleteDirectory($folderHashedName);
92+
} catch (Exception $ex) {
93+
Log::error($ex->getMessage());
94+
}
5095
}
5196

5297
/**
5398
* @param string $directory
5499
* @return bool
55100
*/
56-
public function deleteScormFolder($folderHashedName)
101+
private function deleteScormArchive($uuid)
57102
{
58-
return $this->getDisk()->deleteDirectory($folderHashedName);
103+
try {
104+
return $this->getArchiveDisk()->deleteDirectory($uuid);
105+
} catch (Exception $ex) {
106+
Log::error($ex->getMessage());
107+
}
108+
}
109+
110+
/**
111+
*
112+
* @param array $paths
113+
* @return string joined path
114+
*/
115+
private function join(...$paths)
116+
{
117+
return implode(DIRECTORY_SEPARATOR, $paths);
59118
}
60119

61120
private function isDirectory($zipEntryName)
@@ -78,4 +137,15 @@ private function getDisk()
78137
}
79138
return Storage::disk(config('scorm.disk'));
80139
}
140+
141+
/**
142+
* @return FilesystemAdapter $disk
143+
*/
144+
private function getArchiveDisk()
145+
{
146+
if (!config()->has('filesystems.disks.' . config('scorm.archive'))) {
147+
throw new StorageNotFoundException('scorm_archive_disk_not_define');
148+
}
149+
return Storage::disk(config('scorm.archive'));
150+
}
81151
}

0 commit comments

Comments
 (0)