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();
42 // Check if the source file exists in the file system and is not too big
43 if ( !is_file( $this->params
['src'] ) ) {
44 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
48 // Check if the source file is too big
49 $maxBytes = $this->backend
->maxFileSizeInternal();
50 if ( filesize( $this->params
['src'] ) > $maxBytes ) {
51 $status->fatal( 'backend-fail-maxsize', $this->params
['dst'], $maxBytes );
55 // Check if an incompatible destination file exists
56 $status->merge( $this->precheckDestExistence( $predicates ) );
57 $this->params
['dstExists'] = $this->destExists
; // see FileBackendStore::setFileCache()
58 if ( $status->isOK() ) {
59 // Update file existence predicates
60 $predicates['exists'][$this->params
['dst']] = true;
61 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
64 return $status; // safe to call attempt()
67 protected function doAttempt() {
68 if ( $this->overwriteSameCase
) {
69 $status = StatusValue
::newGood(); // nothing to do
71 // Store the file at the destination
72 $status = $this->backend
->storeInternal( $this->setFlags( $this->params
) );
78 protected function getSourceSha1Base36() {
79 AtEase
::suppressWarnings();
80 $hash = sha1_file( $this->params
['src'] );
81 AtEase
::restoreWarnings();
82 if ( $hash !== false ) {
83 $hash = Wikimedia\base_convert
( $hash, 16, 36, 31 );
89 public function storagePathsChanged() {
90 return [ $this->params
['dst'] ];