EmailMessageAttachment.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Storage;
  5. class EmailMessageAttachment extends Model
  6. {
  7. protected $fillable = ['email_message_id', 'disk', 'path', 'name', 'size'];
  8. protected $appends = ['public_url', 'is_image', 'size_human'];
  9. public function message()
  10. {
  11. return $this->belongsTo(EmailMessage::class);
  12. }
  13. public function getPublicUrlAttribute(): ?string
  14. {
  15. if (!$this->disk || !$this->path) return null;
  16. return Storage::disk($this->disk)->url($this->path);
  17. }
  18. public function getIsImageAttribute(): bool
  19. {
  20. $ext = strtolower(pathinfo($this->name ?: $this->path, PATHINFO_EXTENSION));
  21. return in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg']);
  22. }
  23. public function getSizeHumanAttribute(): ?string
  24. {
  25. if (!$this->size) return null;
  26. $kb = $this->size / 1024;
  27. return $kb >= 1024
  28. ? number_format($kb / 1024, 2) . ' MB'
  29. : number_format($kb, 1) . ' KB';
  30. }
  31. }