| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\FileBackend\FSFile; |
| 4 |
|
| 5 |
use Wikimedia\AtEase\AtEase; |
| 6 |
use Wikimedia\FileBackend\FSFile\TempFSFile; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class TempFSFileFactory { |
| 12 |
|
| 13 |
private $tmpDirectory; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public function __construct( $tmpDirectory = null ) { |
| 20 |
$this->tmpDirectory = $tmpDirectory; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function newTempFSFile( $prefix, $extension = '' ) { |
| 32 |
$ext = ( $extension != '' ) ? ".{$extension}" : ''; |
| 33 |
$tmpDirectory = $this->tmpDirectory; |
| 34 |
if ( !is_string( $tmpDirectory ) ) { |
| 35 |
$tmpDirectory = TempFSFile::getUsableTempDirectory(); |
| 36 |
} |
| 37 |
|
| 38 |
$attempts = 5; |
| 39 |
while ( $attempts-- ) { |
| 40 |
$hex = sprintf( '%06x%06x', mt_rand( 0, 0xffffff ), mt_rand( 0, 0xffffff ) ); |
| 41 |
$path = "$tmpDirectory/$prefix$hex$ext"; |
| 42 |
AtEase::suppressWarnings(); |
| 43 |
$newFileHandle = fopen( $path, 'x' ); |
| 44 |
AtEase::restoreWarnings(); |
| 45 |
if ( $newFileHandle ) { |
| 46 |
fclose( $newFileHandle ); |
| 47 |
$tmpFile = new TempFSFile( $path ); |
| 48 |
$tmpFile->autocollect(); |
| 49 |
|
| 50 |
return $tmpFile; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
return null; |
| 56 |
} |
| 57 |
} |