laravel-6 support
This commit is contained in:
@@ -6,9 +6,6 @@ use Intervention\Image\Facades\Image;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageIsCropping;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageWasCropped;
|
||||
|
||||
/**
|
||||
* Class CropController.
|
||||
*/
|
||||
class CropController extends LfmController
|
||||
{
|
||||
/**
|
||||
@@ -18,11 +15,11 @@ class CropController extends LfmController
|
||||
*/
|
||||
public function getCrop()
|
||||
{
|
||||
$working_dir = request('working_dir');
|
||||
$img = parent::objectPresenter(parent::getCurrentPath(request('img')));
|
||||
|
||||
return view('laravel-filemanager::crop')
|
||||
->with(compact('working_dir', 'img'));
|
||||
->with([
|
||||
'working_dir' => request('working_dir'),
|
||||
'img' => $this->lfm->pretty(request('img'))
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,34 +27,27 @@ class CropController extends LfmController
|
||||
*/
|
||||
public function getCropimage($overWrite = true)
|
||||
{
|
||||
$dataX = request('dataX');
|
||||
$dataY = request('dataY');
|
||||
$dataHeight = request('dataHeight');
|
||||
$dataWidth = request('dataWidth');
|
||||
$image_path = parent::getCurrentPath(request('img'));
|
||||
$image_name = request('img');
|
||||
$image_path = $this->lfm->setName($image_name)->path('absolute');
|
||||
$crop_path = $image_path;
|
||||
|
||||
if (! $overWrite) {
|
||||
$fileParts = explode('.', request('img'));
|
||||
$fileParts = explode('.', $image_name);
|
||||
$fileParts[count($fileParts) - 2] = $fileParts[count($fileParts) - 2] . '_cropped_' . time();
|
||||
$crop_path = parent::getCurrentPath(implode('.', $fileParts));
|
||||
$crop_path = $this->lfm->setName(implode('.', $fileParts))->path('absolute');
|
||||
}
|
||||
|
||||
event(new ImageIsCropping($image_path));
|
||||
|
||||
$crop_info = request()->only('dataWidth', 'dataHeight', 'dataX', 'dataY');
|
||||
|
||||
// crop image
|
||||
Image::make($image_path)
|
||||
->crop($dataWidth, $dataHeight, $dataX, $dataY)
|
||||
->crop(...array_values($crop_info))
|
||||
->save($crop_path);
|
||||
|
||||
if (config('lfm.should_create_thumbnails', true)) {
|
||||
// create thumb folder
|
||||
parent::createFolderByPath(parent::getThumbPath());
|
||||
|
||||
// make new thumbnail
|
||||
Image::make($crop_path)
|
||||
->fit(config('lfm.thumb_img_width', 200), config('lfm.thumb_img_height', 200))
|
||||
->save(parent::getThumbPath(parent::getName($crop_path)));
|
||||
}
|
||||
// make new thumbnail
|
||||
$this->lfm->generateThumbnail($image_name);
|
||||
|
||||
event(new ImageWasCropped($image_path));
|
||||
}
|
||||
|
@@ -2,13 +2,14 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use UniSharp\LaravelFilemanager\Events\FileIsDeleting;
|
||||
use UniSharp\LaravelFilemanager\Events\FileWasDeleted;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderIsDeleting;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderWasDeleted;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageIsDeleting;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageWasDeleted;
|
||||
|
||||
/**
|
||||
* Class CropController.
|
||||
*/
|
||||
class DeleteController extends LfmController
|
||||
{
|
||||
/**
|
||||
@@ -18,39 +19,61 @@ class DeleteController extends LfmController
|
||||
*/
|
||||
public function getDelete()
|
||||
{
|
||||
$name_to_delete = request('items');
|
||||
$item_names = request('items');
|
||||
$errors = [];
|
||||
|
||||
$file_to_delete = parent::getCurrentPath($name_to_delete);
|
||||
$thumb_to_delete = parent::getThumbPath($name_to_delete);
|
||||
foreach ($item_names as $name_to_delete) {
|
||||
$file = $this->lfm->setName($name_to_delete);
|
||||
|
||||
event(new ImageIsDeleting($file_to_delete));
|
||||
|
||||
if (is_null($name_to_delete)) {
|
||||
return parent::error('folder-name');
|
||||
}
|
||||
|
||||
if (! File::exists($file_to_delete)) {
|
||||
return parent::error('folder-not-found', ['folder' => $file_to_delete]);
|
||||
}
|
||||
|
||||
if (File::isDirectory($file_to_delete)) {
|
||||
if (! parent::directoryIsEmpty($file_to_delete)) {
|
||||
return parent::error('delete-folder');
|
||||
if ($file->isDirectory()) {
|
||||
event(new FolderIsDeleting($file->path('absolute')));
|
||||
} else {
|
||||
event(new FileIsDeleting($file->path('absolute')));
|
||||
event(new ImageIsDeleting($file->path('absolute')));
|
||||
}
|
||||
|
||||
File::deleteDirectory($file_to_delete);
|
||||
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return parent::$success_response;
|
||||
$file_to_delete = $this->lfm->pretty($name_to_delete);
|
||||
$file_path = $file_to_delete->path('absolute');
|
||||
|
||||
if (is_null($name_to_delete)) {
|
||||
array_push($errors, parent::error('folder-name'));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->lfm->setName($name_to_delete)->exists()) {
|
||||
array_push($errors, parent::error('folder-not-found', ['folder' => $file_path]));
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->lfm->setName($name_to_delete)->isDirectory()) {
|
||||
if (! $this->lfm->setName($name_to_delete)->directoryIsEmpty()) {
|
||||
array_push($errors, parent::error('delete-folder'));
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->lfm->setName($name_to_delete)->delete();
|
||||
|
||||
event(new FolderWasDeleted($file_path));
|
||||
} else {
|
||||
if ($file_to_delete->isImage()) {
|
||||
$this->lfm->setName($name_to_delete)->thumb()->delete();
|
||||
}
|
||||
|
||||
$this->lfm->setName($name_to_delete)->delete();
|
||||
|
||||
event(new FileWasDeleted($file_path));
|
||||
event(new ImageWasDeleted($file_path));
|
||||
}
|
||||
}
|
||||
|
||||
if (parent::fileIsImage($file_to_delete)) {
|
||||
File::delete($thumb_to_delete);
|
||||
if (count($errors) > 0) {
|
||||
return $errors;
|
||||
}
|
||||
|
||||
File::delete($file_to_delete);
|
||||
|
||||
event(new ImageWasDeleted($file_to_delete));
|
||||
|
||||
return parent::$success_response;
|
||||
}
|
||||
}
|
||||
|
@@ -2,14 +2,8 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
/**
|
||||
* Class DemoController.
|
||||
*/
|
||||
class DemoController extends LfmController
|
||||
{
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('laravel-filemanager::demo');
|
||||
|
@@ -2,18 +2,18 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
/**
|
||||
* Class DownloadController.
|
||||
*/
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class DownloadController extends LfmController
|
||||
{
|
||||
/**
|
||||
* Download a file.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDownload()
|
||||
{
|
||||
return response()->download(parent::getCurrentPath(request('file')));
|
||||
$file = $this->lfm->setName(request('file'));
|
||||
|
||||
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return response()->download($file->path('absolute'));
|
||||
}
|
||||
}
|
||||
|
@@ -2,11 +2,9 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderIsCreating;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderWasCreated;
|
||||
|
||||
/**
|
||||
* Class FolderController.
|
||||
*/
|
||||
class FolderController extends LfmController
|
||||
{
|
||||
/**
|
||||
@@ -16,35 +14,23 @@ class FolderController extends LfmController
|
||||
*/
|
||||
public function getFolders()
|
||||
{
|
||||
$folder_types = [];
|
||||
$root_folders = [];
|
||||
|
||||
if (parent::allowMultiUser()) {
|
||||
$folder_types['user'] = 'root';
|
||||
}
|
||||
|
||||
if (parent::allowShareFolder()) {
|
||||
$folder_types['share'] = 'shares';
|
||||
}
|
||||
|
||||
foreach ($folder_types as $folder_type => $lang_key) {
|
||||
$root_folder_path = parent::getRootFolderPath($folder_type);
|
||||
|
||||
$children = parent::getDirectories($root_folder_path);
|
||||
usort($children, function ($a, $b) {
|
||||
return strcmp($a->name, $b->name);
|
||||
});
|
||||
|
||||
array_push($root_folders, (object) [
|
||||
'name' => trans('laravel-filemanager::lfm.title-' . $lang_key),
|
||||
'path' => parent::getInternalPath($root_folder_path),
|
||||
'children' => $children,
|
||||
'has_next' => ! ($lang_key == end($folder_types)),
|
||||
]);
|
||||
}
|
||||
$folder_types = array_filter(['user', 'share'], function ($type) {
|
||||
return $this->helper->allowFolderType($type);
|
||||
});
|
||||
|
||||
return view('laravel-filemanager::tree')
|
||||
->with(compact('root_folders'));
|
||||
->with([
|
||||
'root_folders' => array_map(function ($type) use ($folder_types) {
|
||||
$path = $this->lfm->dir($this->helper->getRootFolder($type));
|
||||
|
||||
return (object) [
|
||||
'name' => trans('laravel-filemanager::lfm.title-' . $type),
|
||||
'url' => $path->path('working_dir'),
|
||||
'children' => $path->folders(),
|
||||
'has_next' => ! ($type == end($folder_types)),
|
||||
];
|
||||
}, $folder_types),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,22 +40,28 @@ class FolderController extends LfmController
|
||||
*/
|
||||
public function getAddfolder()
|
||||
{
|
||||
$folder_name = parent::translateFromUtf8(trim(request('name')));
|
||||
$path = parent::getCurrentPath($folder_name);
|
||||
$folder_name = $this->helper->input('name');
|
||||
|
||||
if (empty($folder_name)) {
|
||||
return parent::error('folder-name');
|
||||
$new_path = $this->lfm->setName($folder_name)->path('absolute');
|
||||
|
||||
event(new FolderIsCreating($new_path));
|
||||
|
||||
try {
|
||||
if ($folder_name === null || $folder_name == '') {
|
||||
return $this->helper->error('folder-name');
|
||||
} elseif ($this->lfm->setName($folder_name)->exists()) {
|
||||
return $this->helper->error('folder-exist');
|
||||
} elseif (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $folder_name)) {
|
||||
return $this->helper->error('folder-alnum');
|
||||
} else {
|
||||
$this->lfm->setName($folder_name)->createFolder();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
if (File::exists($path)) {
|
||||
return parent::error('folder-exist');
|
||||
}
|
||||
event(new FolderWasCreated($new_path));
|
||||
|
||||
if (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $folder_name)) {
|
||||
return parent::error('folder-alnum');
|
||||
}
|
||||
|
||||
parent::createFolderByPath($path);
|
||||
return parent::$success_response;
|
||||
}
|
||||
}
|
||||
|
@@ -2,9 +2,12 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
/**
|
||||
* Class ItemsController.
|
||||
*/
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use UniSharp\LaravelFilemanager\Events\FileIsMoving;
|
||||
use UniSharp\LaravelFilemanager\Events\FileWasMoving;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderIsMoving;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderWasMoving;
|
||||
|
||||
class ItemsController extends LfmController
|
||||
{
|
||||
/**
|
||||
@@ -14,47 +17,90 @@ class ItemsController extends LfmController
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
$path = parent::getCurrentPath();
|
||||
$sort_type = request('sort_type');
|
||||
$currentPage = self::getCurrentPageFromRequest();
|
||||
|
||||
$files = parent::sortFilesAndDirectories(parent::getFilesWithInfo($path), $sort_type);
|
||||
$directories = parent::sortFilesAndDirectories(parent::getDirectories($path), $sort_type);
|
||||
$perPage = $this->helper->getPaginationPerPage();
|
||||
$items = array_merge($this->lfm->folders(), $this->lfm->files());
|
||||
|
||||
return [
|
||||
'html' => (string) view($this->getView())->with([
|
||||
'files' => $files,
|
||||
'directories' => $directories,
|
||||
'items' => array_merge($directories, $files),
|
||||
]),
|
||||
'working_dir' => parent::getInternalPath($path),
|
||||
'items' => array_map(function ($item) {
|
||||
return $item->fill()->attributes;
|
||||
}, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
|
||||
'paginator' => [
|
||||
'current_page' => $currentPage,
|
||||
'total' => count($items),
|
||||
'per_page' => $perPage,
|
||||
],
|
||||
'display' => $this->helper->getDisplayMode(),
|
||||
'working_dir' => $this->lfm->path('working_dir'),
|
||||
];
|
||||
}
|
||||
|
||||
private function getView()
|
||||
public function move()
|
||||
{
|
||||
$view_type = request('show_list');
|
||||
$items = request('items');
|
||||
$folder_types = array_filter(['user', 'share'], function ($type) {
|
||||
return $this->helper->allowFolderType($type);
|
||||
});
|
||||
return view('laravel-filemanager::move')
|
||||
->with([
|
||||
'root_folders' => array_map(function ($type) use ($folder_types) {
|
||||
$path = $this->lfm->dir($this->helper->getRootFolder($type));
|
||||
|
||||
if (null === $view_type) {
|
||||
return $this->composeViewName($this->getStartupViewFromConfig());
|
||||
}
|
||||
|
||||
$view_mapping = [
|
||||
'0' => 'grid',
|
||||
'1' => 'list'
|
||||
];
|
||||
|
||||
return $this->composeViewName($view_mapping[$view_type]);
|
||||
return (object) [
|
||||
'name' => trans('laravel-filemanager::lfm.title-' . $type),
|
||||
'url' => $path->path('working_dir'),
|
||||
'children' => $path->folders(),
|
||||
'has_next' => ! ($type == end($folder_types)),
|
||||
];
|
||||
}, $folder_types),
|
||||
])
|
||||
->with('items', $items);
|
||||
}
|
||||
|
||||
private function composeViewName($view_type = 'grid')
|
||||
public function domove()
|
||||
{
|
||||
return "laravel-filemanager::$view_type-view";
|
||||
$target = $this->helper->input('goToFolder');
|
||||
$items = $this->helper->input('items');
|
||||
|
||||
foreach ($items as $item) {
|
||||
$old_file = $this->lfm->pretty($item);
|
||||
$is_directory = $old_file->isDirectory();
|
||||
|
||||
$file = $this->lfm->setName($item);
|
||||
|
||||
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$old_path = $old_file->path();
|
||||
|
||||
if ($old_file->hasThumb()) {
|
||||
$new_file = $this->lfm->setName($item)->thumb()->dir($target);
|
||||
if ($is_directory) {
|
||||
event(new FolderIsMoving($old_file->path(), $new_file->path()));
|
||||
} else {
|
||||
event(new FileIsMoving($old_file->path(), $new_file->path()));
|
||||
}
|
||||
$this->lfm->setName($item)->thumb()->move($new_file);
|
||||
}
|
||||
$new_file = $this->lfm->setName($item)->dir($target);
|
||||
$this->lfm->setName($item)->move($new_file);
|
||||
if ($is_directory) {
|
||||
event(new FolderWasMoving($old_path, $new_file->path()));
|
||||
} else {
|
||||
event(new FileWasMoving($old_path, $new_file->path()));
|
||||
}
|
||||
};
|
||||
|
||||
return parent::$success_response;
|
||||
}
|
||||
|
||||
private function getStartupViewFromConfig($default = 'grid')
|
||||
private static function getCurrentPageFromRequest()
|
||||
{
|
||||
$type_key = parent::currentLfmType();
|
||||
$startup_view = config('lfm.' . $type_key . 's_startup_view', $default);
|
||||
return $startup_view;
|
||||
$currentPage = (int) request()->get('page', 1);
|
||||
$currentPage = $currentPage < 1 ? 1 : $currentPage;
|
||||
|
||||
return $currentPage;
|
||||
}
|
||||
}
|
||||
|
@@ -2,15 +2,11 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
use UniSharp\LaravelFilemanager\Traits\LfmHelpers;
|
||||
use UniSharp\LaravelFilemanager\Lfm;
|
||||
use UniSharp\LaravelFilemanager\LfmPath;
|
||||
|
||||
/**
|
||||
* Class LfmController.
|
||||
*/
|
||||
class LfmController extends Controller
|
||||
{
|
||||
use LfmHelpers;
|
||||
|
||||
protected static $success_response = 'OK';
|
||||
|
||||
public function __construct()
|
||||
@@ -18,6 +14,20 @@ class LfmController extends Controller
|
||||
$this->applyIniOverrides();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up needed functions.
|
||||
*
|
||||
* @return object|null
|
||||
*/
|
||||
public function __get($var_name)
|
||||
{
|
||||
if ($var_name === 'lfm') {
|
||||
return app(LfmPath::class);
|
||||
} elseif ($var_name === 'helper') {
|
||||
return app(Lfm::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the filemanager.
|
||||
*
|
||||
@@ -25,9 +35,15 @@ class LfmController extends Controller
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return view('laravel-filemanager::index');
|
||||
return view('laravel-filemanager::index')
|
||||
->withHelper($this->helper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if any extension or config is missing.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors()
|
||||
{
|
||||
$arr_errors = [];
|
||||
@@ -36,14 +52,42 @@ class LfmController extends Controller
|
||||
array_push($arr_errors, trans('laravel-filemanager::lfm.message-extension_not_found'));
|
||||
}
|
||||
|
||||
$type_key = $this->currentLfmType();
|
||||
$mine_config = 'lfm.valid_' . $type_key . '_mimetypes';
|
||||
$config_error = null;
|
||||
if (! extension_loaded('exif')) {
|
||||
array_push($arr_errors, 'EXIF extension not found.');
|
||||
}
|
||||
|
||||
if (! is_array(config($mine_config))) {
|
||||
array_push($arr_errors, 'Config : ' . $mine_config . ' is not a valid array.');
|
||||
if (! extension_loaded('fileinfo')) {
|
||||
array_push($arr_errors, 'Fileinfo extension not found.');
|
||||
}
|
||||
|
||||
$mine_config_key = 'lfm.folder_categories.'
|
||||
. $this->helper->currentLfmType()
|
||||
. '.valid_mime';
|
||||
|
||||
if (! is_array(config($mine_config_key))) {
|
||||
array_push($arr_errors, 'Config : ' . $mine_config_key . ' is not a valid array.');
|
||||
}
|
||||
|
||||
return $arr_errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides settings in php.ini.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function applyIniOverrides()
|
||||
{
|
||||
$overrides = config('lfm.php_ini_overrides', []);
|
||||
|
||||
if ($overrides && is_array($overrides) && count($overrides) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($overrides as $key => $value) {
|
||||
if ($value && $value != 'false') {
|
||||
ini_set($key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,64 +2,19 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* Class RedirectController.
|
||||
*/
|
||||
class RedirectController extends LfmController
|
||||
{
|
||||
private $file_path;
|
||||
|
||||
public function __construct()
|
||||
public function showFile($file_path)
|
||||
{
|
||||
$delimiter = config('lfm.url_prefix', config('lfm.prefix')). '/';
|
||||
$url = urldecode(request()->url());
|
||||
$external_path = substr($url, strpos($url, $delimiter) + strlen($delimiter));
|
||||
$storage = Storage::disk($this->helper->config('disk'));
|
||||
|
||||
$this->file_path = base_path(config('lfm.base_directory', 'public') . '/' . $external_path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image from custom directory by route.
|
||||
*
|
||||
* @param string $image_path
|
||||
* @return string
|
||||
*/
|
||||
public function getImage($base_path, $image_name)
|
||||
{
|
||||
return $this->responseImageOrFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file from custom directory by route.
|
||||
*
|
||||
* @param string $file_name
|
||||
* @return string
|
||||
*/
|
||||
public function getFile(Request $request, $base_path, $file_name)
|
||||
{
|
||||
$request->request->add(['type' => 'Files']);
|
||||
|
||||
return $this->responseImageOrFile();
|
||||
}
|
||||
|
||||
private function responseImageOrFile()
|
||||
{
|
||||
$file_path = $this->file_path;
|
||||
|
||||
if (! File::exists($file_path)) {
|
||||
if (! $storage->exists($file_path)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$file = File::get($file_path);
|
||||
$type = parent::getFileType($file_path);
|
||||
|
||||
$response = Response::make($file);
|
||||
$response->header('Content-Type', $type);
|
||||
|
||||
return $response;
|
||||
return response($storage->get($file_path))
|
||||
->header('Content-Type', $storage->mimeType($file_path));
|
||||
}
|
||||
}
|
||||
|
@@ -2,86 +2,79 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming;
|
||||
use UniSharp\LaravelFilemanager\Events\FolderWasRenamed;
|
||||
use UniSharp\LaravelFilemanager\Events\FileIsRenaming;
|
||||
use UniSharp\LaravelFilemanager\Events\FileWasRenamed;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
|
||||
|
||||
/**
|
||||
* Class RenameController.
|
||||
*/
|
||||
class RenameController extends LfmController
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRename()
|
||||
{
|
||||
$old_name = parent::translateFromUtf8(request('file'));
|
||||
$new_name = parent::translateFromUtf8(trim(request('new_name')));
|
||||
$old_name = $this->helper->input('file');
|
||||
$new_name = $this->helper->input('new_name');
|
||||
|
||||
$old_file = parent::getCurrentPath($old_name);
|
||||
if (File::isDirectory($old_file)) {
|
||||
return $this->renameDirectory($old_name, $new_name);
|
||||
} else {
|
||||
return $this->renameFile($old_name, $new_name);
|
||||
$file = $this->lfm->setName($old_name);
|
||||
|
||||
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
|
||||
abort(404);
|
||||
}
|
||||
}
|
||||
|
||||
protected function renameDirectory($old_name, $new_name)
|
||||
{
|
||||
$old_file = $this->lfm->pretty($old_name);
|
||||
|
||||
$is_directory = $file->isDirectory();
|
||||
|
||||
if (empty($new_name)) {
|
||||
return parent::error('folder-name');
|
||||
if ($is_directory) {
|
||||
return parent::error('folder-name');
|
||||
} else {
|
||||
return parent::error('file-name');
|
||||
}
|
||||
}
|
||||
|
||||
$old_file = parent::getCurrentPath($old_name);
|
||||
$new_file = parent::getCurrentPath($new_name);
|
||||
|
||||
event(new FolderIsRenaming($old_file, $new_file));
|
||||
|
||||
if (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
|
||||
if ($is_directory && config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
|
||||
return parent::error('folder-alnum');
|
||||
}
|
||||
|
||||
if (File::exists($new_file)) {
|
||||
return parent::error('rename');
|
||||
}
|
||||
|
||||
File::move($old_file, $new_file);
|
||||
event(new FolderWasRenamed($old_file, $new_file));
|
||||
|
||||
return parent::$success_response;
|
||||
}
|
||||
|
||||
protected function renameFile($old_name, $new_name)
|
||||
{
|
||||
if (empty($new_name)) {
|
||||
return parent::error('file-name');
|
||||
}
|
||||
|
||||
$old_file = parent::getCurrentPath($old_name);
|
||||
$extension = File::extension($old_file);
|
||||
$new_file = parent::getCurrentPath(basename($new_name, ".$extension") . ".$extension");
|
||||
|
||||
if (config('lfm.alphanumeric_filename') && preg_match('/[^\w-.]/i', $new_name)) {
|
||||
} elseif (config('lfm.alphanumeric_filename') && preg_match('/[^.\w-]/i', $new_name)) {
|
||||
return parent::error('file-alnum');
|
||||
}
|
||||
|
||||
// TODO Should be "FileIsRenaming"
|
||||
event(new ImageIsRenaming($old_file, $new_file));
|
||||
|
||||
if (File::exists($new_file)) {
|
||||
} elseif ($this->lfm->setName($new_name)->exists()) {
|
||||
return parent::error('rename');
|
||||
}
|
||||
|
||||
if (parent::fileIsImage($old_file) && File::exists(parent::getThumbPath($old_name))) {
|
||||
File::move(parent::getThumbPath($old_name), parent::getThumbPath($new_name));
|
||||
if (! $is_directory) {
|
||||
$extension = $old_file->extension();
|
||||
if ($extension) {
|
||||
$new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension;
|
||||
}
|
||||
}
|
||||
|
||||
File::move($old_file, $new_file);
|
||||
// TODO Should be "FileWasRenamed"
|
||||
event(new ImageWasRenamed($old_file, $new_file));
|
||||
$new_path = $this->lfm->setName($new_name)->path('absolute');
|
||||
|
||||
if ($is_directory) {
|
||||
event(new FolderIsRenaming($old_file->path(), $new_path));
|
||||
} else {
|
||||
event(new FileIsRenaming($old_file->path(), $new_path));
|
||||
event(new ImageIsRenaming($old_file->path(), $new_path));
|
||||
}
|
||||
|
||||
$old_path = $old_file->path();
|
||||
|
||||
if ($old_file->hasThumb()) {
|
||||
$this->lfm->setName($old_name)->thumb()
|
||||
->move($this->lfm->setName($new_name)->thumb());
|
||||
}
|
||||
|
||||
$this->lfm->setName($old_name)
|
||||
->move($this->lfm->setName($new_name));
|
||||
|
||||
if ($is_directory) {
|
||||
event(new FolderWasRenamed($old_path, $new_path));
|
||||
} else {
|
||||
event(new FileWasRenamed($old_path, $new_path));
|
||||
event(new ImageWasRenamed($old_path, $new_path));
|
||||
}
|
||||
|
||||
return parent::$success_response;
|
||||
}
|
||||
|
@@ -6,9 +6,6 @@ use Intervention\Image\Facades\Image;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageIsResizing;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageWasResized;
|
||||
|
||||
/**
|
||||
* Class ResizeController.
|
||||
*/
|
||||
class ResizeController extends LfmController
|
||||
{
|
||||
/**
|
||||
@@ -21,7 +18,7 @@ class ResizeController extends LfmController
|
||||
$ratio = 1.0;
|
||||
$image = request('img');
|
||||
|
||||
$original_image = Image::make(parent::getCurrentPath($image));
|
||||
$original_image = Image::make($this->lfm->setName($image)->path('absolute'));
|
||||
$original_width = $original_image->width();
|
||||
$original_height = $original_image->height();
|
||||
|
||||
@@ -46,7 +43,7 @@ class ResizeController extends LfmController
|
||||
}
|
||||
|
||||
return view('laravel-filemanager::resize')
|
||||
->with('img', parent::objectPresenter(parent::getCurrentPath($image)))
|
||||
->with('img', $this->lfm->pretty($image))
|
||||
->with('height', number_format($height, 0))
|
||||
->with('width', $width)
|
||||
->with('original_height', $original_height)
|
||||
@@ -57,14 +54,10 @@ class ResizeController extends LfmController
|
||||
|
||||
public function performResize()
|
||||
{
|
||||
$dataX = request('dataX');
|
||||
$dataY = request('dataY');
|
||||
$height = request('dataHeight');
|
||||
$width = request('dataWidth');
|
||||
$image_path = parent::getCurrentPath(request('img'));
|
||||
$image_path = $this->lfm->setName(request('img'))->path('absolute');
|
||||
|
||||
event(new ImageIsResizing($image_path));
|
||||
Image::make($image_path)->resize($width, $height)->save();
|
||||
Image::make($image_path)->resize(request('dataWidth'), request('dataHeight'))->save();
|
||||
event(new ImageWasResized($image_path));
|
||||
|
||||
return parent::$success_response;
|
||||
|
@@ -2,16 +2,10 @@
|
||||
|
||||
namespace UniSharp\LaravelFilemanager\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Intervention\Image\Facades\Image;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageIsUploading;
|
||||
use UniSharp\LaravelFilemanager\Events\ImageWasUploaded;
|
||||
use UniSharp\LaravelFilemanager\Lfm;
|
||||
|
||||
/**
|
||||
* Class UploadController.
|
||||
*/
|
||||
class UploadController extends LfmController
|
||||
{
|
||||
protected $errors;
|
||||
@@ -26,211 +20,47 @@ class UploadController extends LfmController
|
||||
* Upload files
|
||||
*
|
||||
* @param void
|
||||
* @return string
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
$files = request()->file('upload');
|
||||
$uploaded_files = request()->file('upload');
|
||||
$error_bag = [];
|
||||
$new_filename = null;
|
||||
|
||||
// single file
|
||||
if (!is_array($files)) {
|
||||
$file = $files;
|
||||
if (!$this->fileIsValid($file)) {
|
||||
return $this->errors;
|
||||
foreach (is_array($uploaded_files) ? $uploaded_files : [$uploaded_files] as $file) {
|
||||
try {
|
||||
$this->lfm->validateUploadedFile($file);
|
||||
|
||||
$new_filename = $this->lfm->upload($file);
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e->getMessage(), [
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
array_push($error_bag, $e->getMessage());
|
||||
}
|
||||
|
||||
$filename = $this->proceedSingleUpload($file);
|
||||
if ($filename === false) {
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
// upload via ckeditor 'Upload' tab
|
||||
return $this->useFile($filename);
|
||||
}
|
||||
|
||||
|
||||
// Multiple files
|
||||
foreach ($files as $file) {
|
||||
if (!$this->fileIsValid($file)) {
|
||||
continue;
|
||||
}
|
||||
$this->proceedSingleUpload($file);
|
||||
}
|
||||
|
||||
return count($this->errors) > 0 ? $this->errors : parent::$success_response;
|
||||
}
|
||||
|
||||
private function proceedSingleUpload($file)
|
||||
{
|
||||
$new_filename = $this->getNewName($file);
|
||||
$new_file_path = parent::getCurrentPath($new_filename);
|
||||
|
||||
event(new ImageIsUploading($new_file_path));
|
||||
try {
|
||||
if (parent::fileIsImage($file) && !in_array($file->getMimeType(), ['image/gif', 'image/svg+xml'])) {
|
||||
// Handle image rotation
|
||||
Image::make($file->getRealPath())
|
||||
->orientate() //Apply orientation from exif data
|
||||
->save($new_file_path);
|
||||
|
||||
// Generate a thumbnail
|
||||
if (parent::imageShouldHaveThumb($file)) {
|
||||
$this->makeThumb($new_filename);
|
||||
}
|
||||
if (is_array($uploaded_files)) {
|
||||
$response = count($error_bag) > 0 ? $error_bag : parent::$success_response;
|
||||
} else { // upload via ckeditor5 expects json responses
|
||||
if (is_null($new_filename)) {
|
||||
$response = [
|
||||
'error' => [ 'message' => $error_bag[0] ]
|
||||
];
|
||||
} else {
|
||||
// Create (move) the file
|
||||
File::move($file->getRealPath(), $new_file_path);
|
||||
}
|
||||
if (config('lfm.should_change_file_mode', true)) {
|
||||
chmod($new_file_path, config('lfm.create_file_mode', 0644));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
array_push($this->errors, parent::error('invalid'));
|
||||
$url = $this->lfm->setName($new_filename)->url();
|
||||
|
||||
Log::error($e->getMessage(), [
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO should be "FileWasUploaded"
|
||||
event(new ImageWasUploaded(realpath($new_file_path)));
|
||||
|
||||
return $new_filename;
|
||||
}
|
||||
|
||||
private function fileIsValid($file)
|
||||
{
|
||||
if (empty($file)) {
|
||||
array_push($this->errors, parent::error('file-empty'));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $file instanceof UploadedFile) {
|
||||
array_push($this->errors, parent::error('instance'));
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($file->getError() == UPLOAD_ERR_INI_SIZE) {
|
||||
$max_size = ini_get('upload_max_filesize');
|
||||
array_push($this->errors, parent::error('file-size', ['max' => $max_size]));
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($file->getError() != UPLOAD_ERR_OK) {
|
||||
$msg = 'File failed to upload. Error code: ' . $file->getError();
|
||||
array_push($this->errors, $msg);
|
||||
return false;
|
||||
}
|
||||
|
||||
$new_filename = $this->getNewName($file);
|
||||
|
||||
if (File::exists(parent::getCurrentPath($new_filename))) {
|
||||
array_push($this->errors, parent::error('file-exist'));
|
||||
return false;
|
||||
}
|
||||
|
||||
$mimetype = $file->getMimeType();
|
||||
|
||||
// Bytes to KB
|
||||
$file_size = $file->getSize() / 1024;
|
||||
$type_key = parent::currentLfmType();
|
||||
|
||||
if (config('lfm.should_validate_mime', false)) {
|
||||
$mine_config = 'lfm.valid_' . $type_key . '_mimetypes';
|
||||
$valid_mimetypes = config($mine_config, []);
|
||||
if (false === in_array($mimetype, $valid_mimetypes)) {
|
||||
array_push($this->errors, parent::error('mime') . $mimetype);
|
||||
return false;
|
||||
$response = [
|
||||
'url' => $url,
|
||||
'uploaded' => $url
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (config('lfm.should_validate_size', false)) {
|
||||
$max_size = config('lfm.max_' . $type_key . '_size', 0);
|
||||
if ($file_size > $max_size) {
|
||||
array_push($this->errors, parent::error('size'));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function replaceInsecureSuffix($name)
|
||||
{
|
||||
return preg_replace("/\.php$/i", '', $name);
|
||||
}
|
||||
|
||||
private function getNewName($file)
|
||||
{
|
||||
$new_filename = parent::translateFromUtf8(trim($this->pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)));
|
||||
if (config('lfm.rename_file') === true) {
|
||||
$new_filename = uniqid();
|
||||
} elseif (config('lfm.alphanumeric_filename') === true) {
|
||||
$new_filename = preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_filename);
|
||||
}
|
||||
|
||||
return $new_filename . $this->replaceInsecureSuffix('.' . $file->getClientOriginalExtension());
|
||||
}
|
||||
|
||||
private function makeThumb($new_filename)
|
||||
{
|
||||
// create thumb folder
|
||||
parent::createFolderByPath(parent::getThumbPath());
|
||||
|
||||
// create thumb image
|
||||
Image::make(parent::getCurrentPath($new_filename))
|
||||
->fit(config('lfm.thumb_img_width', 200), config('lfm.thumb_img_height', 200))
|
||||
->save(parent::getThumbPath($new_filename));
|
||||
}
|
||||
|
||||
private function useFile($new_filename)
|
||||
{
|
||||
$file = parent::getFileUrl($new_filename);
|
||||
|
||||
$responseType = request()->input('responseType');
|
||||
if ($responseType && $responseType == 'json') {
|
||||
return [
|
||||
"uploaded" => 1,
|
||||
"fileName" => $new_filename,
|
||||
"url" => $file,
|
||||
];
|
||||
}
|
||||
|
||||
return "<script type='text/javascript'>
|
||||
|
||||
function getUrlParam(paramName) {
|
||||
var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i');
|
||||
var match = window.location.search.match(reParam);
|
||||
return ( match && match.length > 1 ) ? match[1] : null;
|
||||
}
|
||||
|
||||
var funcNum = getUrlParam('CKEditorFuncNum');
|
||||
|
||||
var par = window.parent,
|
||||
op = window.opener,
|
||||
o = (par && par.CKEDITOR) ? par : ((op && op.CKEDITOR) ? op : false);
|
||||
|
||||
if (op) window.close();
|
||||
if (o !== false) o.CKEDITOR.tools.callFunction(funcNum, '$file');
|
||||
</script>";
|
||||
}
|
||||
|
||||
private function pathinfo($path, $options = null)
|
||||
{
|
||||
$path = urlencode($path);
|
||||
$parts = is_null($options) ? pathinfo($path) : pathinfo($path, $options);
|
||||
if (is_array($parts)) {
|
||||
foreach ($parts as $field => $value) {
|
||||
$parts[$field] = urldecode($value);
|
||||
}
|
||||
} else {
|
||||
$parts = urldecode($parts);
|
||||
}
|
||||
|
||||
return $parts;
|
||||
return response()->json($response);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user