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