| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace Wikimedia\FileBackend\FileOps; |
| 23 |
|
| 24 |
use StatusValue; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class CreateFileOp extends FileOp { |
| 31 |
protected function allowedParams() { |
| 32 |
return [ |
| 33 |
[ 'content', 'dst' ], |
| 34 |
[ 'overwrite', 'overwriteSame', 'headers' ], |
| 35 |
[ 'dst' ] |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
protected function doPrecheck( |
| 40 |
FileStatePredicates $opPredicates, |
| 41 |
FileStatePredicates $batchPredicates |
| 42 |
) { |
| 43 |
$status = StatusValue::newGood(); |
| 44 |
|
| 45 |
|
| 46 |
$sourceSize = $this->getSourceSize(); |
| 47 |
$maxFileSize = $this->backend->maxFileSizeInternal(); |
| 48 |
if ( $sourceSize > $maxFileSize ) { |
| 49 |
$status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize ); |
| 50 |
|
| 51 |
return $status; |
| 52 |
} |
| 53 |
|
| 54 |
$sourceSha1 = $this->getSourceSha1Base36(); |
| 55 |
$status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) ); |
| 56 |
$this->params['dstExists'] = $this->destExists; |
| 57 |
|
| 58 |
|
| 59 |
if ( $status->isOK() ) { |
| 60 |
$batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 ); |
| 61 |
} |
| 62 |
|
| 63 |
return $status; |
| 64 |
} |
| 65 |
|
| 66 |
protected function doAttempt() { |
| 67 |
if ( $this->overwriteSameCase ) { |
| 68 |
$status = StatusValue::newGood(); |
| 69 |
} else { |
| 70 |
|
| 71 |
$status = $this->backend->createInternal( $this->setFlags( $this->params ) ); |
| 72 |
} |
| 73 |
|
| 74 |
return $status; |
| 75 |
} |
| 76 |
|
| 77 |
protected function getSourceSize() { |
| 78 |
return strlen( $this->params['content'] ); |
| 79 |
} |
| 80 |
|
| 81 |
protected function getSourceSha1Base36() { |
| 82 |
return \Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 ); |
| 83 |
} |
| 84 |
|
| 85 |
public function storagePathsChanged() { |
| 86 |
return [ $this->params['dst'] ]; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
class_alias( CreateFileOp::class, 'CreateFileOp' ); |