Skip to content

Commit 5454fd1

Browse files
committed
modify namespace of controllers and middlewares
1 parent 8f89b27 commit 5454fd1

29 files changed

+640
-17
lines changed

src/Controllers/Controller.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
use Illuminate\Foundation\Bus\DispatchesJobs;
6+
use Illuminate\Routing\Controller as BaseController;
7+
use Illuminate\Foundation\Validation\ValidatesRequests;
8+
9+
abstract class Controller extends BaseController
10+
{
11+
use DispatchesJobs, ValidatesRequests;
12+
}

src/Controllers/CropController.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
use Intervention\Image\Facades\Image;
6+
use UniSharp\LaravelFilemanager\Events\ImageIsCropping;
7+
use UniSharp\LaravelFilemanager\Events\ImageWasCropped;
8+
9+
class CropController extends LfmController
10+
{
11+
/**
12+
* Show crop page.
13+
*
14+
* @return mixed
15+
*/
16+
public function getCrop()
17+
{
18+
return view('laravel-filemanager::crop')
19+
->with([
20+
'working_dir' => request('working_dir'),
21+
'img' => $this->lfm->pretty(request('img'))
22+
]);
23+
}
24+
25+
/**
26+
* Crop the image (called via ajax).
27+
*/
28+
public function getCropimage($overWrite = true)
29+
{
30+
$image_name = request('img');
31+
$image_path = $this->lfm->setName($image_name)->path('absolute');
32+
$crop_path = $image_path;
33+
34+
if (! $overWrite) {
35+
$fileParts = explode('.', $image_name);
36+
$fileParts[count($fileParts) - 2] = $fileParts[count($fileParts) - 2] . '_cropped_' . time();
37+
$crop_path = $this->lfm->setName(implode('.', $fileParts))->path('absolute');
38+
}
39+
40+
event(new ImageIsCropping($image_path));
41+
42+
$crop_info = request()->only('dataWidth', 'dataHeight', 'dataX', 'dataY');
43+
44+
// crop image
45+
Image::make($image_path)
46+
->crop(...array_values($crop_info))
47+
->save($crop_path);
48+
49+
// make new thumbnail
50+
$this->lfm->makeThumbnail($image_name);
51+
52+
event(new ImageWasCropped($image_path));
53+
}
54+
55+
public function getNewCropimage()
56+
{
57+
$this->getCropimage(false);
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
use UniSharp\LaravelFilemanager\Events\ImageIsDeleting;
6+
use UniSharp\LaravelFilemanager\Events\ImageWasDeleted;
7+
8+
class DeleteController extends LfmController
9+
{
10+
/**
11+
* Delete image and associated thumbnail.
12+
*
13+
* @return mixed
14+
*/
15+
public function getDelete()
16+
{
17+
$item_names = request('items');
18+
$errors = [];
19+
20+
foreach ($item_names as $name_to_delete) {
21+
$file_to_delete = $this->lfm->pretty($name_to_delete);
22+
$file_path = $file_to_delete->path();
23+
24+
event(new ImageIsDeleting($file_path));
25+
26+
if (is_null($name_to_delete)) {
27+
array_push($errors, parent::error('folder-name'));
28+
continue;
29+
}
30+
31+
if (! $this->lfm->setName($name_to_delete)->exists()) {
32+
array_push($errors, parent::error('folder-not-found', ['folder' => $file_path]));
33+
continue;
34+
}
35+
36+
if ($this->lfm->setName($name_to_delete)->isDirectory()) {
37+
if (! $this->lfm->setName($name_to_delete)->directoryIsEmpty()) {
38+
array_push($errors, parent::error('delete-folder'));
39+
continue;
40+
}
41+
} else {
42+
if ($file_to_delete->isImage()) {
43+
$this->lfm->setName($name_to_delete)->thumb()->delete();
44+
}
45+
}
46+
47+
$this->lfm->setName($name_to_delete)->delete();
48+
49+
event(new ImageWasDeleted($file_path));
50+
}
51+
52+
if (count($errors) > 0) {
53+
return $errors;
54+
}
55+
56+
return parent::$success_response;
57+
}
58+
}

src/Controllers/DemoController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
class DemoController extends LfmController
6+
{
7+
public function index()
8+
{
9+
return view('laravel-filemanager::demo');
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
class DownloadController extends LfmController
6+
{
7+
public function getDownload()
8+
{
9+
return response()->download($this->lfm->setName(request('file'))->path('absolute'));
10+
}
11+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
class FolderController extends LfmController
6+
{
7+
/**
8+
* Get list of folders as json to populate treeview.
9+
*
10+
* @return mixed
11+
*/
12+
public function getFolders()
13+
{
14+
$folder_types = array_filter(['user', 'share'], function ($type) {
15+
return $this->helper->allowFolderType($type);
16+
});
17+
18+
return view('laravel-filemanager::tree')
19+
->with([
20+
'root_folders' => array_map(function ($type) use ($folder_types) {
21+
$path = $this->lfm->dir($this->helper->getRootFolder($type));
22+
23+
return (object) [
24+
'name' => trans('laravel-filemanager::lfm.title-' . $type),
25+
'url' => $path->path('working_dir'),
26+
'children' => $path->folders(),
27+
'has_next' => ! ($type == end($folder_types)),
28+
];
29+
}, $folder_types),
30+
]);
31+
}
32+
33+
/**
34+
* Add a new folder.
35+
*
36+
* @return mixed
37+
*/
38+
public function getAddfolder()
39+
{
40+
$folder_name = $this->helper->input('name');
41+
42+
try {
43+
if (empty($folder_name)) {
44+
return $this->helper->error('folder-name');
45+
} elseif ($this->lfm->setName($folder_name)->exists()) {
46+
return $this->helper->error('folder-exist');
47+
} elseif (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $folder_name)) {
48+
return $this->helper->error('folder-alnum');
49+
} else {
50+
$this->lfm->setName($folder_name)->createFolder();
51+
}
52+
} catch (\Exception $e) {
53+
return $e->getMessage();
54+
}
55+
56+
return parent::$success_response;
57+
}
58+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
class ItemsController extends LfmController
6+
{
7+
/**
8+
* Get the images to load for a selected folder.
9+
*
10+
* @return mixed
11+
*/
12+
public function getItems()
13+
{
14+
return [
15+
'items' => array_map(function ($item) {
16+
return $item->fill()->attributes;
17+
}, array_merge($this->lfm->folders(), $this->lfm->files())),
18+
'display' => $this->helper->getDisplayMode(),
19+
'working_dir' => $this->lfm->path('working_dir'),
20+
];
21+
}
22+
}

src/Controllers/LfmController.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
use UniSharp\LaravelFilemanager\Lfm;
6+
use UniSharp\LaravelFilemanager\LfmPath;
7+
8+
class LfmController extends Controller
9+
{
10+
protected static $success_response = 'OK';
11+
12+
public function __construct()
13+
{
14+
$this->applyIniOverrides();
15+
}
16+
17+
/**
18+
* Set up needed functions.
19+
*
20+
* @return object|null
21+
*/
22+
public function __get($var_name)
23+
{
24+
if ($var_name === 'lfm') {
25+
return app(LfmPath::class);
26+
} elseif ($var_name === 'helper') {
27+
return app(Lfm::class);
28+
}
29+
}
30+
31+
/**
32+
* Show the filemanager.
33+
*
34+
* @return mixed
35+
*/
36+
public function show()
37+
{
38+
return view('laravel-filemanager::index')
39+
->withHelper($this->helper);
40+
}
41+
42+
/**
43+
* Check if any extension or config is missing.
44+
*
45+
* @return array
46+
*/
47+
public function getErrors()
48+
{
49+
$arr_errors = [];
50+
51+
if (! extension_loaded('gd') && ! extension_loaded('imagick')) {
52+
array_push($arr_errors, trans('laravel-filemanager::lfm.message-extension_not_found'));
53+
}
54+
55+
if (! extension_loaded('exif')) {
56+
array_push($arr_errors, 'EXIF extension not found.');
57+
}
58+
59+
if (! extension_loaded('fileinfo')) {
60+
array_push($arr_errors, 'Fileinfo extension not found.');
61+
}
62+
63+
$mine_config_key = 'lfm.folder_categories.'
64+
. $this->helper->currentLfmType()
65+
. '.valid_mime';
66+
67+
if (! is_array(config($mine_config_key))) {
68+
array_push($arr_errors, 'Config : ' . $mine_config_key . ' is not a valid array.');
69+
}
70+
71+
return $arr_errors;
72+
}
73+
74+
public function error($error_type, $variables = [])
75+
{
76+
return $this->helper->error($error_type, $variables);
77+
}
78+
79+
/**
80+
* Overrides settings in php.ini.
81+
*
82+
* @return null
83+
*/
84+
public function applyIniOverrides()
85+
{
86+
$overrides = config('lfm.php_ini_overrides');
87+
if ($overrides && count($overrides) === 0) {
88+
return;
89+
}
90+
91+
foreach ($overrides as $key => $value) {
92+
if ($value && $value != 'false') {
93+
ini_set($key, $value);
94+
}
95+
}
96+
}
97+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Controllers;
4+
5+
use Illuminate\Support\Facades\Storage;
6+
7+
class RedirectController extends LfmController
8+
{
9+
public function showFile($file_path)
10+
{
11+
$storage = Storage::disk($this->helper->config('disk'));
12+
13+
if (! $storage->exists($file_path)) {
14+
abort(404);
15+
}
16+
17+
return response($storage->get($file_path))
18+
->header('Content-Type', $storage->mimeType($file_path));
19+
}
20+
}

0 commit comments

Comments
 (0)