1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Jobs/Util/UploadFile.php

176 lines
5.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) 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;
use App\Libraries\MultiDB;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
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;
use Illuminate\Support\Facades\Storage;
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
{
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;
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
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;
$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');
//MultiDB::setDB($this->company->db);
2019-04-28 07:46:45 +02:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : ?Document
{
$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,
$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;
$document->path = $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();
$document->width = isset($width) ?? null;
$document->height = isset($height) ?? null;
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
}
}