Просмотр файла app/Models/File.php

Размер файла: 1.59Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. /**
  8. * Class File
  9. *
  10. * @property int $id
  11. * @property int $user_id
  12. * @property int $story_id
  13. * @property string $path
  14. * @property string $name
  15. * @property string $ext
  16. * @property int $size
  17. * @property int $created_at
  18. */
  19. class File extends Model
  20. {
  21. /**
  22. * Audio extensions
  23. */
  24. public const AUDIO = ['mp3'];
  25.  
  26. /**
  27. * Video extensions
  28. */
  29. public const VIDEO = ['mp4'];
  30.  
  31. /**
  32. * Image extensions
  33. */
  34. public const IMAGES = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'];
  35.  
  36. /**
  37. * Table name
  38. */
  39. protected string $table = 'files.csv';
  40.  
  41. /**
  42. * The attributes that should be cast.
  43. */
  44. protected array $casts = [
  45. 'size' => 'int',
  46. ];
  47.  
  48. /**
  49. * Является ли файл картинкой
  50. *
  51. * @return bool
  52. */
  53. public function isImage(): bool
  54. {
  55. return in_array($this->ext, self::IMAGES, true);
  56. }
  57.  
  58. /**
  59. * Является ли файл аудио
  60. *
  61. * @return bool
  62. */
  63. public function isAudio(): bool
  64. {
  65. return in_array($this->ext, self::AUDIO, true);
  66. }
  67.  
  68. /**
  69. * Является ли файл видео
  70. *
  71. * @return bool
  72. */
  73. public function isVideo(): bool
  74. {
  75. return in_array($this->ext, self::VIDEO, true);
  76. }
  77.  
  78. /**
  79. * Delete file
  80. *
  81. * @return int
  82. */
  83. public function delete(): int
  84. {
  85. if (file_exists(publicPath($this->path))) {
  86. unlink(publicPath($this->path));
  87. }
  88.  
  89. return parent::delete();
  90. }
  91. }