1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Jobs/Util/UploadFile.php

157 lines
4.5 KiB
PHP
Raw Normal View History

2019-04-28 07:46:45 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2019-04-28 07:46:45 +02:00
2019-04-28 14:23:22 +02:00
namespace Jobs\Util;
2019-04-28 07:46:45 +02:00
use App\Models\Document;
2019-04-28 14:23:22 +02:00
use App\Utils\Traits\MakesHash;
2019-04-28 07:46:45 +02:00
use Illuminate\Http\Request;
2019-04-28 14:23:22 +02:00
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManager;
2019-04-28 07:46:45 +02:00
class UploadFile
{
use MakesHash;
2019-04-28 14:23:22 +02:00
protected $file;
2019-04-28 07:46:45 +02:00
protected $user;
protected $company;
2019-04-28 14:23:22 +02:00
$entity;
2019-04-28 07:46:45 +02:00
/**
* Create a new job instance.
*
* @return void
*/
2019-04-28 14:23:22 +02:00
public function __construct($file, $user, $company, $entity)
2019-04-28 07:46:45 +02:00
{
2019-04-28 14:23:22 +02:00
$this->file = $file;
2019-04-28 07:46:45 +02:00
$this->user = $user;
$this->company = $company;
2019-04-28 14:23:22 +02:00
$this->entity = $entity;
2019-04-28 07:46:45 +02:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : ?Document
{
2019-04-28 14:23:22 +02:00
$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;
2019-04-28 07:46:45 +02:00
}
2019-04-28 14:23:22 +02:00
2019-04-28 07:46:45 +02:00
}