| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace Wikimedia\FileBackend\FSFile; |
| 25 |
|
| 26 |
use Wikimedia\AtEase\AtEase; |
| 27 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class FSFile { |
| 35 |
|
| 36 |
protected $path; |
| 37 |
|
| 38 |
|
| 39 |
protected $sha1Base36; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( $path ) { |
| 47 |
$this->path = $path; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function getPath() { |
| 56 |
return $this->path; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function exists() { |
| 65 |
return is_file( $this->path ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function getSize() { |
| 74 |
AtEase::suppressWarnings(); |
| 75 |
$size = filesize( $this->path ); |
| 76 |
AtEase::restoreWarnings(); |
| 77 |
|
| 78 |
return $size; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function getTimestamp() { |
| 87 |
AtEase::suppressWarnings(); |
| 88 |
$timestamp = filemtime( $this->path ); |
| 89 |
AtEase::restoreWarnings(); |
| 90 |
if ( $timestamp !== false ) { |
| 91 |
$timestamp = ConvertibleTimestamp::convert( TS_MW, $timestamp ); |
| 92 |
} |
| 93 |
|
| 94 |
return $timestamp; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function getProps( $ext = true ) { |
| 115 |
$info = self::placeholderProps(); |
| 116 |
$info['fileExists'] = $this->exists(); |
| 117 |
|
| 118 |
if ( $info['fileExists'] ) { |
| 119 |
$info['size'] = $this->getSize(); |
| 120 |
$info['sha1'] = $this->getSha1Base36(); |
| 121 |
|
| 122 |
$mime = mime_content_type( $this->path ); |
| 123 |
|
| 124 |
$info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime; |
| 125 |
|
| 126 |
$info['mime'] = $mime; |
| 127 |
|
| 128 |
if ( strpos( $mime, '/' ) !== false ) { |
| 129 |
[ $info['major_mime'], $info['minor_mime'] ] = explode( '/', $mime, 2 ); |
| 130 |
} else { |
| 131 |
[ $info['major_mime'], $info['minor_mime'] ] = [ $mime, 'unknown' ]; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return $info; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static function placeholderProps() { |
| 153 |
$info = []; |
| 154 |
$info['fileExists'] = false; |
| 155 |
$info['size'] = 0; |
| 156 |
$info['file-mime'] = null; |
| 157 |
$info['major_mime'] = null; |
| 158 |
$info['minor_mime'] = null; |
| 159 |
$info['mime'] = null; |
| 160 |
$info['sha1'] = ''; |
| 161 |
|
| 162 |
return $info; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function getSha1Base36( $recache = false ) { |
| 176 |
if ( $this->sha1Base36 !== null && !$recache ) { |
| 177 |
return $this->sha1Base36; |
| 178 |
} |
| 179 |
|
| 180 |
AtEase::suppressWarnings(); |
| 181 |
$this->sha1Base36 = sha1_file( $this->path ); |
| 182 |
AtEase::restoreWarnings(); |
| 183 |
|
| 184 |
if ( $this->sha1Base36 !== false ) { |
| 185 |
$this->sha1Base36 = \Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 ); |
| 186 |
} |
| 187 |
|
| 188 |
return $this->sha1Base36; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static function extensionFromPath( $path ) { |
| 198 |
$i = strrpos( $path, '.' ); |
| 199 |
|
| 200 |
return strtolower( $i ? substr( $path, $i + 1 ) : '' ); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static function getPropsFromPath( $path, $ext = true ) { |
| 212 |
$fsFile = new self( $path ); |
| 213 |
|
| 214 |
return $fsFile->getProps( $ext ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public static function getSha1Base36FromPath( $path ) { |
| 228 |
$fsFile = new self( $path ); |
| 229 |
|
| 230 |
return $fsFile->getSha1Base36(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
class_alias( FSFile::class, 'FSFile' ); |