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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class MarkDirectoryAsRoot extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('directories', function (Blueprint $table) {
|
||||
$table->boolean('is_root')->default(false);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('directories', function (Blueprint $table) {
|
||||
$table->dropColumn('is_root');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Modules\Blog\Modificators;
|
||||
|
||||
class PerexCreator
|
||||
{
|
||||
/**
|
||||
* @var \DOMDocument
|
||||
*/
|
||||
protected $DOMDocument;
|
||||
protected $perex;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->DOMDocument = new \DOMDocument('1.0', 'UTF-8');
|
||||
$this->maxLength = 300;
|
||||
$this->maxImages = 2;
|
||||
$this->perex = '';
|
||||
}
|
||||
|
||||
private function addHtml($node)
|
||||
{
|
||||
$this->perex .= strip_tags($this->DOMDocument->saveHTML($node), '<p><strong><em><b><i><u>');
|
||||
}
|
||||
|
||||
public function create($html)
|
||||
{
|
||||
if (empty($html)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
|
||||
$this->DOMDocument->loadHTML($html);
|
||||
|
||||
$texts = $this->DOMDocument->getElementsByTagName('p');
|
||||
$images = $this->DOMDocument->getElementsByTagName('img');
|
||||
$iframes = $this->DOMDocument->getElementsByTagName('iframe');
|
||||
|
||||
$textId = 0;
|
||||
$imgId = 0;
|
||||
if ($texts->length > 1) {
|
||||
$this->addHtml($texts[$textId++]);
|
||||
}
|
||||
|
||||
if (mb_strlen($this->perex) < $this->maxLength) {
|
||||
if ($iframes->length > 0) {
|
||||
$this->perex .= $this->DOMDocument->saveHTML($iframes[0]);
|
||||
} elseif ($images->length > 0) {
|
||||
while ($imgId < $images->length && $imgId < $this->maxImages) {
|
||||
$this->perex .= $this->DOMDocument->saveHTML($images[$imgId++]);
|
||||
}
|
||||
} else {
|
||||
while (mb_strlen($this->perex) < $this->maxLength && $textId < $texts->length) {
|
||||
$this->addHtml($texts[$textId++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->perex;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ return [
|
||||
"directories" => "Podadresáře v adresáři :directory",
|
||||
"directoryDescription" => "Popis",
|
||||
"directoryHidden" => "Skrytý adresář",
|
||||
'directory' => 'Adresář',
|
||||
"directoryName" => "Jméno adresáře",
|
||||
"edit" => "Upravit soubor :name",
|
||||
"editButton" => "Upravit",
|
||||
@@ -39,4 +40,5 @@ return [
|
||||
"upload" => "Nahrát nový soubor",
|
||||
"uploadSuccessfull" => "Soubor úspěšně nahrán|:count soubory úspěšně nahrány|:count souborů úspěšně nahráno",
|
||||
"url" => "Veřejná URL",
|
||||
'rootDirectory' => 'Kořenový adresář'
|
||||
];
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<option value="{{$directory->id}}" @if($file->directory_id == $directory->id) selected @endif>
|
||||
{{str_repeat('-', $depth)}}
|
||||
{{$directory->is_root ? trans('admin/fileManager.rootDirectory') : $directory->name}}
|
||||
</option>
|
||||
|
||||
@if (!is_null($directory->subdirectories))
|
||||
@foreach($directory->subdirectories as $dir)
|
||||
@include('admin.fileManager.directories.subdirectoriesOptionList', ['directory' => $dir, 'depth' => $depth +1, 'file' => $file])
|
||||
@endforeach
|
||||
@endif
|
||||
@@ -1,4 +1,4 @@
|
||||
@if($directory->id)
|
||||
@if (!$directory->is_root)
|
||||
<div class="col-sm-4 col-md-3 text-center">
|
||||
@include('admin.snippets.card', [
|
||||
'body' => '<a href="'.route('upload.overview', ['directory' => $directory->parent_id]).'"><p><i class="fa fa-2x fa-level-up"></i><br />'.trans('admin/fileManager.parentDirectory').'</p></a>',
|
||||
|
||||
@@ -12,6 +12,15 @@
|
||||
</label>
|
||||
|
||||
<input class="form-control" type="text" name="keywords" id="keywords" value="{{$file->keywords}}" />
|
||||
|
||||
<lavel class="control-label">
|
||||
@lang('admin/fileManager.directory')
|
||||
</lavel>
|
||||
|
||||
<select name="directory_id" class="form-control">
|
||||
@include('admin/fileManager.directories.subdirectoriesOptionList', ['directory' => $root, 'depth' => 0, 'file' => $file])
|
||||
</select>
|
||||
|
||||
<label class="control-label">
|
||||
@lang('admin/fileManager.description')
|
||||
</label>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<input type="hidden" name="directoryId" value="{{$directory->id}}" />
|
||||
@endif
|
||||
|
||||
<input type="file" multiple name="files[]" class="form-control" id="file" />
|
||||
<input type="file" multiple name="files[]" required class="form-control" id="file" />
|
||||
|
||||
<input type="text" name="name" id="name" class="form-control" placeholder="@lang('admin/fileManager.name')"
|
||||
value="{{old('name')}}" />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
@extends('admin.layoutWithNavigation')
|
||||
|
||||
@if ($directory->id)
|
||||
@if (!$directory->is_root)
|
||||
@section('pageHeading', @trans('admin/fileManager.directories', ['directory' => $directory->name]))
|
||||
@else
|
||||
@section('pageHeading', @trans('admin/fileManager.rootDirectories'))
|
||||
@@ -31,7 +31,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if ($directory->exists)
|
||||
@if (!$directory->is_root)
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading" role="tab" id="edit-dir-title">
|
||||
<h5 class="panel-title">
|
||||
|
||||
Reference in New Issue
Block a user