Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.74% |
35 / 39 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
StoreFileOp | |
89.74% |
35 / 39 |
|
83.33% |
5 / 6 |
11.13 | |
0.00% |
0 / 1 |
allowedParams | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
doPrecheck | |
78.95% |
15 / 19 |
|
0.00% |
0 / 1 |
4.15 | |||
doAttempt | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getSourceSize | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getSourceSha1Base36 | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
storagePathsChanged | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Helper class for representing operations with transaction support. |
4 | * |
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. |
9 | * |
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. |
14 | * |
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 |
19 | * |
20 | * @file |
21 | * @ingroup FileBackend |
22 | */ |
23 | |
24 | use Wikimedia\AtEase\AtEase; |
25 | |
26 | /** |
27 | * Store a file into the backend from a file on the file system. |
28 | * Parameters for this operation are outlined in FileBackend::doOperations(). |
29 | */ |
30 | class StoreFileOp extends FileOp { |
31 | protected function allowedParams() { |
32 | return [ |
33 | [ 'src', 'dst' ], |
34 | [ 'overwrite', 'overwriteSame', 'headers' ], |
35 | [ 'src', 'dst' ] |
36 | ]; |
37 | } |
38 | |
39 | protected function doPrecheck( |
40 | FileStatePredicates $opPredicates, |
41 | FileStatePredicates $batchPredicates |
42 | ) { |
43 | $status = StatusValue::newGood(); |
44 | |
45 | // Check if the source file exists in the file system |
46 | if ( !is_file( $this->params['src'] ) ) { |
47 | $status->fatal( 'backend-fail-notexists', $this->params['src'] ); |
48 | |
49 | return $status; |
50 | } |
51 | // Check if the source file is too big |
52 | $sourceSize = $this->getSourceSize(); |
53 | $maxFileSize = $this->backend->maxFileSizeInternal(); |
54 | if ( $sourceSize > $maxFileSize ) { |
55 | $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize ); |
56 | |
57 | return $status; |
58 | } |
59 | // Check if an incompatible destination file exists |
60 | $sourceSha1 = function () { |
61 | static $sha1 = null; |
62 | $sha1 ??= $this->getSourceSha1Base36(); |
63 | return $sha1; |
64 | }; |
65 | $status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) ); |
66 | $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache() |
67 | |
68 | // Update file existence predicates if the operation is expected to be allowed to run |
69 | if ( $status->isOK() ) { |
70 | $batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 ); |
71 | } |
72 | |
73 | return $status; // safe to call attempt() |
74 | } |
75 | |
76 | protected function doAttempt() { |
77 | if ( $this->overwriteSameCase ) { |
78 | $status = StatusValue::newGood(); // nothing to do |
79 | } else { |
80 | // Store the file at the destination |
81 | $status = $this->backend->storeInternal( $this->setFlags( $this->params ) ); |
82 | } |
83 | |
84 | return $status; |
85 | } |
86 | |
87 | protected function getSourceSize() { |
88 | AtEase::suppressWarnings(); |
89 | $size = filesize( $this->params['src'] ); |
90 | AtEase::restoreWarnings(); |
91 | |
92 | return $size; |
93 | } |
94 | |
95 | protected function getSourceSha1Base36() { |
96 | AtEase::suppressWarnings(); |
97 | $hash = sha1_file( $this->params['src'] ); |
98 | AtEase::restoreWarnings(); |
99 | if ( $hash !== false ) { |
100 | $hash = Wikimedia\base_convert( $hash, 16, 36, 31 ); |
101 | } |
102 | |
103 | return $hash; |
104 | } |
105 | |
106 | public function storagePathsChanged() { |
107 | return [ $this->params['dst'] ]; |
108 | } |
109 | } |