| 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\FileOps; |
| 25 |
|
| 26 |
use StatusValue; |
| 27 |
use Wikimedia\AtEase\AtEase; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class StoreFileOp extends FileOp { |
| 34 |
protected function allowedParams() { |
| 35 |
return [ |
| 36 |
[ 'src', 'dst' ], |
| 37 |
[ 'overwrite', 'overwriteSame', 'headers' ], |
| 38 |
[ 'src', 'dst' ] |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
protected function doPrecheck( |
| 43 |
FileStatePredicates $opPredicates, |
| 44 |
FileStatePredicates $batchPredicates |
| 45 |
) { |
| 46 |
$status = StatusValue::newGood(); |
| 47 |
|
| 48 |
|
| 49 |
if ( !is_file( $this->params['src'] ) ) { |
| 50 |
$status->fatal( 'backend-fail-notexists', $this->params['src'] ); |
| 51 |
|
| 52 |
return $status; |
| 53 |
} |
| 54 |
|
| 55 |
$sourceSize = $this->getSourceSize(); |
| 56 |
$maxFileSize = $this->backend->maxFileSizeInternal(); |
| 57 |
if ( $sourceSize > $maxFileSize ) { |
| 58 |
$status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize ); |
| 59 |
|
| 60 |
return $status; |
| 61 |
} |
| 62 |
|
| 63 |
$sourceSha1 = function () { |
| 64 |
static $sha1 = null; |
| 65 |
$sha1 ??= $this->getSourceSha1Base36(); |
| 66 |
return $sha1; |
| 67 |
}; |
| 68 |
$status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) ); |
| 69 |
$this->params['dstExists'] = $this->destExists; |
| 70 |
|
| 71 |
|
| 72 |
if ( $status->isOK() ) { |
| 73 |
$batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 ); |
| 74 |
} |
| 75 |
|
| 76 |
return $status; |
| 77 |
} |
| 78 |
|
| 79 |
protected function doAttempt() { |
| 80 |
if ( $this->overwriteSameCase ) { |
| 81 |
$status = StatusValue::newGood(); |
| 82 |
} else { |
| 83 |
|
| 84 |
$status = $this->backend->storeInternal( $this->setFlags( $this->params ) ); |
| 85 |
} |
| 86 |
|
| 87 |
return $status; |
| 88 |
} |
| 89 |
|
| 90 |
protected function getSourceSize() { |
| 91 |
AtEase::suppressWarnings(); |
| 92 |
$size = filesize( $this->params['src'] ); |
| 93 |
AtEase::restoreWarnings(); |
| 94 |
|
| 95 |
return $size; |
| 96 |
} |
| 97 |
|
| 98 |
protected function getSourceSha1Base36() { |
| 99 |
AtEase::suppressWarnings(); |
| 100 |
$hash = sha1_file( $this->params['src'] ); |
| 101 |
AtEase::restoreWarnings(); |
| 102 |
if ( $hash !== false ) { |
| 103 |
$hash = \Wikimedia\base_convert( $hash, 16, 36, 31 ); |
| 104 |
} |
| 105 |
|
| 106 |
return $hash; |
| 107 |
} |
| 108 |
|
| 109 |
public function storagePathsChanged() { |
| 110 |
return [ $this->params['dst'] ]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
class_alias( StoreFileOp::class, 'StoreFileOp' ); |