Skip to content

Instantly share code, notes, and snippets.

@egin10
Last active January 24, 2021 10:32
Show Gist options
  • Save egin10/29aba94dd94e4277e1e52c389ca81e77 to your computer and use it in GitHub Desktop.
Save egin10/29aba94dd94e4277e1e52c389ca81e77 to your computer and use it in GitHub Desktop.
Custom Directory Files Elfinder By User ID
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Filesystem\FilesystemAdapter;
use Barryvdh\Elfinder\ElfinderController;
use Barryvdh\Elfinder\Connector;
use Barryvdh\Elfinder\Session\LaravelSession;
use elFinder;
use Tymon\JWTAuth\Facades\JWTAuth;
class CustomELfinderController extends ElfinderController
{
public function showTinyMCE5()
{
$access_token = request('access_token');
$user = [];
try {
$user = JWTAuth::setToken($access_token)->toUser();
} catch (\Exception $e) {
if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException) return responseJson('error', 'Token is Invalid', null, 401);
if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException) return responseJson('error', 'Token is Expired', null, 401);
return responseJson('error', 'Authorization Token not found', null, 401);
}
$pathUser = ($user->username == 'super_admin') ? '' : strval($user->id);
session(['access_path_user' => $pathUser]);
return $this->app['view']
->make($this->package . '::tinymce5')
->with($this->getViewVars());
}
public function showConnector()
{
$privatedir = session('access_path_user');
$roots = $this->app->config->get('elfinder.roots', []);
if (empty($roots)) {
$dirs = (array) $this->app['config']->get('elfinder.dir', []);
foreach ($dirs as $dir) {
$roots[] = [
'driver' => 'LocalFileSystem', // driver for accessing file system (REQUIRED)
'path' => public_path($dir), // path to files (REQUIRED)
'URL' => url($dir), // URL to files (REQUIRED)
'accessControl' => $this->app->config->get('elfinder.access') // filter callback (OPTIONAL)
];
}
$disks = (array) $this->app['config']->get('elfinder.disks', []);
foreach ($disks as $key => $root) {
if (is_string($root)) {
$key = $root;
$root = [];
}
$disk = app('filesystem')->disk($key);
if ($disk instanceof FilesystemAdapter) {
$defaults = [
'driver' => 'Flysystem',
'filesystem' => $disk->getDriver(),
'alias' => $key,
'path' => $privatedir,
];
$roots[] = array_merge($defaults, $root);
}
}
}
if (app()->bound('session.store')) {
$sessionStore = app('session.store');
$session = new LaravelSession($sessionStore);
} else {
$session = null;
}
$rootOptions = $this->app->config->get('elfinder.root_options', array());
foreach ($roots as $key => $root) {
$roots[$key] = array_merge($rootOptions, $root);
}
$opts = $this->app->config->get('elfinder.options', array());
$opts = array_merge($opts, ['roots' => $roots, 'session' => $session]);
// run elFinder
$connector = new Connector(new elFinder($opts));
$connector->run();
return $connector->getResponse();
}
}
<?php
use App\Http\Controllers\CustomELfinderController;
Route::prefix('custom-elfinder')->group(function () {
Route::any('connector', [CustomELfinderController::class, 'showConnector'])->name('elfinder.connector');
Route::get('tinymce5/{access_token}', [CustomELfinderController::class, 'showTinyMCE5'])->name('elfinder.tinymce5');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment