3 * Helper class for representing operations with transaction support.
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 use Wikimedia\AtEase\AtEase
;
27 * Store a file into the backend from a file on the file system.
28 * Parameters for this operation are outlined in FileBackend::doOperations().
30 class StoreFileOp
extends FileOp
{
31 protected function allowedParams() {
34 [ 'overwrite', 'overwriteSame', 'headers' ],
39 protected function doPrecheck( array &$predicates ) {
40 $status = StatusValue
::newGood();
41 // Check if the source file exists on the file system
42 if ( !is_file( $this->params
['src'] ) ) {
43 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
46 // Check if the source file is too big
47 } elseif ( filesize( $this->params
['src'] ) > $this->backend
->maxFileSizeInternal() ) {
48 $status->fatal( 'backend-fail-maxsize',
49 $this->params
['dst'], $this->backend
->maxFileSizeInternal() );
50 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
53 // Check if a file can be placed/changed at the destination
54 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
55 $status->fatal( 'backend-fail-usable', $this->params
['dst'] );
56 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
60 // Check if destination file exists
61 $status->merge( $this->precheckDestExistence( $predicates ) );
62 $this->params
['dstExists'] = $this->destExists
; // see FileBackendStore::setFileCache()
63 if ( $status->isOK() ) {
64 // Update file existence predicates
65 $predicates['exists'][$this->params
['dst']] = true;
66 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
69 return $status; // safe to call attempt()
72 protected function doAttempt() {
73 if ( !$this->overwriteSameCase
) {
74 // Store the file at the destination
75 return $this->backend
->storeInternal( $this->setFlags( $this->params
) );
78 return StatusValue
::newGood();
81 protected function getSourceSha1Base36() {
82 AtEase
::suppressWarnings();
83 $hash = sha1_file( $this->params
['src'] );
84 AtEase
::restoreWarnings();
85 if ( $hash !== false ) {
86 $hash = Wikimedia\base_convert
( $hash, 16, 36, 31 );
92 public function storagePathsChanged() {
93 return [ $this->params
['dst'] ];