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