97 if ( is_object( $this->user ) ) {
98 $this->userId = $this->user->getId();
99 $this->isRegistered = $this->user->isRegistered();
116 public function getFile( $key, $noAuth =
false ) {
117 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
119 wfMessage(
'uploadstash-bad-path-bad-format', $key )
123 if ( !$noAuth && !$this->isRegistered ) {
129 if ( !isset( $this->fileMetadata[$key] ) ) {
136 if ( !isset( $this->fileMetadata[$key] ) ) {
138 wfMessage(
'uploadstash-file-not-found', $key )
146 if ( strlen( $this->fileMetadata[$key][
'us_props'] ) ) {
147 $this->fileProps[$key] =
unserialize( $this->fileMetadata[$key][
'us_props'] );
149 wfDebug( __METHOD__ .
" fetched props for $key from file" );
150 $path = $this->fileMetadata[$key][
'us_path'];
151 $this->fileProps[$key] = $this->repo->getFileProps(
$path );
155 if ( !$this->files[$key]->exists() ) {
156 wfDebug( __METHOD__ .
" tried to get file at $key, but it doesn't exist" );
163 if ( !$noAuth && $this->fileMetadata[$key][
'us_user'] != $this->userId ) {
165 wfMessage(
'uploadstash-wrong-owner', $key )
169 return $this->files[$key];
181 return $this->fileMetadata[$key];
193 return $this->fileProps[$key];
209 if ( !is_file(
$path ) ) {
210 wfDebug( __METHOD__ .
" tried to stash file at '$path', but it doesn't exist" );
218 wfDebug( __METHOD__ .
" stashing file at '$path'" );
223 if ( !preg_match(
"/\\.\\Q$extension\\E$/",
$path ) ) {
224 $pathWithGoodExtension =
"$path.$extension";
226 $pathWithGoodExtension =
$path;
234 list( $usec, $sec ) = explode(
' ', microtime() );
235 $usec = substr( $usec, 2 );
236 $key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) .
'.' .
237 Wikimedia\base_convert( mt_rand(), 10, 36 ) .
'.' .
238 $this->userId .
'.' .
243 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
245 wfMessage(
'uploadstash-bad-path-bad-format', $key )
249 wfDebug( __METHOD__ .
" key for '$path': $key" );
252 $storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ),
$path );
254 if ( !$storeStatus->isOK() ) {
263 $error = $storeStatus->getErrorsArray();
264 $error = reset( $error );
265 if ( !count( $error ) ) {
266 $error = $storeStatus->getWarningsArray();
267 $error = reset( $error );
268 if ( !count( $error ) ) {
269 $error = [
'unknown',
'no error recorded' ];
274 $errorMsg = array_shift( $error );
277 $stashPath = $storeStatus->value;
280 if ( !$this->isRegistered ) {
287 wfDebug( __METHOD__ .
" inserting $stashPath under $key" );
288 $dbw = $this->repo->getMasterDB();
291 if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
299 $this->fileMetadata[$key] = [
302 'us_orig_path' =>
$path,
303 'us_path' => $stashPath,
304 'us_props' => $dbw->encodeBlob( $serializedFileProps ),
312 'us_source_type' => $sourceType,
313 'us_timestamp' => $dbw->timestamp(),
314 'us_status' =>
'finished'
319 $this->fileMetadata[$key],
325 $this->fileMetadata[$key][
'us_id'] = $dbw->insertId();
327 # create the UploadStashFile object for this file.
341 if ( !$this->isRegistered ) {
347 wfDebug( __METHOD__ .
' clearing all rows for user ' . $this->userId );
348 $dbw = $this->repo->getMasterDB();
351 [
'us_user' => $this->userId ],
357 $this->fileMetadata = [];
371 if ( !$this->isRegistered ) {
377 $dbw = $this->repo->getMasterDB();
381 $row = $dbw->selectRow(
384 [
'us_key' => $key ],
390 wfMessage(
'uploadstash-no-such-key', $key )
394 if ( $row->us_user != $this->userId ) {
396 wfMessage(
'uploadstash-wrong-owner', $key )
410 wfDebug( __METHOD__ .
" clearing row $key" );
415 $dbw = $this->repo->getMasterDB();
419 [
'us_key' => $key ],
426 $this->files[$key]->remove();
428 unset( $this->files[$key] );
429 unset( $this->fileMetadata[$key] );
441 if ( !$this->isRegistered ) {
447 $dbr = $this->repo->getReplicaDB();
451 [
'us_user' => $this->userId ],
455 if ( !is_object(
$res ) ||
$res->numRows() == 0 ) {
462 foreach (
$res as $row ) {
463 array_push(
$keys, $row->us_key );
481 $n = strrpos(
$path,
'.' );
483 if ( $n !==
false ) {
484 $extension = $n ? substr(
$path, $n + 1 ) :
'';
488 $mimeType = $magic->guessMimeType(
$path );
489 $extension = $magic->getExtensionFromMimeTypeOrNull( $mimeType );
516 $dbr = $this->repo->getMasterDB();
518 $dbr = $this->repo->getReplicaDB();
521 $row =
$dbr->selectRow(
524 'us_user',
'us_key',
'us_orig_path',
'us_path',
'us_props',
525 'us_size',
'us_sha1',
'us_mime',
'us_media_type',
526 'us_image_width',
'us_image_height',
'us_image_bits',
527 'us_source_type',
'us_timestamp',
'us_status',
529 [
'us_key' => $key ],
533 if ( !is_object( $row ) ) {
538 $this->fileMetadata[$key] = (array)$row;
539 $this->fileMetadata[$key][
'us_props'] =
$dbr->decodeBlob( $row->us_props );
553 if (
$file->getSize() === 0 ) {
558 $this->files[$key] =
$file;