79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
class EmailAttachment extends Model
|
||
|
|
{
|
||
|
|
protected $table = 'email_attachments';
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'email_message_id', 'filename', 'mime_type', 'size', 'file_path',
|
||
|
|
'is_saved_to_documenti', 'documenti_id'
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'is_saved_to_documenti' => 'boolean',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function message(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(EmailMessage::class, 'email_message_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function documento(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Documento::class, 'documenti_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getUrlAttribute(): string
|
||
|
|
{
|
||
|
|
return route('email.attachment.download', $this->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function saveToDocumenti(): ?Documento
|
||
|
|
{
|
||
|
|
if ($this->is_saved_to_documenti || !$this->documenti_id) {
|
||
|
|
return $this->documento;
|
||
|
|
}
|
||
|
|
|
||
|
|
$documento = Documento::create([
|
||
|
|
'nome_file' => $this->filename,
|
||
|
|
'file_path' => $this->file_path,
|
||
|
|
'tipo' => 'upload',
|
||
|
|
'tipologia' => 'email_attachment',
|
||
|
|
'mime_type' => $this->mime_type,
|
||
|
|
'dimensione' => $this->size,
|
||
|
|
'note' => 'Allegato email: ' . ($this->message->subject ?? ''),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->update(['is_saved_to_documenti' => true, 'documenti_id' => $documento->id]);
|
||
|
|
|
||
|
|
return $documento;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getIconAttribute(): string
|
||
|
|
{
|
||
|
|
if (str_starts_with($this->mime_type, 'image/')) {
|
||
|
|
return 'fa-image';
|
||
|
|
}
|
||
|
|
if (str_starts_with($this->mime_type, 'video/')) {
|
||
|
|
return 'fa-video';
|
||
|
|
}
|
||
|
|
if (str_starts_with($this->mime_type, 'audio/')) {
|
||
|
|
return 'fa-music';
|
||
|
|
}
|
||
|
|
if (str_contains($this->mime_type, 'pdf')) {
|
||
|
|
return 'fa-file-pdf';
|
||
|
|
}
|
||
|
|
if (str_contains($this->mime_type, 'word')) {
|
||
|
|
return 'fa-file-word';
|
||
|
|
}
|
||
|
|
if (str_contains($this->mime_type, 'excel') || str_contains($this->mime_type, 'spreadsheet')) {
|
||
|
|
return 'fa-file-excel';
|
||
|
|
}
|
||
|
|
return 'fa-file';
|
||
|
|
}
|
||
|
|
}
|