Skip to content

Commit

Permalink
v2.3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ctf0 committed Dec 23, 2017
1 parent 449bed5 commit 1a4f217
Show file tree
Hide file tree
Showing 19 changed files with 449 additions and 220 deletions.
30 changes: 0 additions & 30 deletions logs/v2.3.7.txt

This file was deleted.

10 changes: 10 additions & 0 deletions logs/v2.3.8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- fix “select + click” for search
- add support for zip progress
- new styling for path breadcrumb on mobile
- add animation for breadcrumbs
- fix local-storage toolbar changing on its own
- some cleanup to the controller

- update assets
- update db file
- update language file
182 changes: 132 additions & 50 deletions src/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,39 @@ class MediaController extends Controller
{
use OpsTrait;

// main
protected $fileSystem;
protected $storageDisk;
protected $ignoreFiles;
protected $fileChars;
protected $folderChars;
protected $sanitizedText;
protected $unallowed_mimes;
protected $unallowedMimes;
protected $LMF;

protected $locked_files_list;
protected $disks;
// extra
protected $storageDiskInfo;
protected $lockedList;
protected $cacheStore;
protected $db;

public function __construct()
{
$config = config('mediaManager');

$this->fileSystem = array_get($config, 'storage_disk');
$this->storageDisk = app('filesystem')->disk($this->fileSystem);
$this->ignoreFiles = array_get($config, 'ignore_files');
$this->fileChars = array_get($config, 'allowed_fileNames_chars');
$this->folderChars = array_get($config, 'allowed_folderNames_chars');
$this->sanitizedText = array_get($config, 'sanitized_text');
$this->unallowed_mimes = array_get($config, 'unallowed_mimes');
$this->LMF = array_get($config, 'last_modified_format');

$this->locked_files_list = array_get($config, 'locked_files_list');
$this->disks = config("filesystems.disks.{$this->fileSystem}");
$this->fileSystem = array_get($config, 'storage_disk');
$this->storageDisk = app('filesystem')->disk($this->fileSystem);
$this->ignoreFiles = array_get($config, 'ignore_files');
$this->fileChars = array_get($config, 'allowed_fileNames_chars');
$this->folderChars = array_get($config, 'allowed_folderNames_chars');
$this->sanitizedText = array_get($config, 'sanitized_text');
$this->unallowedMimes = array_get($config, 'unallowed_mimes');
$this->LMF = array_get($config, 'last_modified_format');

$this->db = app('db')->connection('mediamanager');
$this->lockedList = $this->db->table('locked')->pluck('path');
$this->storageDiskInfo = config("filesystems.disks.{$this->fileSystem}");
$this->cacheStore = app('cache')->store('mediamanager');
}

/**
Expand Down Expand Up @@ -68,7 +74,7 @@ public function get_files(Request $request)
}

return response()->json([
'locked' => app('db')->connection('mediamanager')->table('locked')->pluck('path'),
'locked' => $this->lockedList,
'files' => [
'path' => $folder,
'items' => $this->getData($folder),
Expand Down Expand Up @@ -121,7 +127,7 @@ public function upload(Request $request)

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

Expand Down Expand Up @@ -291,7 +297,7 @@ public function move_file(Request $request)
'size' => $file_size,
];
} else {
$exc = array_get($this->disks, 'root')
$exc = array_get($this->storageDiskInfo, 'root')
? trans('MediaManager::messages.error_moving')
: trans('MediaManager::messages.error_moving_cloud');

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

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

Expand Down Expand Up @@ -410,7 +416,7 @@ public function lock_file(Request $request)
{
$path = $request->path;
$state = $request->state;
$db = app('db')->connection('mediamanager')->table('locked');
$db = $this->db->table('locked');

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

/**
* zip folders.
*
* @param Request $request [description]
*
* @return [type] [description]
* zip ops.
*/
public function folder_download(Request $request)
{
$name = $request->name;
$dir = "{$request->folders}/$name";
return $this->download(
$request->name,
$this->storageDisk->allFiles("{$request->folders}/$request->name"),
'folder'
);
}

public function files_download(Request $request)
{
return $this->download(
$request->name . '-files',
json_decode($request->list, true),
'files'
);
}

protected function download($name, $list, $type)
{
// track changes
$counter = 100 / count($list);
$store = $this->cacheStore;
$cache_name = $name;
$store->forever("$cache_name.progress", 0);

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

foreach ($this->storageDisk->allFiles($dir) as $file) {
if ($streamRead = $this->storageDisk->readStream($file)) {
$zip->addFileFromStream(pathinfo($file, PATHINFO_BASENAME), $streamRead);
foreach ($list as $file) {
if ('folder' == $type) {
$file_name = pathinfo($file, PATHINFO_BASENAME);
$streamRead = $this->storageDisk->readStream($file);
} else {
$file_name = $file['name'];
$streamRead = @fopen($file['path'], 'r');
}

if ($streamRead) {
$store->increment("$cache_name.progress", round($counter, 2));
$zip->addFileFromStream($file_name, $streamRead);
} else {
die('Could not open stream for reading');
$store->forever("$cache_name.abort", $file_name);
die();
}
}

$store->forever("$cache_name.done", true);
$zip->finish();
});
}

/**
* zip files.
*
* @param Request $request [description]
*
* @return [type] [description]
* zip progress update.
*/
public function files_download(Request $request)
public function zip_progress(Request $request)
{
$name = $request->name;
$list = json_decode($request->list, true);
// stop execution
$start = time();
$maxExecution = ini_get('max_execution_time');
$sleep = array_get($this->storageDiskInfo, 'root') ? 0.5 : 1.5;
$close = false;

// params
$id = $request->header('last-event-id');
$name = $request->name;

// get changes
$store = $this->cacheStore;
$cache_name = $name;

return response()->stream(function () use ($start, $maxExecution, $close, $sleep, $store, $cache_name) {
while (!$close) {
// progress
$this->es_msg($store->get("$cache_name.progress"), 'progress');

// avoid server crash
if (time() >= $start + $maxExecution) {
$close = true;
$this->es_msg(null, 'exit');
$this->clearZipCache($store, $cache_name);
}

return response()->stream(function () use ($name, $list) {
$zip = new ZipStream("$name.zip", [
'content_type' => 'application/octet-stream',
]);
// abort
if ($store->has("$cache_name.abort")) {
$close = true;
$this->es_msg('Could not open "' . $store->get("$cache_name.abort") . '" stream for reading.', 'abort');
$this->clearZipCache($store, $cache_name);
}

foreach ($list as $file) {
if ($streamRead = fopen($file['path'], 'r')) {
$zip->addFileFromStream($file['name'], $streamRead);
} else {
die('Could not open stream for reading');
// done
if ($store->has("$cache_name.done")) {
$close = true;
$this->es_msg(100, 'progress');
$this->es_msg('All Done', 'done');
$this->clearZipCache($store, $cache_name);
}

ob_flush();
flush();

// don't wait unnecessary
if (!$close) {
sleep($sleep);
}
}
}, 200, [
'Content-Type' => 'text/event-stream', // needed for SSE to work
'Cache-Control' => 'no-cache', // make sure we dont cache this response
'X-Accel-Buffering' => 'no', // needed for while loop to work
'Access-Control-Allow-Origin' => config('app.url'), // for cors
'Access-Control-Expose-Headers' => '*', // for cors
'Access-Control-Allow-Credentials' => true, // for cors
]);
}

$zip->finish();
});
protected function es_msg($data = null, $event = null)
{
echo $event ? "event: $event\n" : ':';
echo $data ? 'data: ' . json_encode(['response' => $data]) . "\n\n" : ':';
}

protected function clearZipCache($store, $item)
{
$store->forget("$item.progress");
$store->forget("$item.done");
$store->forget("$item.abort");
}
}
8 changes: 4 additions & 4 deletions src/Controllers/OpsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ protected function folderFiles($folder)
*/
protected function getFilePath($name)
{
$disks = $this->disks;
$url = $this->storageDisk->url($name);
$dir = str_replace(array_get($disks, 'url'), '', $url);
$root = array_get($disks, 'root');
$info = $this->storageDiskInfo;
$url = $this->storageDisk->url($name);
$dir = str_replace(array_get($info, 'url'), '', $url);
$root = array_get($info, 'root');

// for other disks without root ex."cloud"
if (!$root) {
Expand Down
7 changes: 7 additions & 0 deletions src/MediaManagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ protected function packagePublish()
'prefix' => '',
]]);

// caching for zip-stream
config(['cache.stores.mediamanager' => [
'driver' => 'database',
'table' => 'cache',
'connection' => 'mediamanager',
]]);

// public
$this->publishes([
__DIR__ . '/dist' => public_path('assets/vendor/MediaManager'),
Expand Down
1 change: 1 addition & 0 deletions src/MediaRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static function routes()
Route::post('rename_file', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@rename_file', 'as' => 'rename_file']);
Route::post('lock_file', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@lock_file', 'as' => 'lock_file']);

Route::get('zip_progress/{name?}', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@zip_progress', 'as' => 'zip_progress']);
Route::post('folder_download', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@folder_download', 'as' => 'folder_download']);
Route::post('files_download', ['uses' => '\ctf0\MediaManager\Controllers\MediaController@files_download', 'as' => 'files_download']);
});
Expand Down
Binary file modified src/database/MediaManager.sqlite
Binary file not shown.
9 changes: 5 additions & 4 deletions src/resources/assets/js/components/media.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default {
'filesRoute',
'dirsRoute',
'lockFileRoute',
'zipProgressRoute',
'restrictPath',
'uploadPanelImgList',
'hideExt',
Expand Down Expand Up @@ -118,10 +119,10 @@ export default {
let ls = this.$ls.get('mediamanager')
if (ls) {
this.randomNames = ls.randomNames == 'undefined' ? false : ls.randomNames
this.folders = ls.folders == 'undefined' ? [] : ls.folders
this.toolBar = ls.toolBar == 'undefined' ? true : ls.toolBar
this.selectedFile = ls.selectedFileName == 'undefined' ? null : ls.selectedFileName
this.randomNames = ls.randomNames === undefined ? false : ls.randomNames
this.folders = ls.folders === undefined ? [] : ls.folders
this.toolBar = ls.toolBar === undefined ? true : ls.toolBar
this.selectedFile = ls.selectedFileName === undefined ? null : ls.selectedFileName
}
},
Expand Down
Loading

0 comments on commit 1a4f217

Please sign in to comment.