Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 9cfcc17

Browse files
committed
v2.4.0
1 parent 1a4f217 commit 9cfcc17

File tree

20 files changed

+426
-303
lines changed

20 files changed

+426
-303
lines changed

logs/v2.3.8.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

logs/v2.4.0.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## New
2+
3+
- added a new config key “controller” https://github.com/ctf0/Laravel-Media-Manager/wiki/Have-More-Control-Uploads.
4+
- we now cache the server response on client side “probably the only manager that does that”, so navigation will be much faster specially for slow connections.
5+
- for zip streaming, instead of bailing out when a file cant be read, we’ll download all as normal and show warning for the bad ones.
6+
7+
8+
## Edit
9+
10+
- remove the crazy modal z-index
11+
- changed the local storage key name
12+
- vendor folders is now renamed to `packages` to avoid issues with installation
13+
- some optimization & cleanup
14+
15+
- update wiki

src/Controllers/MediaController.php

Lines changed: 50 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace ctf0\MediaManager\Controllers;
44

55
use Exception;
6-
use ZipStream\ZipStream;
76
use Illuminate\Http\Request;
87
use App\Http\Controllers\Controller;
98

@@ -75,9 +74,10 @@ public function get_files(Request $request)
7574

7675
return response()->json([
7776
'locked' => $this->lockedList,
77+
'dirs' => $this->dirsList($request->dirs),
7878
'files' => [
79-
'path' => $folder,
80-
'items' => $this->getData($folder),
79+
'path' => $folder,
80+
'items' => $this->getData($folder),
8181
],
8282
]);
8383
}
@@ -97,9 +97,16 @@ public function get_dirs(Request $request)
9797
$folderLocation = rtrim(implode('/', $folderLocation), '/');
9898
}
9999

100-
return response()->json(
101-
str_replace($folderLocation, '', $this->storageDisk->allDirectories($folderLocation))
102-
);
100+
return response()->json($this->dirsList($folderLocation));
101+
}
102+
103+
protected function dirsList($location)
104+
{
105+
if (is_array($location)) {
106+
$location = rtrim(implode('/', $location), '/');
107+
}
108+
109+
return str_replace($location, '', $this->storageDisk->allDirectories($location));
103110
}
104111

105112
/**
@@ -117,12 +124,11 @@ public function upload(Request $request)
117124
$result = [];
118125

119126
foreach ($files as $one) {
120-
$file_type = $one->getMimeType();
121-
122127
$original = $one->getClientOriginalName();
123-
$get_name = pathinfo($original, PATHINFO_FILENAME);
124-
$file_ext = pathinfo($original, PATHINFO_EXTENSION);
125-
$file_name = $random_name ? uniqid() . ".$file_ext" : $this->cleanName($get_name, null) . ".$file_ext";
128+
$name_only = pathinfo($original, PATHINFO_FILENAME);
129+
$ext_only = pathinfo($original, PATHINFO_EXTENSION);
130+
$file_name = $random_name ? uniqid() . ".$ext_only" : $this->cleanName($name_only, null) . ".$ext_only";
131+
$file_type = $one->getMimeType();
126132
$destination = "$upload_path/$file_name";
127133

128134
try {
@@ -137,7 +143,7 @@ public function upload(Request $request)
137143
}
138144

139145
// save file
140-
$saved_name = $one->storeAs($upload_path, $file_name, $this->fileSystem);
146+
$saved_name = $this->storeFile($one, $upload_path, $file_name);
141147

142148
// fire event
143149
event('MMFileUploaded', $this->getFilePath($saved_name));
@@ -426,7 +432,11 @@ public function lock_file(Request $request)
426432
}
427433

428434
/**
429-
* zip ops.
435+
* zip folder.
436+
*
437+
* @param Request $request [description]
438+
*
439+
* @return [type] [description]
430440
*/
431441
public function folder_download(Request $request)
432442
{
@@ -437,6 +447,13 @@ public function folder_download(Request $request)
437447
);
438448
}
439449

450+
/**
451+
* zip files.
452+
*
453+
* @param Request $request [description]
454+
*
455+
* @return [type] [description]
456+
*/
440457
public function files_download(Request $request)
441458
{
442459
return $this->download(
@@ -446,42 +463,6 @@ public function files_download(Request $request)
446463
);
447464
}
448465

449-
protected function download($name, $list, $type)
450-
{
451-
// track changes
452-
$counter = 100 / count($list);
453-
$store = $this->cacheStore;
454-
$cache_name = $name;
455-
$store->forever("$cache_name.progress", 0);
456-
457-
return response()->stream(function () use ($name, $list, $type, $counter, $store, $cache_name) {
458-
$zip = new ZipStream("$name.zip", [
459-
'content_type' => 'application/octet-stream',
460-
]);
461-
462-
foreach ($list as $file) {
463-
if ('folder' == $type) {
464-
$file_name = pathinfo($file, PATHINFO_BASENAME);
465-
$streamRead = $this->storageDisk->readStream($file);
466-
} else {
467-
$file_name = $file['name'];
468-
$streamRead = @fopen($file['path'], 'r');
469-
}
470-
471-
if ($streamRead) {
472-
$store->increment("$cache_name.progress", round($counter, 2));
473-
$zip->addFileFromStream($file_name, $streamRead);
474-
} else {
475-
$store->forever("$cache_name.abort", $file_name);
476-
die();
477-
}
478-
}
479-
480-
$store->forever("$cache_name.done", true);
481-
$zip->finish();
482-
});
483-
}
484-
485466
/**
486467
* zip progress update.
487468
*/
@@ -498,34 +479,34 @@ public function zip_progress(Request $request)
498479
$name = $request->name;
499480

500481
// get changes
501-
$store = $this->cacheStore;
502-
$cache_name = $name;
482+
$store = $this->cacheStore;
503483

504-
return response()->stream(function () use ($start, $maxExecution, $close, $sleep, $store, $cache_name) {
484+
return response()->stream(function () use ($start, $maxExecution, $close, $sleep, $store, $name) {
505485
while (!$close) {
506486
// progress
507-
$this->es_msg($store->get("$cache_name.progress"), 'progress');
508-
509-
// avoid server crash
510-
if (time() >= $start + $maxExecution) {
511-
$close = true;
512-
$this->es_msg(null, 'exit');
513-
$this->clearZipCache($store, $cache_name);
487+
$this->SSE_msg($store->get("$name.progress"), 'progress');
488+
489+
// warn
490+
if ($store->has("$name.warn")) {
491+
$this->SSE_msg(
492+
trans('MediaManager::messages.stream_error', ['attr' => $store->pull("$name.warn")]),
493+
'warn'
494+
);
514495
}
515496

516-
// abort
517-
if ($store->has("$cache_name.abort")) {
497+
// done
498+
if ($store->has("$name.done")) {
518499
$close = true;
519-
$this->es_msg('Could not open "' . $store->get("$cache_name.abort") . '" stream for reading.', 'abort');
520-
$this->clearZipCache($store, $cache_name);
500+
$this->SSE_msg(100, 'progress');
501+
$this->SSE_msg('All Done', 'done');
502+
$this->clearZipCache($store, $name);
521503
}
522504

523-
// done
524-
if ($store->has("$cache_name.done")) {
505+
// exit
506+
if (time() >= $start + $maxExecution) {
525507
$close = true;
526-
$this->es_msg(100, 'progress');
527-
$this->es_msg('All Done', 'done');
528-
$this->clearZipCache($store, $cache_name);
508+
$this->SSE_msg(null, 'exit');
509+
$this->clearZipCache($store, $name);
529510
}
530511

531512
ob_flush();
@@ -538,24 +519,11 @@ public function zip_progress(Request $request)
538519
}
539520
}, 200, [
540521
'Content-Type' => 'text/event-stream', // needed for SSE to work
541-
'Cache-Control' => 'no-cache', // make sure we dont cache this response
522+
'Cache-Control' => 'no-cache', // dont cache this response
542523
'X-Accel-Buffering' => 'no', // needed for while loop to work
543524
'Access-Control-Allow-Origin' => config('app.url'), // for cors
544525
'Access-Control-Expose-Headers' => '*', // for cors
545526
'Access-Control-Allow-Credentials' => true, // for cors
546527
]);
547528
}
548-
549-
protected function es_msg($data = null, $event = null)
550-
{
551-
echo $event ? "event: $event\n" : ':';
552-
echo $data ? 'data: ' . json_encode(['response' => $data]) . "\n\n" : ':';
553-
}
554-
555-
protected function clearZipCache($store, $item)
556-
{
557-
$store->forget("$item.progress");
558-
$store->forget("$item.done");
559-
$store->forget("$item.abort");
560-
}
561529
}

src/Controllers/OpsTrait.php

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
namespace ctf0\MediaManager\Controllers;
44

55
use Carbon\Carbon;
6+
use ZipStream\ZipStream;
67

78
trait OpsTrait
89
{
910
/**
10-
* [getFiles description].
11+
* get files list.
1112
*
12-
* @param [type] $dir [description]
13-
*
14-
* @return [type] [description]
13+
* @param mixed $dir
1514
*/
1615
protected function getData($dir)
1716
{
@@ -52,6 +51,20 @@ protected function getData($dir)
5251
return $files;
5352
}
5453

54+
/**
55+
* save file to disk.
56+
*
57+
* @param [type] $item [description]
58+
* @param [type] $upload_path [description]
59+
* @param [type] $file_name [description]
60+
*
61+
* @return [type] [description]
62+
*/
63+
protected function storeFile($item, $upload_path, $file_name)
64+
{
65+
return $item->storeAs($upload_path, $file_name, $this->fileSystem);
66+
}
67+
5568
/**
5669
* sanitize input.
5770
*
@@ -81,16 +94,10 @@ protected function filePattern($item)
8194
/**
8295
* helpers for folder ops.
8396
*
84-
* @param [type] $folder [description]
85-
*
86-
* @return [type] [description]
97+
* @param mixed $folder
8798
*/
8899
protected function folderCount($folder)
89100
{
90-
// files + directories count
91-
// return count($this->folderFiles($folder)) + count($this->storageDisk->allDirectories($folder));
92-
93-
// files only
94101
return count($this->folderFiles($folder));
95102
}
96103

@@ -132,4 +139,58 @@ protected function getFilePath($name)
132139

133140
return $root . $dir;
134141
}
142+
143+
/**
144+
* zip ops.
145+
*
146+
* @param mixed $name
147+
* @param mixed $list
148+
* @param mixed $type
149+
*/
150+
protected function download($name, $list, $type)
151+
{
152+
// track changes
153+
$counter = 100 / count($list);
154+
$store = $this->cacheStore;
155+
$store->forever("$name.progress", 0);
156+
157+
return response()->stream(function () use ($name, $list, $type, $counter, $store) {
158+
$zip = new ZipStream("$name.zip", [
159+
'content_type' => 'application/octet-stream',
160+
]);
161+
162+
foreach ($list as $file) {
163+
if ('folder' == $type) {
164+
$file_name = pathinfo($file, PATHINFO_BASENAME);
165+
$streamRead = $this->storageDisk->readStream($file);
166+
} else {
167+
$file_name = $file['name'];
168+
$streamRead = @fopen($file['path'], 'r');
169+
}
170+
171+
if ($streamRead) {
172+
$store->increment("$name.progress", round($counter, 2));
173+
$zip->addFileFromStream($file_name, $streamRead);
174+
} else {
175+
$store->forever("$name.warn", $file_name);
176+
}
177+
}
178+
179+
$store->forever("$name.done", true);
180+
$zip->finish();
181+
});
182+
}
183+
184+
protected function SSE_msg($data = null, $event = null)
185+
{
186+
echo $event ? "event: $event\n" : ':';
187+
echo $data ? 'data: ' . json_encode(['response' => $data]) . "\n\n" : ':';
188+
}
189+
190+
protected function clearZipCache($store, $item)
191+
{
192+
$store->forget("$item.progress");
193+
$store->forget("$item.done");
194+
$store->forget("$item.abort");
195+
}
135196
}

0 commit comments

Comments
 (0)