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

Commit 1a4f217

Browse files
committed
v2.3.8
1 parent 449bed5 commit 1a4f217

File tree

19 files changed

+449
-220
lines changed

19 files changed

+449
-220
lines changed

logs/v2.3.7.txt

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

logs/v2.3.8.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- fix “select + click” for search
2+
- add support for zip progress
3+
- new styling for path breadcrumb on mobile
4+
- add animation for breadcrumbs
5+
- fix local-storage toolbar changing on its own
6+
- some cleanup to the controller
7+
8+
- update assets
9+
- update db file
10+
- update language file

src/Controllers/MediaController.php

Lines changed: 132 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,39 @@ class MediaController extends Controller
1111
{
1212
use OpsTrait;
1313

14+
// main
1415
protected $fileSystem;
1516
protected $storageDisk;
1617
protected $ignoreFiles;
1718
protected $fileChars;
1819
protected $folderChars;
1920
protected $sanitizedText;
20-
protected $unallowed_mimes;
21+
protected $unallowedMimes;
2122
protected $LMF;
2223

23-
protected $locked_files_list;
24-
protected $disks;
24+
// extra
25+
protected $storageDiskInfo;
26+
protected $lockedList;
27+
protected $cacheStore;
28+
protected $db;
2529

2630
public function __construct()
2731
{
2832
$config = config('mediaManager');
2933

30-
$this->fileSystem = array_get($config, 'storage_disk');
31-
$this->storageDisk = app('filesystem')->disk($this->fileSystem);
32-
$this->ignoreFiles = array_get($config, 'ignore_files');
33-
$this->fileChars = array_get($config, 'allowed_fileNames_chars');
34-
$this->folderChars = array_get($config, 'allowed_folderNames_chars');
35-
$this->sanitizedText = array_get($config, 'sanitized_text');
36-
$this->unallowed_mimes = array_get($config, 'unallowed_mimes');
37-
$this->LMF = array_get($config, 'last_modified_format');
38-
39-
$this->locked_files_list = array_get($config, 'locked_files_list');
40-
$this->disks = config("filesystems.disks.{$this->fileSystem}");
34+
$this->fileSystem = array_get($config, 'storage_disk');
35+
$this->storageDisk = app('filesystem')->disk($this->fileSystem);
36+
$this->ignoreFiles = array_get($config, 'ignore_files');
37+
$this->fileChars = array_get($config, 'allowed_fileNames_chars');
38+
$this->folderChars = array_get($config, 'allowed_folderNames_chars');
39+
$this->sanitizedText = array_get($config, 'sanitized_text');
40+
$this->unallowedMimes = array_get($config, 'unallowed_mimes');
41+
$this->LMF = array_get($config, 'last_modified_format');
42+
43+
$this->db = app('db')->connection('mediamanager');
44+
$this->lockedList = $this->db->table('locked')->pluck('path');
45+
$this->storageDiskInfo = config("filesystems.disks.{$this->fileSystem}");
46+
$this->cacheStore = app('cache')->store('mediamanager');
4147
}
4248

4349
/**
@@ -68,7 +74,7 @@ public function get_files(Request $request)
6874
}
6975

7076
return response()->json([
71-
'locked' => app('db')->connection('mediamanager')->table('locked')->pluck('path'),
77+
'locked' => $this->lockedList,
7278
'files' => [
7379
'path' => $folder,
7480
'items' => $this->getData($folder),
@@ -121,7 +127,7 @@ public function upload(Request $request)
121127

122128
try {
123129
// check for mime type
124-
if (str_contains($file_type, $this->unallowed_mimes)) {
130+
if (str_contains($file_type, $this->unallowedMimes)) {
125131
throw new Exception(trans('MediaManager::messages.not_allowed_file_ext', ['attr'=>$file_type]));
126132
}
127133

@@ -291,7 +297,7 @@ public function move_file(Request $request)
291297
'size' => $file_size,
292298
];
293299
} else {
294-
$exc = array_get($this->disks, 'root')
300+
$exc = array_get($this->storageDiskInfo, 'root')
295301
? trans('MediaManager::messages.error_moving')
296302
: trans('MediaManager::messages.error_moving_cloud');
297303

@@ -334,7 +340,7 @@ public function move_file(Request $request)
334340
} else {
335341
$exc = trans('MediaManager::messages.error_moving');
336342

337-
if ('folder' == $one['type'] && !array_get($this->disks, 'root')) {
343+
if ('folder' == $one['type'] && !array_get($this->storageDiskInfo, 'root')) {
338344
$exc = trans('MediaManager::messages.error_moving_cloud');
339345
}
340346

@@ -410,7 +416,7 @@ public function lock_file(Request $request)
410416
{
411417
$path = $request->path;
412418
$state = $request->state;
413-
$db = app('db')->connection('mediamanager')->table('locked');
419+
$db = $this->db->table('locked');
414420

415421
'locked' == $state
416422
? $db->insert(['path'=>$path])
@@ -420,60 +426,136 @@ public function lock_file(Request $request)
420426
}
421427

422428
/**
423-
* zip folders.
424-
*
425-
* @param Request $request [description]
426-
*
427-
* @return [type] [description]
429+
* zip ops.
428430
*/
429431
public function folder_download(Request $request)
430432
{
431-
$name = $request->name;
432-
$dir = "{$request->folders}/$name";
433+
return $this->download(
434+
$request->name,
435+
$this->storageDisk->allFiles("{$request->folders}/$request->name"),
436+
'folder'
437+
);
438+
}
439+
440+
public function files_download(Request $request)
441+
{
442+
return $this->download(
443+
$request->name . '-files',
444+
json_decode($request->list, true),
445+
'files'
446+
);
447+
}
448+
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);
433456

434-
return response()->stream(function () use ($name, $dir) {
457+
return response()->stream(function () use ($name, $list, $type, $counter, $store, $cache_name) {
435458
$zip = new ZipStream("$name.zip", [
436459
'content_type' => 'application/octet-stream',
437460
]);
438461

439-
foreach ($this->storageDisk->allFiles($dir) as $file) {
440-
if ($streamRead = $this->storageDisk->readStream($file)) {
441-
$zip->addFileFromStream(pathinfo($file, PATHINFO_BASENAME), $streamRead);
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);
442474
} else {
443-
die('Could not open stream for reading');
475+
$store->forever("$cache_name.abort", $file_name);
476+
die();
444477
}
445478
}
446479

480+
$store->forever("$cache_name.done", true);
447481
$zip->finish();
448482
});
449483
}
450484

451485
/**
452-
* zip files.
453-
*
454-
* @param Request $request [description]
455-
*
456-
* @return [type] [description]
486+
* zip progress update.
457487
*/
458-
public function files_download(Request $request)
488+
public function zip_progress(Request $request)
459489
{
460-
$name = $request->name;
461-
$list = json_decode($request->list, true);
490+
// stop execution
491+
$start = time();
492+
$maxExecution = ini_get('max_execution_time');
493+
$sleep = array_get($this->storageDiskInfo, 'root') ? 0.5 : 1.5;
494+
$close = false;
495+
496+
// params
497+
$id = $request->header('last-event-id');
498+
$name = $request->name;
499+
500+
// get changes
501+
$store = $this->cacheStore;
502+
$cache_name = $name;
503+
504+
return response()->stream(function () use ($start, $maxExecution, $close, $sleep, $store, $cache_name) {
505+
while (!$close) {
506+
// 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);
514+
}
462515

463-
return response()->stream(function () use ($name, $list) {
464-
$zip = new ZipStream("$name.zip", [
465-
'content_type' => 'application/octet-stream',
466-
]);
516+
// abort
517+
if ($store->has("$cache_name.abort")) {
518+
$close = true;
519+
$this->es_msg('Could not open "' . $store->get("$cache_name.abort") . '" stream for reading.', 'abort');
520+
$this->clearZipCache($store, $cache_name);
521+
}
467522

468-
foreach ($list as $file) {
469-
if ($streamRead = fopen($file['path'], 'r')) {
470-
$zip->addFileFromStream($file['name'], $streamRead);
471-
} else {
472-
die('Could not open stream for reading');
523+
// done
524+
if ($store->has("$cache_name.done")) {
525+
$close = true;
526+
$this->es_msg(100, 'progress');
527+
$this->es_msg('All Done', 'done');
528+
$this->clearZipCache($store, $cache_name);
529+
}
530+
531+
ob_flush();
532+
flush();
533+
534+
// don't wait unnecessary
535+
if (!$close) {
536+
sleep($sleep);
473537
}
474538
}
539+
}, 200, [
540+
'Content-Type' => 'text/event-stream', // needed for SSE to work
541+
'Cache-Control' => 'no-cache', // make sure we dont cache this response
542+
'X-Accel-Buffering' => 'no', // needed for while loop to work
543+
'Access-Control-Allow-Origin' => config('app.url'), // for cors
544+
'Access-Control-Expose-Headers' => '*', // for cors
545+
'Access-Control-Allow-Credentials' => true, // for cors
546+
]);
547+
}
475548

476-
$zip->finish();
477-
});
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");
478560
}
479561
}

src/Controllers/OpsTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ protected function folderFiles($folder)
120120
*/
121121
protected function getFilePath($name)
122122
{
123-
$disks = $this->disks;
124-
$url = $this->storageDisk->url($name);
125-
$dir = str_replace(array_get($disks, 'url'), '', $url);
126-
$root = array_get($disks, 'root');
123+
$info = $this->storageDiskInfo;
124+
$url = $this->storageDisk->url($name);
125+
$dir = str_replace(array_get($info, 'url'), '', $url);
126+
$root = array_get($info, 'root');
127127

128128
// for other disks without root ex."cloud"
129129
if (!$root) {

src/MediaManagerServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ protected function packagePublish()
4343
'prefix' => '',
4444
]]);
4545

46+
// caching for zip-stream
47+
config(['cache.stores.mediamanager' => [
48+
'driver' => 'database',
49+
'table' => 'cache',
50+
'connection' => 'mediamanager',
51+
]]);
52+
4653
// public
4754
$this->publishes([
4855
__DIR__ . '/dist' => public_path('assets/vendor/MediaManager'),

src/MediaRoutes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static function routes()
2222
Route::post('rename_file', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@rename_file', 'as' => 'rename_file']);
2323
Route::post('lock_file', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@lock_file', 'as' => 'lock_file']);
2424

25+
Route::get('zip_progress/{name?}', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@zip_progress', 'as' => 'zip_progress']);
2526
Route::post('folder_download', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@folder_download', 'as' => 'folder_download']);
2627
Route::post('files_download', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@files_download', 'as' => 'files_download']);
2728
});

src/database/MediaManager.sqlite

16 KB
Binary file not shown.

src/resources/assets/js/components/media.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default {
3333
'filesRoute',
3434
'dirsRoute',
3535
'lockFileRoute',
36+
'zipProgressRoute',
3637
'restrictPath',
3738
'uploadPanelImgList',
3839
'hideExt',
@@ -118,10 +119,10 @@ export default {
118119
let ls = this.$ls.get('mediamanager')
119120
120121
if (ls) {
121-
this.randomNames = ls.randomNames == 'undefined' ? false : ls.randomNames
122-
this.folders = ls.folders == 'undefined' ? [] : ls.folders
123-
this.toolBar = ls.toolBar == 'undefined' ? true : ls.toolBar
124-
this.selectedFile = ls.selectedFileName == 'undefined' ? null : ls.selectedFileName
122+
this.randomNames = ls.randomNames === undefined ? false : ls.randomNames
123+
this.folders = ls.folders === undefined ? [] : ls.folders
124+
this.toolBar = ls.toolBar === undefined ? true : ls.toolBar
125+
this.selectedFile = ls.selectedFileName === undefined ? null : ls.selectedFileName
125126
}
126127
},
127128

0 commit comments

Comments
 (0)