FIX: fix file manager
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace App\CMS\Helpers;
|
||||
|
||||
use App\Models\FileManager\Directory;
|
||||
use App\Models\FileManager\File;
|
||||
|
||||
class RootDirectory
|
||||
{
|
||||
protected static $directory;
|
||||
|
||||
/**
|
||||
* Create root directory
|
||||
*
|
||||
* @return Directory
|
||||
*/
|
||||
protected static function create()
|
||||
{
|
||||
$directory = new Directory();
|
||||
$directory->name = 'Root';
|
||||
$directory->is_root = true;
|
||||
$directory->save();
|
||||
|
||||
Directory::whereNull('parent_id')
|
||||
->where('is_root', false)
|
||||
->update(['parent_id' => $directory->id]);
|
||||
|
||||
File::where('directory_id', null)
|
||||
->update(['directory_id' => $directory->id]);
|
||||
|
||||
return $directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get root directory
|
||||
*
|
||||
* @return Directory
|
||||
*/
|
||||
public static function get()
|
||||
{
|
||||
self::$directory = Directory::where('is_root', true)
|
||||
->first();
|
||||
|
||||
if (is_null(self::$directory)) {
|
||||
self::$directory = self::create();
|
||||
}
|
||||
|
||||
return self::$directory;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace App\CMS\Repositories\FileManager;
|
||||
|
||||
use App\CMS\Helpers\RootDirectory;
|
||||
use App\Models\FileManager\Directory as DirectoryModel;
|
||||
use App\Models\FileManager\File;
|
||||
|
||||
class Directory
|
||||
{
|
||||
@@ -11,4 +13,25 @@ class Directory
|
||||
->where('parent_id', $directoryId)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DirectoryModel $directory
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function getAllSubdirectories(DirectoryModel &$directory)
|
||||
{
|
||||
if (!$directory->exists) {
|
||||
$directory = DirectoryModel::where('is_root', true)
|
||||
->first();
|
||||
|
||||
if (is_null($directory)) {
|
||||
$directory = RootDirectory::get();
|
||||
}
|
||||
}
|
||||
|
||||
$subdirectories = $directory->allSubdirectories();
|
||||
|
||||
return $subdirectories;
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin\FileManager;
|
||||
|
||||
use App\Events\EditFile;
|
||||
use App\CMS\Helpers\FileManager;
|
||||
use App\CMS\Helpers\RootDirectory;
|
||||
use App\Events\EditFile;
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests\EditUploadedFileRequest;
|
||||
use App\Models\FileManager\File;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class EditUploadedFile extends Controller
|
||||
{
|
||||
@@ -17,14 +17,18 @@ class EditUploadedFile extends Controller
|
||||
*
|
||||
* Render edit form for displaying details and updating given file
|
||||
*
|
||||
* @param int $id file id
|
||||
* @param File $file
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
|
||||
* @internal param int $id file id
|
||||
*
|
||||
*/
|
||||
public function show(File $file)
|
||||
{
|
||||
$root = RootDirectory::get();
|
||||
return view('admin.fileManager.edit', [
|
||||
'file' => $file,
|
||||
'root' => $root,
|
||||
'extraInfo' => FileManager::getAdditionalInfo($file)
|
||||
]);
|
||||
}
|
||||
@@ -48,6 +52,7 @@ class EditUploadedFile extends Controller
|
||||
$file->name = $request->name;
|
||||
$file->keywords = $request->keywords;
|
||||
$file->description = $request->description;
|
||||
$file->directory_id = $request->directory_id;
|
||||
$file->save();
|
||||
|
||||
// fire action
|
||||
|
||||
@@ -27,7 +27,7 @@ class UploadFile extends Controller
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
|
||||
@@ -1,12 +1,28 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers\Admin\FileManager;
|
||||
|
||||
use App\CMS\Repositories\FileManager\Directory as DirectoryRepository;
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Models\FileManager\Directory;
|
||||
use Illuminate\Support\Facades\Input;
|
||||
|
||||
class UploadedFilesOverview extends Controller
|
||||
{
|
||||
/**
|
||||
* @var DirectoryRepository
|
||||
*/
|
||||
private $directoryRepository;
|
||||
|
||||
/**
|
||||
* UploadedFilesOverview constructor.
|
||||
*
|
||||
* @param DirectoryRepository $directoryRepository
|
||||
*/
|
||||
public function __construct(DirectoryRepository $directoryRepository)
|
||||
{
|
||||
$this->directoryRepository = $directoryRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* List of all files and directories
|
||||
*
|
||||
@@ -19,13 +35,9 @@ class UploadedFilesOverview extends Controller
|
||||
// include hidden directories?
|
||||
$includeHidden = Input::get('hidden');
|
||||
|
||||
if (!$directory->exists) {
|
||||
$subdirectories = Directory::whereNull('parent_id');
|
||||
} else {
|
||||
$subdirectories = $directory->allSubdirectories();
|
||||
}
|
||||
$subdirectories = $this->directoryRepository->getAllSubdirectories($directory);
|
||||
|
||||
if ($includeHidden === false) {
|
||||
if (!$includeHidden) {
|
||||
$subdirectories = $subdirectories->where('hidden', false);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Models\FileManager;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Psy\Exception\ErrorException;
|
||||
|
||||
class Directory extends Model
|
||||
{
|
||||
@@ -17,7 +18,16 @@ class Directory extends Model
|
||||
'user_id',
|
||||
'hidden'
|
||||
];
|
||||
protected $casts = [
|
||||
'hidden' => 'bool',
|
||||
'is_root' => 'bool'
|
||||
];
|
||||
|
||||
/**
|
||||
* Files in directory
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function files()
|
||||
{
|
||||
return $this->hasMany(File::class);
|
||||
@@ -30,14 +40,7 @@ class Directory extends Model
|
||||
*/
|
||||
public function allSubdirectories()
|
||||
{
|
||||
$subdirectories = $this->hasMany(Directory::class, 'parent_id', 'id');
|
||||
|
||||
// todo fix this ugly hack! statement above produces WHERE parent_id IS NOT NULL and parent_id IS NULL when selecting subdirectories from root/none directory
|
||||
if (is_null($this->id)) {
|
||||
$subdirectories = $subdirectories->orWhereNull('parent_id');
|
||||
}
|
||||
|
||||
return $subdirectories;
|
||||
return $this->hasMany(Directory::class, 'parent_id', 'id');
|
||||
}
|
||||
|
||||
public function subdirectories()
|
||||
@@ -47,4 +50,14 @@ class Directory extends Model
|
||||
|
||||
return $subdirectories;
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
// try to prevent from deleting root folder
|
||||
if ($this->is_root == true) {
|
||||
throw new ErrorException('Cannot delete root folder!');
|
||||
}
|
||||
|
||||
return parent::delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ class File extends Model
|
||||
'user_id',
|
||||
'file_size',
|
||||
'mime_type',
|
||||
'hash_algorith',
|
||||
'hash_algorithm',
|
||||
'name',
|
||||
'file_size'
|
||||
'file_hash'
|
||||
];
|
||||
|
||||
public function directory()
|
||||
|
||||
@@ -4,12 +4,8 @@ namespace App\Models\FileManager\ModelTraits;
|
||||
|
||||
use App\Events\UploadFile;
|
||||
use App\Models\FileManager\File as FileModel;
|
||||
use Franzose\ClosureTable\Extensions\Str;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\File as FileFacade;
|
||||
use Intervention\Image\Constraint;
|
||||
use Intervention\Image\Facades\Image;
|
||||
use Symfony\Component\HttpFoundation\File\File as SymphonyFile;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
@@ -26,7 +22,7 @@ trait File
|
||||
*/
|
||||
public function getFilename(UploadedFile $file)
|
||||
{
|
||||
return uniqid() . '.' . $file->getClientOriginalExtension();
|
||||
return date('Y-m-').uniqid() . '.' . $file->getClientOriginalExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,12 +44,6 @@ trait File
|
||||
// save file into physical location (local FS)
|
||||
$uploadedFile = $file->move($destination, $filename);
|
||||
|
||||
// is uploaded file an image?
|
||||
if (Str::startsWith($uploadedFile->getMimeType(), 'image')) {
|
||||
// yes, create thumbnail
|
||||
$this->createThumbnail($uploadedFile);
|
||||
}
|
||||
|
||||
// insert record into database
|
||||
$id = $this->saveIntoDatabase($uploadedFile, $data, $file->getClientOriginalName());
|
||||
|
||||
@@ -101,29 +91,4 @@ trait File
|
||||
|
||||
return $newFile->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create thumbnail of given image
|
||||
*
|
||||
* @param SymphonyFile $file
|
||||
*/
|
||||
private function createThumbnail(SymphonyFile $file)
|
||||
{
|
||||
// does thumbnail directory exist?
|
||||
if (!is_dir(config('app.thumbnail_path'))) {
|
||||
FileFacade::makeDirectory(config('app.thumbnail_path'));
|
||||
}
|
||||
|
||||
// get image instance
|
||||
$image = Image::make(config('app.upload_path') . DIRECTORY_SEPARATOR . $file->getFilename());
|
||||
|
||||
// resize image and keep aspect ration
|
||||
$image->resize(env('THUMBNAIL_WIDTH', 300), null, function (Constraint $constraint) {
|
||||
$constraint->aspectRatio();
|
||||
$constraint->upsize();
|
||||
});
|
||||
|
||||
// save thumbnail with lower quality
|
||||
$image->save(config('app.thumbnail_path') . DIRECTORY_SEPARATOR . $file->getFilename(), 75);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user