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
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-28 07:46:45 +02:00
|
|
|
|
2019-05-23 07:08:31 +02:00
|
|
|
namespace App\Jobs\Util;
|
2019-04-28 07:46:45 +02:00
|
|
|
|
|
|
|
use App\Models\Document;
|
2020-01-29 01:12:52 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use Illuminate\Support\Str;
|
2019-12-27 01:28:36 +01:00
|
|
|
use Illuminate\Http\Request;
|
2020-01-29 01:12:52 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Intervention\Image\ImageManager;
|
2019-06-03 02:28:12 +02:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
2019-12-27 01:28:36 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-01-29 01:12:52 +01:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2019-04-28 07:46:45 +02:00
|
|
|
|
2019-06-03 02:28:12 +02:00
|
|
|
class UploadFile implements ShouldQueue
|
2019-04-28 07:46:45 +02:00
|
|
|
{
|
2020-01-29 01:12:52 +01:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
|
2019-04-28 07:46:45 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
const IMAGE = 1;
|
|
|
|
const DOCUMENT = 2;
|
2019-04-28 07:46:45 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
const PROPERTIES = [
|
|
|
|
self::IMAGE => [
|
2020-03-09 21:05:23 +01:00
|
|
|
'path' => 'images',
|
2020-01-29 01:12:52 +01:00
|
|
|
],
|
|
|
|
self::DOCUMENT => [
|
2020-03-09 21:05:23 +01:00
|
|
|
'path' => 'documents',
|
2020-01-29 01:12:52 +01:00
|
|
|
]
|
|
|
|
];
|
2019-04-28 07:46:45 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
protected $file;
|
2019-04-28 07:46:45 +02:00
|
|
|
protected $user;
|
|
|
|
protected $company;
|
2020-01-29 01:12:52 +01:00
|
|
|
protected $type;
|
2019-04-28 07:46:45 +02:00
|
|
|
|
2019-06-03 02:28:12 +02:00
|
|
|
public $entity;
|
2019-04-28 14:23:22 +02:00
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
public function __construct($file, $type, $user, $company, $entity, $disk = null)
|
2019-04-28 07:46:45 +02:00
|
|
|
{
|
2019-04-28 14:23:22 +02:00
|
|
|
$this->file = $file;
|
2020-01-29 01:12:52 +01:00
|
|
|
$this->type = $type;
|
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;
|
2020-01-29 01:12:52 +01:00
|
|
|
$this->disk = $disk ?? config('filesystems.default');
|
|
|
|
|
2020-04-10 07:07:36 +02:00
|
|
|
//MultiDB::setDB($this->company->db);
|
2019-04-28 07:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle() : ?Document
|
|
|
|
{
|
2020-03-09 21:05:23 +01:00
|
|
|
$path = self::PROPERTIES[$this->type]['path'];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->company) {
|
2020-03-09 21:05:23 +01:00
|
|
|
$path = sprintf('%s/%s', $this->company->company_key, self::PROPERTIES[$this->type]['path']);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-09 21:05:23 +01:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
$instance = Storage::disk($this->disk)->putFileAs(
|
2020-03-21 06:37:30 +01:00
|
|
|
$path,
|
|
|
|
$this->file,
|
|
|
|
$this->file->hashName()
|
2020-01-29 01:12:52 +01:00
|
|
|
);
|
2019-04-28 14:23:22 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
if (in_array($this->file->extension(), ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) {
|
|
|
|
$image_size = getimagesize($this->file);
|
2019-06-03 02:28:12 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
$width = $image_size[0];
|
|
|
|
$height = $image_size[1];
|
2019-04-28 14:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$document = new Document();
|
|
|
|
$document->user_id = $this->user->id;
|
|
|
|
$document->company_id = $this->company->id;
|
2020-01-29 01:12:52 +01:00
|
|
|
$document->path = $instance;
|
2019-04-28 14:23:22 +02:00
|
|
|
$document->name = $this->file->getClientOriginalName();
|
2020-01-29 01:12:52 +01:00
|
|
|
$document->type = $this->file->extension();
|
|
|
|
$document->disk = $this->disk;
|
2019-06-03 02:28:12 +02:00
|
|
|
$document->hash = $this->file->hashName();
|
2020-01-29 01:12:52 +01:00
|
|
|
$document->size = $this->file->getSize();
|
|
|
|
$document->width = isset($width) ?? null;
|
|
|
|
$document->height = isset($height) ?? null;
|
2019-04-28 14:23:22 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
// $preview_path = $this->encodePrimaryKey($this->company->id);
|
|
|
|
// $document->preview = $this->generatePreview($preview_path);
|
2019-04-28 14:23:22 +02:00
|
|
|
|
|
|
|
$this->entity->documents()->save($document);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $document;
|
2019-04-28 14:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function generatePreview($preview_path) : string
|
|
|
|
{
|
2019-06-03 07:31:20 +02:00
|
|
|
$extension = $this->file->getClientOriginalExtension();
|
|
|
|
|
|
|
|
if (empty(Document::$types[$extension]) && ! empty(Document::$extraExtensions[$extension])) {
|
|
|
|
$documentType = Document::$extraExtensions[$extension];
|
|
|
|
} else {
|
|
|
|
$documentType = $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty(Document::$types[$documentType])) {
|
|
|
|
return 'Unsupported file type';
|
|
|
|
}
|
2020-03-09 21:05:23 +01:00
|
|
|
|
2019-04-28 14:23:22 +02:00
|
|
|
$preview = '';
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (in_array($this->file->getClientOriginalExtension(), ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) {
|
2019-04-28 14:23:22 +02:00
|
|
|
$makePreview = false;
|
|
|
|
$imageSize = getimagesize($this->file);
|
|
|
|
$width = $imageSize[0];
|
|
|
|
$height = $imageSize[1];
|
|
|
|
$imgManagerConfig = [];
|
2019-12-30 22:59:12 +01:00
|
|
|
if (in_array($this->file->getClientOriginalExtension(), ['gif', 'bmp', 'tiff', 'psd'])) {
|
2019-04-28 14:23:22 +02:00
|
|
|
// Needs to be converted
|
|
|
|
$makePreview = true;
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif ($width > Document::DOCUMENT_PREVIEW_SIZE || $height > Document::DOCUMENT_PREVIEW_SIZE) {
|
2019-04-28 14:23:22 +02:00
|
|
|
$makePreview = true;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (in_array($documentType, ['bmp', 'tiff', 'psd'])) {
|
|
|
|
if (! class_exists('Imagick')) {
|
2019-04-28 14:23:22 +02:00
|
|
|
// Cant't read this
|
|
|
|
$makePreview = false;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-04-28 14:23:22 +02:00
|
|
|
$imgManagerConfig['driver'] = 'imagick';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($makePreview) {
|
2019-04-28 14:23:22 +02:00
|
|
|
// We haven't created a preview yet
|
|
|
|
$imgManager = new ImageManager($imgManagerConfig);
|
|
|
|
|
2019-06-03 07:31:20 +02:00
|
|
|
$img = $imgManager->make($preview_path);
|
2019-04-28 14:23:22 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
Storage::put($preview_path, $previewContent);
|
2019-04-28 14:23:22 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$preview = $preview_path;
|
|
|
|
}
|
2019-04-28 14:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $preview;
|
2019-04-28 07:46:45 +02:00
|
|
|
}
|
|
|
|
}
|