3 * Non-directory file on the file system.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileBackend
24 namespace Wikimedia\FileBackend\FSFile
;
26 use Wikimedia\AtEase\AtEase
;
27 use Wikimedia\Timestamp\ConvertibleTimestamp
;
30 * Class representing a non-directory file on the file system
32 * @ingroup FileBackend
35 /** @var string Path to file */
38 /** @var string File SHA-1 in base 36 */
39 protected $sha1Base36;
42 * Sets up the file object
44 * @param string $path Path to temporary file on local disk
46 public function __construct( $path ) {
51 * Returns the file system path
55 public function getPath() {
60 * Checks if the file exists
64 public function exists() {
65 return is_file( $this->path
);
69 * Get the file size in bytes
73 public function getSize() {
74 AtEase
::suppressWarnings();
75 $size = filesize( $this->path
);
76 AtEase
::restoreWarnings();
82 * Get the file's last-modified timestamp
84 * @return string|bool TS_MW timestamp or false on failure
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 );
98 * Get an associative array containing information about
99 * a file with the given storage path.
101 * Resulting array fields include:
103 * - size (filesize in bytes)
104 * - mime (as major/minor)
105 * - file-mime (as major/minor)
106 * - sha1 (in base 36)
110 * @param string|bool $ext The file extension, or true to extract it from the filename.
111 * Set it to false to ignore the extension. Currently unused.
114 public function getProps( $ext = true ) {
115 $info = self
::placeholderProps();
116 $info['fileExists'] = $this->exists();
118 if ( $info['fileExists'] ) {
119 $info['size'] = $this->getSize(); // bytes
120 $info['sha1'] = $this->getSha1Base36();
122 $mime = mime_content_type( $this->path
);
123 # MIME type according to file contents
124 $info['file-mime'] = ( $mime === false ) ?
'unknown/unknown' : $mime;
126 $info['mime'] = $mime;
128 if ( strpos( $mime, '/' ) !== false ) {
129 [ $info['major_mime'], $info['minor_mime'] ] = explode( '/', $mime, 2 );
131 [ $info['major_mime'], $info['minor_mime'] ] = [ $mime, 'unknown' ];
139 * Placeholder file properties to use for files that don't exist
141 * Resulting array fields include:
143 * - size (filesize in bytes)
144 * - mime (as major/minor)
145 * - file-mime (as major/minor)
146 * - sha1 (in base 36)
152 public static function placeholderProps() {
154 $info['fileExists'] = false;
156 $info['file-mime'] = null;
157 $info['major_mime'] = null;
158 $info['minor_mime'] = null;
159 $info['mime'] = null;
166 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
167 * encoding, zero padded to 31 digits.
169 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
172 * @param bool $recache
173 * @return bool|string False on failure
175 public function getSha1Base36( $recache = false ) {
176 if ( $this->sha1Base36
!== null && !$recache ) {
177 return $this->sha1Base36
;
180 AtEase
::suppressWarnings();
181 $this->sha1Base36
= sha1_file( $this->path
);
182 AtEase
::restoreWarnings();
184 if ( $this->sha1Base36
!== false ) {
185 $this->sha1Base36
= \Wikimedia\base_convert
( $this->sha1Base36
, 16, 36, 31 );
188 return $this->sha1Base36
;
192 * Get the final file extension from a file system path
194 * @param string $path
197 public static function extensionFromPath( $path ) {
198 $i = strrpos( $path, '.' );
200 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
204 * Get an associative array containing information about a file in the local filesystem.
206 * @param string $path Absolute local filesystem path
207 * @param string|bool $ext The file extension, or true to extract it from the filename.
208 * Set it to false to ignore the extension.
211 public static function getPropsFromPath( $path, $ext = true ) {
212 $fsFile = new self( $path );
214 return $fsFile->getProps( $ext );
218 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
219 * encoding, zero padded to 31 digits.
221 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
224 * @param string $path
225 * @return false|string False on failure
227 public static function getSha1Base36FromPath( $path ) {
228 $fsFile = new self( $path );
230 return $fsFile->getSha1Base36();
234 /** @deprecated class alias since 1.43 */
235 class_alias( FSFile
::class, 'FSFile' );