You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

175 lines
3.6 KiB

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