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

Размер файла: 1.58Kb
  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. * The attributes that should be cast.
  38. */
  39. protected array $casts = [
  40. 'size' => 'int',
  41. ];
  42.  
  43. protected string $filePath = __DIR__ . '/../../database/files.csv';
  44.  
  45. /**
  46. * Является ли файл картинкой
  47. *
  48. * @return bool
  49. */
  50. public function isImage(): bool
  51. {
  52. return in_array($this->ext, self::IMAGES, true);
  53. }
  54.  
  55. /**
  56. * Является ли файл аудио
  57. *
  58. * @return bool
  59. */
  60. public function isAudio(): bool
  61. {
  62. return in_array($this->ext, self::AUDIO, true);
  63. }
  64.  
  65. /**
  66. * Является ли файл видео
  67. *
  68. * @return bool
  69. */
  70. public function isVideo(): bool
  71. {
  72. return in_array($this->ext, self::VIDEO, true);
  73. }
  74.  
  75. /**
  76. * Delete file
  77. *
  78. * @return int
  79. */
  80. public function delete(): int
  81. {
  82. if (file_exists(publicPath($this->path))) {
  83. unlink(publicPath($this->path));
  84. }
  85.  
  86. return parent::delete();
  87. }
  88. }