FIX: fix file manager
This commit is contained in:
@@ -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