2019-04-28 07:46:45 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
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
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2020-09-06 11:38:10 +02:00
|
|
|
use App\Models\Document;
|
2020-01-29 01:12:52 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-09-06 11:38:10 +02:00
|
|
|
use Illuminate\Bus\Queueable;
|
2020-01-29 01:12:52 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2020-09-06 11:38:10 +02:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Intervention\Image\ImageManager;
|
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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
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-09-06 11:38:10 +02:00
|
|
|
],
|
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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-04-28 07:46:45 +02:00
|
|
|
protected $user;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-04-28 07:46:45 +02:00
|
|
|
protected $company;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-01-29 01:12:52 +01:00
|
|
|
protected $type;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-08-18 16:12:46 +02:00
|
|
|
protected $is_public;
|
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
|
|
|
|
2023-08-08 10:56:31 +02:00
|
|
|
public $disk;
|
|
|
|
|
2020-08-18 16:12:46 +02:00
|
|
|
public function __construct($file, $type, $user, $company, $entity, $disk = null, $is_public = false)
|
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-08-18 16:12:46 +02:00
|
|
|
$this->is_public = $is_public;
|
2020-01-29 01:12:52 +01:00
|
|
|
|
2020-04-10 07:07:36 +02:00
|
|
|
//MultiDB::setDB($this->company->db);
|
2019-04-28 07:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Document|null
|
2019-04-28 07:46:45 +02:00
|
|
|
*/
|
|
|
|
public function handle() : ?Document
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (is_array($this->file)) { //return early if the payload is just JSON
|
2021-01-13 21:33:45 +01:00
|
|
|
return null;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-01-13 21:33:45 +01:00
|
|
|
|
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,
|
2022-06-21 11:57:17 +02:00
|
|
|
$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-06-22 07:52:20 +02:00
|
|
|
$document->url = $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();
|
2020-06-22 13:32:10 +02:00
|
|
|
$document->width = isset($width) ? $width : null;
|
|
|
|
$document->height = isset($height) ? $height : null;
|
2020-08-18 16:12:46 +02:00
|
|
|
$document->is_public = $this->is_public;
|
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;
|
2022-10-23 23:42:38 +02:00
|
|
|
$previewWidth = $width * Document::DOCUMENT_PREVIEW_SIZE / $height;
|
2019-04-28 14:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|