MediaItem.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * HassCMS (http://www.hassium.org/)
  4. *
  5. * @link http://github.com/hasscms for the canonical source repository
  6. * @copyright Copyright (c) 2016-2099 Hassium Software LLC.
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. */
  9. namespace backend\models;
  10. use common\models\Attachment;
  11. /**
  12. *
  13. * @package hass\package_name
  14. * @author zhepama <zhepama@gmail.com>
  15. * @since 0.1.0
  16. *
  17. */
  18. class MediaItem
  19. {
  20. const TYPE_FILE = 'file';
  21. const TYPE_FOLDER = 'folder';
  22. const FILE_TYPE_IMAGE = 'image';
  23. const FILE_TYPE_VIDEO = 'video';
  24. const FILE_TYPE_AUDIO = 'audio';
  25. const FILE_TYPE_DOCUMENT = 'document';
  26. /**
  27. * @var string Specifies the item path relative to the Library root.
  28. */
  29. public $path;
  30. /**
  31. * @var integer Specifies the item size.
  32. * For files the item size is measured in bytes. For folders it
  33. * contains the number of files in the folder.
  34. */
  35. public $size;
  36. /**
  37. * @var integer Contains the last modification time (Unix timestamp).
  38. */
  39. public $lastModified;
  40. /**
  41. * @var string Specifies the item type.
  42. */
  43. public $type;
  44. /**
  45. * @var string Specifies the public URL of the item.
  46. */
  47. public $publicUrl;
  48. /**
  49. * @var array Contains a default list of files and directories to ignore.
  50. * The list can be customized with the following configuration options:
  51. * - cms.storage.media.image_extensions
  52. * - cms.storage.media.video_extensions
  53. * - cms.storage.media.audo_extensions
  54. */
  55. protected static $defaultTypeExtensions = [
  56. 'image' => ['gif', 'png', 'jpg', 'jpeg', 'bmp'],
  57. 'video' => ['mp4', 'avi', 'mov', 'mpg', 'mpeg', 'mkv', 'webm'],
  58. 'audio' => ['mp3', 'wav', 'wma', 'm4a', 'ogg']
  59. ];
  60. protected static $imageExtensions;
  61. protected static $videoExtensions;
  62. protected static $audioExtensions;
  63. public function __construct($path, $size, $lastModified, $type, $publicUrl)
  64. {
  65. $this->path = $path;
  66. $this->size = $size;
  67. $this->lastModified = $lastModified;
  68. $this->type = $type;
  69. $this->publicUrl = $publicUrl;
  70. }
  71. /**
  72. *
  73. * @param Attachment $attachment
  74. */
  75. public static function createFromAttachment($attachment)
  76. {
  77. $item = new static($attachment->path,$attachment->size, $attachment->updated_at, 'file', $attachment->url);
  78. return $item;
  79. }
  80. public function isFile()
  81. {
  82. return $this->type == self::TYPE_FILE;
  83. }
  84. public function isImage()
  85. {
  86. return $this->getFileType() === self::FILE_TYPE_IMAGE;
  87. }
  88. /**
  89. * Returns the file type by its name.
  90. * The known file types are: image, video, audio, document
  91. * @return string Returns the file type or NULL if the item is a folder.
  92. */
  93. public function getFileType()
  94. {
  95. if (!$this->isFile()) {
  96. return null;
  97. }
  98. if (!self::$imageExtensions) {
  99. self::$imageExtensions = \Yii::$app->get("config")->get('attachment.image_extensions', self::$defaultTypeExtensions['image']);
  100. self::$videoExtensions = \Yii::$app->get("config")->get('attachment.video_extensions', self::$defaultTypeExtensions['video']);
  101. self::$audioExtensions = \Yii::$app->get("config")->get('attachment.audio_extensions', self::$defaultTypeExtensions['audio']);
  102. }
  103. $extension = pathinfo($this->path, PATHINFO_EXTENSION);
  104. if (!strlen($extension)) {
  105. return self::FILE_TYPE_DOCUMENT;
  106. }
  107. if (in_array($extension, self::$imageExtensions)) {
  108. return self::FILE_TYPE_IMAGE;
  109. }
  110. if (in_array($extension, self::$videoExtensions)) {
  111. return self::FILE_TYPE_VIDEO;
  112. }
  113. if (in_array($extension, self::$audioExtensions)) {
  114. return self::FILE_TYPE_AUDIO;
  115. }
  116. return self::FILE_TYPE_DOCUMENT;
  117. }
  118. /**
  119. * Returns the item size as string.
  120. * For file-type items the size is the number of bytes. For folder-type items
  121. * the size is the number of items contained by the item.
  122. * @return string Returns the size as string.
  123. */
  124. public function sizeToString()
  125. {
  126. return \Yii::$app->getFormatter()->asShortSize($this->size);;
  127. }
  128. public function getResolution()
  129. {
  130. $size = getimagesize($this->path);
  131. return $size[0] . "x" . $size[1];
  132. }
  133. /**
  134. * Returns the item last modification date as string.
  135. * @return string Returns the item's last modification date as string.
  136. */
  137. public function lastModifiedAsString()
  138. {
  139. if (!$this->lastModified) {
  140. return null;
  141. }
  142. return \Yii::$app->getFormatter()->asDatetime($this->lastModified);
  143. }
  144. }