mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
prototype document upload class
This commit is contained in:
parent
a54fd4b931
commit
cc4032ef3a
@ -18,6 +18,13 @@ class StoreInvoiceRequest extends Request
|
||||
return auth()->user()->can('create', Invoice::class);
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'documents' => 'mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function sanitize()
|
||||
{
|
||||
@ -30,4 +37,5 @@ class StoreInvoiceRequest extends Request
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,4 +22,11 @@ class UpdateInvoiceRequest extends Request
|
||||
}
|
||||
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'documents' => 'mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -1,32 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Utils;
|
||||
namespace Jobs\Util;
|
||||
|
||||
use App\Models\Document;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Intervention\Image\ImageManager;
|
||||
|
||||
class UploadFile
|
||||
{
|
||||
|
||||
use MakesHash;
|
||||
|
||||
protected $request;
|
||||
protected $file;
|
||||
|
||||
protected $user;
|
||||
|
||||
protected $company;
|
||||
|
||||
$entity;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
||||
public function __construct(array $request, $user, $company)
|
||||
public function __construct($file, $user, $company, $entity)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->file = $file;
|
||||
$this->user = $user;
|
||||
$this->company = $company;
|
||||
$this->entity = $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,6 +42,106 @@ class UploadFile
|
||||
*/
|
||||
public function handle() : ?Document
|
||||
{
|
||||
|
||||
|
||||
$path = $this->encodePrimaryKey($this->company->id) . '/' . microtime() . '_' . str_replace(" ", "", $this->file->getClientOriginalName());
|
||||
|
||||
Storage::put($path, $this->file);
|
||||
|
||||
$width = 0;
|
||||
$height = 0;
|
||||
|
||||
if (in_array($this->file->getClientOriginalExtension(), ['jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd']))
|
||||
{
|
||||
$imageSize = getimagesize($this->file);
|
||||
$width = $imageSize[0];
|
||||
$height = $imageSize[1];
|
||||
}
|
||||
|
||||
$document = new Document();
|
||||
$document->user_id = $this->user->id;
|
||||
$document->company_id = $this->company->id;
|
||||
$document->path = $path;
|
||||
$document->name = $this->file->getClientOriginalName();
|
||||
$document->type = $this->file->getClientOriginalExtension();
|
||||
$document->disk = config('filesystems.default');
|
||||
$document->hash = $this->createHash();
|
||||
$document->size = filesize($filePath);
|
||||
$document->width = $width;
|
||||
$document->height = $height;
|
||||
|
||||
$preview_path = $this->encodePrimaryKey($this->company->id) . '/' . microtime() . '_preview_' . str_replace(" ", "", $this->file->getClientOriginalName());
|
||||
|
||||
$document->preview = $this->generatePreview($preview_path);
|
||||
|
||||
$this->entity->documents()->save($document);
|
||||
|
||||
}
|
||||
|
||||
private function generatePreview($preview_path) : string
|
||||
{
|
||||
$preview = '';
|
||||
|
||||
if (in_array($this->file->getClientOriginalExtension(), ['jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd']))
|
||||
{
|
||||
$makePreview = false;
|
||||
$imageSize = getimagesize($this->file);
|
||||
$width = $imageSize[0];
|
||||
$height = $imageSize[1];
|
||||
$imgManagerConfig = [];
|
||||
if (in_array($this->file->getClientOriginalExtension(), ['gif', 'bmp', 'tiff', 'psd']))
|
||||
{
|
||||
// Needs to be converted
|
||||
$makePreview = true;
|
||||
} elseif ($width > Document::DOCUMENT_PREVIEW_SIZE || $height > Document::DOCUMENT_PREVIEW_SIZE)
|
||||
{
|
||||
$makePreview = true;
|
||||
}
|
||||
|
||||
if (in_array($documentType, ['bmp', 'tiff', 'psd']))
|
||||
{
|
||||
if (! class_exists('Imagick'))
|
||||
{
|
||||
// Cant't read this
|
||||
$makePreview = false;
|
||||
} else
|
||||
{
|
||||
$imgManagerConfig['driver'] = 'imagick';
|
||||
}
|
||||
}
|
||||
|
||||
if ($makePreview)
|
||||
{
|
||||
// We haven't created a preview yet
|
||||
$imgManager = new ImageManager($imgManagerConfig);
|
||||
|
||||
$img = $imgManager->make($filePath);
|
||||
|
||||
if ($width <= Document::DOCUMENT_PREVIEW_SIZE && $height <= Document::DOCUMENT_PREVIEW_SIZE) {
|
||||
$previewWidth = $width;
|
||||
$previewHeight = $height;
|
||||
} elseif ($width > $height) {
|
||||
$previewWidth = Document::DOCUMENT_PREVIEW_SIZE;
|
||||
$previewHeight = $height * Document::DOCUMENT_PREVIEW_SIZE / $width;
|
||||
} else {
|
||||
$previewHeight = Document::DOCUMENT_PREVIEW_SIZE;
|
||||
$previewWidth = $width * DOCUMENT_PREVIEW_SIZE / $height;
|
||||
}
|
||||
|
||||
$img->resize($previewWidth, $previewHeight);
|
||||
|
||||
$previewContent = (string) $img->encode($this->file->getClientOriginalExtension());
|
||||
|
||||
Storage::put($preview_path, $previewContent);
|
||||
|
||||
$preview = $preview_path;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $preview;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Document extends BaseModel
|
||||
{
|
||||
const DOCUMENT_PREVIEW_SIZE = 300; // pixels
|
||||
|
||||
/**
|
||||
* @var array
|
||||
|
@ -23,6 +23,7 @@
|
||||
"davejamesmiller/laravel-breadcrumbs": "5.x",
|
||||
"fideloper/proxy": "^4.0",
|
||||
"hashids/hashids": "^3.0",
|
||||
"intervention/image": "^2.4",
|
||||
"laracasts/presenter": "^0.2.1",
|
||||
"laravel/framework": "5.8.*",
|
||||
"laravel/socialite": "^3.1",
|
||||
|
Loading…
x
Reference in New Issue
Block a user