*/ protected $fillable = [ 'is_default', 'is_public', 'name', ]; /** * @var array */ protected $casts = [ 'is_public' => 'bool', 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', ]; /** * @var array */ public static $types = [ 'png' => [ 'mime' => 'image/png', ], 'ai' => [ 'mime' => 'application/postscript', ], 'jpeg' => [ 'mime' => 'image/jpeg', ], 'jpg' => [ 'mime' => 'image/jpeg', ], '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', ], ]; /** * @var array */ public static $extraExtensions = [ 'jpg' => 'jpeg', 'tif' => 'tiff', ]; public function getEntityType() { return self::class; } public function documentable() { return $this->morphTo(); } public function user() { return $this->belongsTo(User::class)->withTrashed(); } public function generateUrl($absolute = false) { $url = Storage::disk($this->disk)->url($this->url); if ($url && $absolute) { return url($url); } if ($url) { return $url; } return null; } public function generateRoute($absolute = false) { try { return route('api.documents.show', ['document' => $this->hashed_id]).'/download'; } catch(\Exception $e) { nlog("Exception:: Document::" . $e->getMessage()); return ''; } } public function deleteFile() { Storage::disk($this->disk)->delete($this->url); } public function filePath() { return Storage::disk($this->disk)->url($this->url); } public function diskPath(): string { return Storage::disk($this->disk)->path($this->url); } public function getFile() { return Storage::get($this->url); } public function translate_entity() { return ctrans('texts.document'); } public function link() { $entity_id = $this->encodePrimaryKey($this->documentable_id); $link = ''; match($this->documentable_type) { 'App\Models\Vendor' => $link = "/vendors/{$entity_id}", 'App\Models\Project' => $link = "/projects/{$entity_id}", 'invoices' => $link = "/invoices/{$entity_id}/edit", 'App\Models\Quote' => $link = "/quotes/{$entity_id}/edit", 'App\Models\Credit' => $link = "/credits/{$entity_id}/edit", 'App\Models\Expense' => $link = "/expenses/{$entity_id}/edit", 'App\Models\Payment' => $link = "/payments/{$entity_id}/edit", 'App\Models\Task' => $link = "/tasks/{$entity_id}/edit", 'App\Models\Client' => $link = "/clients/{$entity_id}", 'App\Models\RecurringExpense' => $link = "/recurring_expenses/{$entity_id}/edit", 'App\Models\RecurringInvoice' => $link = "/recurring_invoices/{$entity_id}/edit", default => $link = '', }; return $link; } public function compress(): mixed { $image = $this->getFile(); $catch_image = $image; if(!extension_loaded('imagick')) { return $catch_image; } try { $file = base64_encode($image); $img = new \Imagick(); $img->readImageBlob($file); $img->setImageCompression(true); $img->setImageCompressionQuality(40); return $img->getImageBlob(); } catch(\Exception $e) { nlog("Exception:: Document::" . $e->getMessage()); nlog($e->getMessage()); return $catch_image; } } /** * Returns boolean based on checks for image. * * @return bool */ public function isImage(): bool { if (in_array($this->type, ['png', 'jpeg', 'jpg', 'tiff', 'gif'])) { return true; } return false; } }