MediaFile.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace dokuwiki\File;
  3. use JpegMeta;
  4. class MediaFile
  5. {
  6. protected $id;
  7. protected $path;
  8. protected $mime;
  9. protected $ext;
  10. protected $downloadable;
  11. protected $width;
  12. protected $height;
  13. protected $meta;
  14. /**
  15. * MediaFile constructor.
  16. * @param string $id
  17. * @param string|int $rev optional revision
  18. */
  19. public function __construct($id, $rev = '')
  20. {
  21. $this->id = $id; //FIXME should it be cleaned?
  22. $this->path = mediaFN($id, $rev);
  23. list($this->ext, $this->mime, $this->downloadable) = mimetype($this->path, false);
  24. }
  25. /** @return string */
  26. public function getId()
  27. {
  28. return $this->id;
  29. }
  30. /** @return string */
  31. public function getPath()
  32. {
  33. return $this->path;
  34. }
  35. /**
  36. * The ID without namespace, used for display purposes
  37. *
  38. * @return string
  39. */
  40. public function getDisplayName()
  41. {
  42. return noNS($this->id);
  43. }
  44. /** @return string */
  45. public function getMime()
  46. {
  47. if (!$this->mime) return 'application/octet-stream';
  48. return $this->mime;
  49. }
  50. /** @return string */
  51. public function getExtension()
  52. {
  53. return (string)$this->ext;
  54. }
  55. /**
  56. * Similar to the extesion but does some clean up
  57. *
  58. * @return string
  59. */
  60. public function getIcoClass()
  61. {
  62. $ext = $this->getExtension();
  63. if ($ext === '') $ext = 'file';
  64. return preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
  65. }
  66. /**
  67. * Should this file be downloaded instead being displayed inline?
  68. *
  69. * @return bool
  70. */
  71. public function isDownloadable()
  72. {
  73. return $this->downloadable;
  74. }
  75. /** @return int */
  76. public function getFileSize()
  77. {
  78. return filesize($this->path);
  79. }
  80. /** @return int */
  81. public function getLastModified()
  82. {
  83. return filemtime($this->path);
  84. }
  85. /** @return bool */
  86. public function isWritable()
  87. {
  88. return is_writable($this->path);
  89. }
  90. /** @return bool */
  91. public function isImage()
  92. {
  93. return (substr($this->mime, 0, 6) === 'image/');
  94. }
  95. /**
  96. * initializes width and height for images when requested
  97. */
  98. protected function initSizes()
  99. {
  100. $this->width = 0;
  101. $this->height = 0;
  102. if (!$this->isImage()) return;
  103. $info = getimagesize($this->path);
  104. if ($info === false) return;
  105. list($this->width, $this->height) = $info;
  106. }
  107. /**
  108. * Returns the width if this is a supported image, 0 otherwise
  109. *
  110. * @return int
  111. */
  112. public function getWidth()
  113. {
  114. if ($this->width === null) $this->initSizes();
  115. return $this->width;
  116. }
  117. /**
  118. * Returns the height if this is a supported image, 0 otherwise
  119. *
  120. * @return int
  121. */
  122. public function getHeight()
  123. {
  124. if ($this->height === null) $this->initSizes();
  125. return $this->height;
  126. }
  127. /**
  128. * Returns the permissions the current user has on the file
  129. *
  130. * @todo doing this for each file within a namespace is a waste, we need to cache this somehow
  131. * @return int
  132. */
  133. public function userPermission()
  134. {
  135. return auth_quickaclcheck(getNS($this->id).':*');
  136. }
  137. /** @return JpegMeta */
  138. public function getMeta()
  139. {
  140. if($this->meta === null) $this->meta = new JpegMeta($this->path);
  141. return $this->meta;
  142. }
  143. }