diff --git a/README.md b/README.md index 49cd7cb..25e98ba 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,14 @@ Media manager for `local` disk. ## Installation -``` -$ composer require laravel-admin-ext/media-manager -vvv +```shell +// For laravel-admin 1.x +composer require laravel-admin-ext/media-manager:1.x -vvv + +// For laravel-admin 2.x +composer require laravel-admin-ext/media-manager:2.x -vvv -$ php artisan admin:import media-manager +php artisan admin:import media-manager ``` Add a disk config in `config/admin.php`: @@ -30,9 +34,10 @@ Add a disk config in `config/admin.php`: 'extensions' => [ 'media-manager' => [ - +            // Select a local disk that you configured in `config/filesystem.php` -         'disk' => 'public' +         'disk' => 'public', + 'allowed_ext' => 'jpg,jpeg,png,pdf,doc,docx,zip' ], ], diff --git a/composer.json b/composer.json index 9b10c9a..f7b05cb 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,10 @@ ], "require": { "php": ">=7.0.0", - "laravel/framework": "5.5.*", - "encore/laravel-admin": "1.5.*" + "encore/laravel-admin": "~1.6" }, "require-dev": { - "phpunit/phpunit": "~6.0", - "laravel/laravel": "5.*" + "phpunit/phpunit": "~6.0" }, "autoload": { "psr-4": { @@ -30,7 +28,6 @@ "providers": [ "Encore\\Admin\\Media\\MediaServiceProvider" ] - } } } diff --git a/resources/views/list.blade.php b/resources/views/list.blade.php index c1c78e1..25e5d2b 100644 --- a/resources/views/list.blade.php +++ b/resources/views/list.blade.php @@ -74,29 +74,37 @@ showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "{{ trans('admin.confirm') }}", + showLoaderOnConfirm: true, closeOnConfirm: false, - cancelButtonText: "{{ trans('admin.cancel') }}" - }, - function(){ - $.ajax({ - method: 'delete', - url: '{{ $url['delete'] }}', - data: { - 'files[]':[path], - _token:LA.token, - }, - success: function (data) { - $.pjax.reload('#pjax-container'); - - if (typeof data === 'object') { - if (data.status) { - swal(data.message, '', 'success'); - } else { - swal(data.message, '', 'error'); + cancelButtonText: "{{ trans('admin.cancel') }}", + preConfirm: function() { + return new Promise(function(resolve) { + + $.ajax({ + method: 'delete', + url: '{{ $url['delete'] }}', + data: { + 'files[]':[path], + _token:LA.token + }, + success: function (data) { + $.pjax.reload('#pjax-container'); + + resolve(data); } - } + }); + + }); + } + }).then(function(result){ + var data = result.value; + if (typeof data === 'object') { + if (data.status) { + swal(data.message, '', 'success'); + } else { + swal(data.message, '', 'error'); } - }); + } }); }); @@ -215,29 +223,37 @@ function closeModal() { showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "{{ trans('admin.confirm') }}", + showLoaderOnConfirm: true, closeOnConfirm: false, - cancelButtonText: "{{ trans('admin.cancel') }}" - }, - function(){ - $.ajax({ - method: 'delete', - url: '{{ $url['delete'] }}', - data: { - 'files[]': files, - _token:LA.token, - }, - success: function (data) { - $.pjax.reload('#pjax-container'); - - if (typeof data === 'object') { - if (data.status) { - swal(data.message, '', 'success'); - } else { - swal(data.message, '', 'error'); + cancelButtonText: "{{ trans('admin.cancel') }}", + preConfirm: function() { + return new Promise(function(resolve) { + + $.ajax({ + method: 'delete', + url: '{{ $url['delete'] }}', + data: { + 'files[]': files, + _token:LA.token + }, + success: function (data) { + $.pjax.reload('#pjax-container'); + + resolve(data); } - } + }); + + }); + } + }).then(function (result) { + var data = result.value; + if (typeof data === 'object') { + if (data.status) { + swal(data.message, '', 'success'); + } else { + swal(data.message, '', 'error'); } - }); + } }); }); }); @@ -318,7 +334,7 @@ function(){ {!! $item['preview'] !!}
- + {{ $item['icon'] }} {{ basename($item['name']) }} @@ -333,7 +349,7 @@ function(){
  • Rename & Move
  • Delete
  • @unless($item['isDir']) -
  • Download
  • +
  • Download
  • @endunless
  • Url
  • @@ -419,4 +435,4 @@ function(){
    - \ No newline at end of file + diff --git a/resources/views/table.blade.php b/resources/views/table.blade.php index 2d7fd22..d42ce5e 100644 --- a/resources/views/table.blade.php +++ b/resources/views/table.blade.php @@ -52,29 +52,37 @@ showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "{{ trans('admin.confirm') }}", + showLoaderOnConfirm: true, closeOnConfirm: false, - cancelButtonText: "{{ trans('admin.cancel') }}" - }, - function(){ - $.ajax({ - method: 'delete', - url: '{{ $url['delete'] }}', - data: { - 'files[]':[path], - _token:LA.token, - }, - success: function (data) { - $.pjax.reload('#pjax-container'); - - if (typeof data === 'object') { - if (data.status) { - swal(data.message, '', 'success'); - } else { - swal(data.message, '', 'error'); + cancelButtonText: "{{ trans('admin.cancel') }}", + preConfirm: function() { + return new Promise(function(resolve) { + + $.ajax({ + method: 'delete', + url: '{{ $url['delete'] }}', + data: { + 'files[]':[path], + _token:LA.token + }, + success: function (data) { + $.pjax.reload('#pjax-container'); + + resolve(data); } - } + }); + + }); + } + }).then(function(result){ + var data = result.value; + if (typeof data === 'object') { + if (data.status) { + swal(data.message, '', 'success'); + } else { + swal(data.message, '', 'error'); } - }); + } }); }); @@ -215,29 +223,37 @@ function closeModal() { showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "{{ trans('admin.confirm') }}", + showLoaderOnConfirm: true, closeOnConfirm: false, - cancelButtonText: "{{ trans('admin.cancel') }}" - }, - function(){ - $.ajax({ - method: 'delete', - url: '{{ $url['delete'] }}', - data: { - 'files[]': files, - _token:LA.token, - }, - success: function (data) { - $.pjax.reload('#pjax-container'); - - if (typeof data === 'object') { - if (data.status) { - swal(data.message, '', 'success'); - } else { - swal(data.message, '', 'error'); + cancelButtonText: "{{ trans('admin.cancel') }}", + preConfirm: function() { + return new Promise(function(resolve) { + + $.ajax({ + method: 'delete', + url: '{{ $url['delete'] }}', + data: { + 'files[]': files, + _token:LA.token + }, + success: function (data) { + $.pjax.reload('#pjax-container'); + + resolve(data); } - } + }); + + }); + } + }).then(function (result) { + var data = result.value; + if (typeof data === 'object') { + if (data.status) { + swal(data.message, '', 'success'); + } else { + swal(data.message, '', 'error'); } - }); + } }); }); @@ -336,7 +352,7 @@ function(){ {!! $item['preview'] !!} - + {{ $item['icon'] }} {{ basename($item['name']) }} @@ -346,7 +362,7 @@ function(){ @unless($item['isDir']) - + @endunless @@ -434,4 +450,4 @@ function(){ - \ No newline at end of file + diff --git a/src/MediaController.php b/src/MediaController.php index 47df705..f6b96a3 100644 --- a/src/MediaController.php +++ b/src/MediaController.php @@ -33,7 +33,14 @@ public function download(Request $request) $manager = new MediaManager($file); - return $manager->download(); + try { + return $manager->download(); + } catch (\Exception $e) { + return response()->json([ + 'status' => false, + 'message' => $e->getMessage(), + ]); + } } public function upload(Request $request) @@ -69,7 +76,7 @@ public function delete(Request $request) } } catch (\Exception $e) { return response()->json([ - 'status' => true, + 'status' => false, 'message' => $e->getMessage(), ]); } @@ -91,7 +98,7 @@ public function move(Request $request) } } catch (\Exception $e) { return response()->json([ - 'status' => true, + 'status' => false, 'message' => $e->getMessage(), ]); } @@ -113,7 +120,7 @@ public function newFolder(Request $request) } } catch (\Exception $e) { return response()->json([ - 'status' => true, + 'status' => false, 'message' => $e->getMessage(), ]); } diff --git a/src/MediaManager.php b/src/MediaManager.php index 68710ff..392e264 100644 --- a/src/MediaManager.php +++ b/src/MediaManager.php @@ -26,6 +26,13 @@ class MediaManager extends Extension */ protected $storage; + /** + * List of allowed extensions. + * + * @var string + */ + protected $allowed = []; + /** * @var array */ @@ -50,6 +57,10 @@ public function __construct($path = '/') { $this->path = $path; + if (!empty(config('admin.extensions.media-manager.allowed_ext'))) { + $this->allowed = explode(',', config('admin.extensions.media-manager.allowed_ext')); + } + $this->initStorage(); } @@ -77,10 +88,10 @@ public function ls() $directories = $this->storage->directories($this->path); return $this->formatDirectories($directories) - ->merge($this->formatFiles($files)) - ->sort(function ($item) { - return $item['name']; - })->all(); + ->merge($this->formatFiles($files)) + ->sort(function ($item) { + return $item['name']; + })->all(); } /** @@ -92,7 +103,12 @@ public function ls() */ protected function getFullPath($path) { - return $this->storage->getDriver()->getAdapter()->applyPathPrefix($path); + $fullPath = $this->storage->getDriver()->getAdapter()->applyPathPrefix($path); + if (strstr($fullPath, '..')) { + throw new \Exception('Incorrect path'); + } + + return $fullPath; } public function download() @@ -125,6 +141,11 @@ public function delete($path) public function move($new) { + $ext = pathinfo($new, PATHINFO_EXTENSION); + if ($this->allowed && !in_array($ext, $this->allowed)) { + throw new \Exception('File extension '.$ext.' is not allowed'); + } + return $this->storage->move($this->path, $new); } @@ -137,6 +158,10 @@ public function move($new) public function upload($files = []) { foreach ($files as $file) { + if ($this->allowed && !in_array($file->getClientOriginalExtension(), $this->allowed)) { + throw new \Exception('File extension '.$file->getClientOriginalExtension().' is not allowed'); + } + $this->storage->putFileAs($this->path, $file, $file->getClientOriginalName()); }