98 lines
2.0 KiB
PHP
98 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class Document extends Model
|
|
{
|
|
public static $rules = [
|
|
'file' => 'required|mimes:png,gif,jpeg,jpg,pdf,doc,docx,xls,xlsx'
|
|
];
|
|
|
|
public static $messages = [
|
|
'file.mimes' => 'Uploaded file is not in image format',
|
|
'file.required' => 'Document is required'
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->hasOne('user');
|
|
}
|
|
|
|
|
|
/******************************/
|
|
/* Dateispezifische Attribute */
|
|
/******************************/
|
|
public function getFiletypeAttribute()
|
|
{
|
|
switch($this->mime_type)
|
|
{
|
|
case 'image/gif':
|
|
case 'image/jpeg':
|
|
case 'image/png':
|
|
return 'image';
|
|
break;
|
|
|
|
case 'application/pdf':
|
|
return 'pdf';
|
|
break;
|
|
|
|
case 'application/msword':
|
|
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
|
|
return 'word';
|
|
break;
|
|
|
|
case 'application/msexcel':
|
|
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
|
|
return 'excel';
|
|
break;
|
|
|
|
default:
|
|
return $this->mime_type;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getPathAttribute()
|
|
{
|
|
return '/uploads/'.$this->model.'/'.$this->model_id.'/'.$this->filename;
|
|
}
|
|
|
|
public function getPreviewpathAttribute()
|
|
{
|
|
switch($this->mime_type)
|
|
{
|
|
case 'application/pdf':
|
|
return '/icons/pdf_256x256.png';
|
|
break;
|
|
|
|
case 'application/msword':
|
|
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
|
|
return '/icons/word_256x256.png';
|
|
break;
|
|
|
|
case 'application/msexcel':
|
|
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
|
|
return '/icons/excel_256x256.png';
|
|
break;
|
|
|
|
default:
|
|
return '/uploads/'.$this->model.'/'.$this->model_id.'/thumbs/'.$this->filename;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getFilesizeAttribute()
|
|
{
|
|
$storage = Storage::disk('upload');
|
|
$filepath = $this->getPathAttribute();
|
|
if($storage->exists($filepath))
|
|
{
|
|
return $storage->size($filepath);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
} |