blob: 5b71904dc6da6f76ca288d9a2fed7ea920d6c2af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
<?php
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;
class ScormDisk
{
/**
* Extract zip file into destination directory.
*
* @param UploadedFile|string $file zip source
* @param string $path The path to the destination.
*
* @return bool true on success, false on failure.
*/
function unzipper($file, $target_dir)
{
$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->createDirectory($destination);
continue;
}
$disk->writeStream($destination, $unzipper->getStream($zipEntryName));
}
return true;
}
return false;
}
/**
* @param string $file SCORM archive uri on storage.
* @param callable $fn function run user stuff before unlink
*/
public function readScormArchive($file, callable $fn)
{
try {
$archiveDisk = $this->getArchiveDisk();
// Log the file path being processed for debugging
Log::info('Processing SCORM archive file: ' . $file);
// Check if file exists on archive disk
if (!$archiveDisk->exists($file)) {
Log::error('File not found on archive disk: ' . $file);
throw new StorageNotFoundException('scorm_archive_not_found_on_archive_disk: ' . $file);
}
// Get the stream from archive disk
$stream = $archiveDisk->readStream($file);
if (!is_resource($stream)) {
Log::error('Failed to read stream from archive disk for file: ' . $file . '. Stream type: ' . gettype($stream));
throw new StorageNotFoundException('failed_to_read_scorm_archive_stream: ' . $file);
}
if (Storage::exists($file)) {
Storage::delete($file);
}
Storage::writeStream($file, $stream);
$path = Storage::path($file);
call_user_func($fn, $path);
// Clean local resources
$this->clean($file);
} catch (Exception $ex) {
Log::error('Error in readScormArchive: ' . $ex->getMessage() . ' for file: ' . $file);
throw new StorageNotFoundException('scorm_archive_not_found');
}
}
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);
}
/**
* @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
*/
private function deleteScormArchive($uuid)
{
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)
{
return substr($zipEntryName, -1) === '/';
}
private function cleanPath($path)
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
/**
* @return FilesystemAdapter $disk
*/
private function getDisk()
{
$diskName = config('scorm.disk');
if (empty($diskName)) {
throw new StorageNotFoundException('scorm_disk_not_configured');
}
if (!config()->has('filesystems.disks.' . $diskName)) {
throw new StorageNotFoundException('scorm_disk_not_define: ' . $diskName);
}
$disk = Storage::disk($diskName);
return $disk;
}
/**
* @return FilesystemAdapter $disk
*/
private function getArchiveDisk()
{
$archiveDiskName = config('scorm.archive');
if (empty($archiveDiskName)) {
throw new StorageNotFoundException('scorm_archive_disk_not_configured');
}
if (!config()->has('filesystems.disks.' . $archiveDiskName)) {
throw new StorageNotFoundException('scorm_archive_disk_not_define: ' . $archiveDiskName);
}
$disk = Storage::disk($archiveDiskName);
return $disk;
}
}
|