FEATURE: move bussiness logic out of models
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\FileManager\ModelTraits;
|
||||
|
||||
use App\Events\UploadFile;
|
||||
use App\Http\Requests\Request;
|
||||
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;
|
||||
|
||||
trait File
|
||||
{
|
||||
/**
|
||||
* Getter for filename of uploaded file.
|
||||
*
|
||||
* This filename is used for localy stored file.
|
||||
*
|
||||
* @param UploadedFile $file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFilename(UploadedFile $file)
|
||||
{
|
||||
return uniqid() . '.' . $file->getClientOriginalExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves file into FS and creates new record in database
|
||||
*
|
||||
* @param UploadedFile $file
|
||||
* @param Request $request
|
||||
*
|
||||
* @return array associative array - key 'file' is type of SymphonyFile and contains info about real file, key 'id'
|
||||
* is ID of row in files table
|
||||
*/
|
||||
public function upload(UploadedFile $file, Request $request)
|
||||
{
|
||||
$destination = config('app.upload_path');
|
||||
$filename = $this->getFilename($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, $request, $file->getClientOriginalName());
|
||||
|
||||
// fire event about new file
|
||||
Event::fire(new UploadFile($uploadedFile, $id));
|
||||
|
||||
return [
|
||||
'filename' => $filename,
|
||||
'file' => $file,
|
||||
'id' => $id
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save file into database
|
||||
*
|
||||
* This file saves new record into database. It also calculates hash of given file, obtains MIME type etc.
|
||||
*
|
||||
* @param SymphonyFile $file
|
||||
* @param Request $request
|
||||
* @param $originalFilename
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function saveIntoDatabase(SymphonyFile $file, Request $request, $originalFilename)
|
||||
{
|
||||
// get path and hash of uploaded file
|
||||
$path = config('app.upload_path') . DIRECTORY_SEPARATOR . $file->getFilename();
|
||||
$hash = hash_file(FileModel::HASH_ALGORITHM, $path);
|
||||
|
||||
// define mandatory fields
|
||||
$newFile = FileModel::create([
|
||||
'user_id' => Auth::user()->id,
|
||||
'filename' => $file->getFilename(),
|
||||
'original_filename' => $originalFilename,
|
||||
'file_hash' => $hash,
|
||||
'hash_algorithm' => FileModel::HASH_ALGORITHM,
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'file_size' => $file->getSize(),
|
||||
'name' => $request->name,
|
||||
'description' => $request->description,
|
||||
'directory_id' => $request->directoryId,
|
||||
'keywords' => $request->keywords
|
||||
]);
|
||||
|
||||
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