1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Models/Document.php

149 lines
3.2 KiB
PHP
Raw Normal View History

2019-04-28 07:31:32 +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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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:31:32 +02:00
namespace App\Models;
2021-11-09 17:30:17 +01:00
use App\Helpers\Document\WithTypeHelpers;
2021-01-27 11:38:28 +01:00
use App\Models\Filterable;
2020-02-27 00:32:44 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
2019-04-28 07:31:32 +02:00
class Document extends BaseModel
{
2020-02-27 00:32:44 +01:00
use SoftDeletes;
2021-01-27 11:38:28 +01:00
use Filterable;
2021-11-09 17:30:17 +01:00
use WithTypeHelpers;
2019-04-28 14:23:22 +02:00
const DOCUMENT_PREVIEW_SIZE = 300; // pixels
2019-04-28 12:25:18 +02:00
/**
* @var array
*/
protected $fillable = [
'is_default',
2020-08-19 08:11:57 +02:00
'is_public',
2019-04-28 12:25:18 +02:00
];
/**
2019-04-28 12:25:18 +02:00
* @var array
*/
public static $types = [
'png' => [
'mime' => 'image/png',
],
'ai' => [
'mime' => 'application/postscript',
],
'jpeg' => [
'mime' => 'image/jpeg',
],
2021-01-14 10:25:39 +01:00
'jpg' => [
'mime' => 'image/jpeg',
],
2019-04-28 12:25:18 +02:00
'tiff' => [
'mime' => 'image/tiff',
],
'pdf' => [
'mime' => 'application/pdf',
],
'gif' => [
'mime' => 'image/gif',
],
'psd' => [
'mime' => 'image/vnd.adobe.photoshop',
],
'txt' => [
'mime' => 'text/plain',
],
'doc' => [
'mime' => 'application/msword',
],
'xls' => [
'mime' => 'application/vnd.ms-excel',
],
'ppt' => [
'mime' => 'application/vnd.ms-powerpoint',
],
'xlsx' => [
'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
],
'docx' => [
'mime' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
'pptx' => [
'mime' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
],
];
2019-06-03 07:31:20 +02:00
/**
* @var array
*/
public static $extraExtensions = [
'jpg' => 'jpeg',
'tif' => 'tiff',
];
public function getEntityType()
{
return self::class;
}
2019-06-03 07:31:20 +02:00
2019-04-28 07:31:32 +02:00
public function documentable()
{
return $this->morphTo();
}
public function generateUrl($absolute = false)
{
2020-06-22 07:52:20 +02:00
$url = Storage::disk($this->disk)->url($this->url);
if ($url && $absolute) {
return url($url);
}
if ($url) {
return $url;
}
return null;
}
2020-06-24 10:59:56 +02:00
public function generateRoute($absolute = false)
{
return route('api.documents.show', ['document' => $this->hashed_id]).'/download';
2020-06-24 10:59:56 +02:00
}
2020-06-24 12:17:42 +02:00
public function deleteFile()
{
Storage::disk($this->disk)->delete($this->url);
}
2020-08-20 03:10:04 +02:00
public function filePath()
{
return Storage::disk($this->disk)->url($this->url);
}
2021-02-15 15:52:13 +01:00
public function diskPath(): string
{
return Storage::disk($this->disk)->path($this->url);
}
2022-03-14 23:30:19 +01:00
public function getFile()
{
2022-03-14 23:36:32 +01:00
return Storage::get($this->url);
2022-03-14 23:30:19 +01:00
}
2022-04-06 04:22:13 +02:00
public function translate_entity()
{
return ctrans('texts.document');
}
2019-04-28 07:31:32 +02:00
}