MWFileProps.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * MimeMagic helper functions for detecting and dealing with MIME types.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * MimeMagic helper wrapper
  24. *
  25. * @since 1.28
  26. */
  27. class MWFileProps {
  28. /** @var MimeAnalyzer */
  29. private $magic;
  30. /**
  31. * @param MimeAnalyzer $magic
  32. */
  33. public function __construct( MimeAnalyzer $magic ) {
  34. $this->magic = $magic;
  35. }
  36. /**
  37. * Get an associative array containing information about
  38. * a file with the given storage path.
  39. *
  40. * Resulting array fields include:
  41. * - fileExists
  42. * - size (filesize in bytes)
  43. * - mime (as major/minor)
  44. * - media_type (value to be used with the MEDIATYPE_xxx constants)
  45. * - metadata (handler specific)
  46. * - sha1 (in base 36)
  47. * - width
  48. * - height
  49. * - bits (bitrate)
  50. * - file-mime
  51. * - major_mime
  52. * - minor_mime
  53. *
  54. * @param string $path Filesystem path to a file
  55. * @param string|bool $ext The file extension, or true to extract it from the filename.
  56. * Set it to false to ignore the extension.
  57. * @return array
  58. * @since 1.28
  59. */
  60. public function getPropsFromPath( $path, $ext ) {
  61. $fsFile = new FSFile( $path );
  62. $info = $this->newPlaceholderProps();
  63. $info['fileExists'] = $fsFile->exists();
  64. if ( $info['fileExists'] ) {
  65. $info['size'] = $fsFile->getSize(); // bytes
  66. $info['sha1'] = $fsFile->getSha1Base36();
  67. # MIME type according to file contents
  68. $info['file-mime'] = $this->magic->guessMimeType( $path, false );
  69. # Logical MIME type
  70. $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : $ext;
  71. $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
  72. list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
  73. $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
  74. # Height, width and metadata
  75. $handler = MediaHandler::getHandler( $info['mime'] );
  76. if ( $handler ) {
  77. $info['metadata'] = $handler->getMetadata( $fsFile, $path );
  78. /** @noinspection PhpMethodParametersCountMismatchInspection */
  79. $gis = $handler->getImageSize( $fsFile, $path, $info['metadata'] );
  80. if ( is_array( $gis ) ) {
  81. $info = $this->extractImageSizeInfo( $gis ) + $info;
  82. }
  83. }
  84. }
  85. return $info;
  86. }
  87. /**
  88. * Exract image size information
  89. *
  90. * @param array $gis
  91. * @return array
  92. */
  93. private function extractImageSizeInfo( array $gis ) {
  94. $info = [];
  95. # NOTE: $gis[2] contains a code for the image type. This is no longer used.
  96. $info['width'] = $gis[0];
  97. $info['height'] = $gis[1];
  98. if ( isset( $gis['bits'] ) ) {
  99. $info['bits'] = $gis['bits'];
  100. } else {
  101. $info['bits'] = 0;
  102. }
  103. return $info;
  104. }
  105. /**
  106. * Empty place holder props for non-existing files
  107. *
  108. * Resulting array fields include:
  109. * - fileExists
  110. * - size (filesize in bytes)
  111. * - mime (as major/minor)
  112. * - media_type (value to be used with the MEDIATYPE_xxx constants)
  113. * - metadata (handler specific)
  114. * - sha1 (in base 36)
  115. * - width
  116. * - height
  117. * - bits (bitrate)
  118. * - file-mime
  119. * - major_mime
  120. * - minor_mime
  121. *
  122. * @return array
  123. * @since 1.28
  124. */
  125. public function newPlaceholderProps() {
  126. return FSFile::placeholderProps() + [
  127. 'metadata' => '',
  128. 'width' => 0,
  129. 'height' => 0,
  130. 'bits' => 0,
  131. 'media_type' => MEDIATYPE_UNKNOWN
  132. ];
  133. }
  134. }