2
2
3
3
namespace Peopleaps \Scorm \Manager ;
4
4
5
+ use Exception ;
5
6
use Illuminate \Filesystem \FilesystemAdapter ;
7
+ use Illuminate \Http \UploadedFile ;
8
+ use Illuminate \Support \Facades \Log ;
6
9
use Illuminate \Support \Facades \Storage ;
7
10
use Peopleaps \Scorm \Exception \StorageNotFoundException ;
8
- use ZipArchive ;
9
11
10
12
class ScormDisk
11
13
{
12
14
/**
13
15
* Extract zip file into destination directory.
14
16
*
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 .
17
19
*
18
- * @return bool True on success, false on failure.
20
+ * @return bool true on success, false on failure.
19
21
*/
20
- public function unzip ($ file , $ path )
22
+ function unzipper ($ file , $ target_dir )
21
23
{
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 ;
27
39
}
40
+ return false ;
41
+ }
28
42
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 ' );
37
61
}
62
+ }
38
63
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 ());
47
71
}
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
+ }
48
83
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
+ }
50
95
}
51
96
52
97
/**
53
98
* @param string $directory
54
99
* @return bool
55
100
*/
56
- public function deleteScormFolder ( $ folderHashedName )
101
+ private function deleteScormArchive ( $ uuid )
57
102
{
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 );
59
118
}
60
119
61
120
private function isDirectory ($ zipEntryName )
@@ -78,4 +137,15 @@ private function getDisk()
78
137
}
79
138
return Storage::disk (config ('scorm.disk ' ));
80
139
}
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
+ }
81
151
}
0 commit comments