1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Jobs/Util/UploadFile.php

180 lines
5.7 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).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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\Libraries\MultiDB;
use App\Models\Document;
use App\Utils\Traits\MakesHash;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
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
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
2019-04-28 07:46:45 +02:00
const IMAGE = 1;
const DOCUMENT = 2;
2019-04-28 07:46:45 +02:00
const PROPERTIES = [
self::IMAGE => [
'path' => 'images',
],
self::DOCUMENT => [
'path' => 'documents',
],
];
2019-04-28 07:46:45 +02:00
protected $file;
2019-04-28 07:46:45 +02:00
protected $user;
protected $company;
protected $type;
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
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;
$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;
$this->disk = $disk ?? config('filesystems.default');
2020-08-18 16:12:46 +02:00
$this->is_public = $is_public;
//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
{
if(is_array($this->file)) //return early if the payload is just JSON
return null;
$path = self::PROPERTIES[$this->type]['path'];
if ($this->company) {
$path = sprintf('%s/%s', $this->company->company_key, self::PROPERTIES[$this->type]['path']);
}
$instance = Storage::disk($this->disk)->putFileAs(
$path,
$this->file,
2021-02-15 11:52:50 +01:00
$this->file->hashName()
);
2019-04-28 14:23:22 +02: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
$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();
$document->type = $this->file->extension();
$document->disk = $this->disk;
2019-06-03 02:28:12 +02:00
$document->hash = $this->file->hashName();
$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
// $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);
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';
}
2019-04-28 14:23:22 +02:00
$preview = '';
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 = [];
if (in_array($this->file->getClientOriginalExtension(), ['gif', 'bmp', 'tiff', 'psd'])) {
2019-04-28 14:23:22 +02:00
// Needs to be converted
$makePreview = true;
} elseif ($width > Document::DOCUMENT_PREVIEW_SIZE || $height > Document::DOCUMENT_PREVIEW_SIZE) {
2019-04-28 14:23:22 +02:00
$makePreview = true;
}
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;
} else {
2019-04-28 14:23:22 +02:00
$imgManagerConfig['driver'] = 'imagick';
}
}
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());
Storage::put($preview_path, $previewContent);
2019-04-28 14:23:22 +02:00
$preview = $preview_path;
}
2019-04-28 14:23:22 +02:00
}
return $preview;
2019-04-28 07:46:45 +02:00
}
}