Skip to content

Commit e4bf50d

Browse files
fix: remove just uploaded files in case of error - also for storages which do not support part files (#40892)
1 parent efc6fb5 commit e4bf50d

5 files changed

Lines changed: 107 additions & 14 deletions

File tree

apps/dav/lib/Connector/Sabre/File.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ public function put($data) {
187187
try {
188188
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
189189
} catch (LockedException $e) {
190-
if ($usePartFile) {
191-
$partStorage->unlink($internalPartPath);
192-
}
190+
$this->cleanFailedUpload($partStorage, $internalPartPath);
193191
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
194192
}
195193

@@ -234,9 +232,7 @@ public function put($data) {
234232
}
235233
}
236234
} catch (\Exception $e) {
237-
if ($usePartFile) {
238-
$partStorage->unlink($internalPartPath);
239-
}
235+
$this->cleanFailedUpload($partStorage, $internalPartPath);
240236
$this->convertToSabreException($e);
241237
}
242238

@@ -255,9 +251,7 @@ public function put($data) {
255251
try {
256252
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
257253
} catch (LockedException $e) {
258-
if ($usePartFile) {
259-
$partStorage->unlink($internalPartPath);
260-
}
254+
$this->cleanFailedUpload($partStorage, $internalPartPath);
261255
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
262256
}
263257

@@ -633,9 +627,7 @@ private function createFileChunked($data) {
633627
}
634628
return $etag;
635629
} catch (\Exception $e) {
636-
if ($partFile !== null) {
637-
$targetStorage->unlink($targetInternalPath);
638-
}
630+
$this->cleanFailedUpload($targetStorage, $targetInternalPath);
639631
$this->convertToSabreException($e);
640632
}
641633
}
@@ -767,4 +759,23 @@ protected function header($string) {
767759
public function getNode() {
768760
return \OC::$server->getRootFolder()->get($this->getFileInfo()->getPath());
769761
}
762+
763+
private function cleanFailedUpload(Storage $partStorage, $internalPartPath): void {
764+
if ($partStorage->file_exists($internalPartPath)) {
765+
try {
766+
# broken/uncompleted uploaded files shall not go into trash-bin
767+
if (class_exists(\OCA\Files_Trashbin\Storage::class)) {
768+
\OCA\Files_Trashbin\Storage::$disableTrash = true;
769+
}
770+
# delete file from storage
771+
$partStorage->unlink($internalPartPath);
772+
# delete file from cache
773+
$partStorage->getCache()->remove($internalPartPath);
774+
} finally {
775+
if (class_exists(\OCA\Files_Trashbin\Storage::class)) {
776+
\OCA\Files_Trashbin\Storage::$disableTrash = false;
777+
}
778+
}
779+
}
780+
}
770781
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* @author Thomas Müller <thomas.mueller@tmit.eu>
4+
*
5+
* @copyright Copyright (c) 2023, ownCloud GmbH
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
namespace OCA\DAV\Tests\unit\Upload;
22+
23+
use OC\Files\FileInfo;
24+
use OC\Files\View;
25+
use OCA\DAV\Connector\Sabre\File;
26+
use OCP\Lock\ILockingProvider;
27+
use Sabre\DAV\Exception\BadRequest;
28+
use Test\TestCase;
29+
use Test\Traits\UserTrait;
30+
31+
/**
32+
* @group DB
33+
*/
34+
class FailedUploadTest extends TestCase {
35+
use UserTrait;
36+
37+
public function test(): void {
38+
# init
39+
$user = $this->createUser('user');
40+
$folder = \OC::$server->getUserFolder($user->getUID());
41+
42+
# fake request
43+
$_SERVER['CONTENT_LENGTH'] = 12;
44+
$_SERVER['REQUEST_METHOD'] = 'PUT';
45+
unset($_SERVER['HTTP_OC_CHUNKED']);
46+
47+
# perform the request
48+
$path = '/test.txt';
49+
$info = new FileInfo("user/files/$path", null, null, [], null);
50+
$view = new View();
51+
$file = new File($view, $info);
52+
$file->acquireLock(ILockingProvider::LOCK_SHARED);
53+
$stream = fopen('data://text/plain,' . '123456', 'rb');
54+
try {
55+
$file->put($stream);
56+
} catch (BadRequest $e) {
57+
self::assertEquals('expected filesize 12 got 6', $e->getMessage());
58+
}
59+
60+
# assert file does not exist
61+
self::assertFalse($folder->nodeExists($path));
62+
# ensure folder can ge listed
63+
$children = $folder->getDirectoryListing();
64+
self::assertCount(0, $children);
65+
66+
# assert there is no file on disk
67+
$internalPath = $folder->getInternalPath();
68+
self::assertFalse($folder->getStorage()->file_exists($internalPath.'/test.txt'));
69+
70+
# assert file is not in cache
71+
self::assertFalse($folder->getStorage()->getCache()->inCache($internalPath.'/test.txt'));
72+
}
73+
}

apps/files/js/file-upload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,9 @@ OC.Uploader.prototype = _.extend({
12961296
var message = '';
12971297
if (upload) {
12981298
var response = upload.getResponse();
1299-
message = response.message;
1299+
message = t('files', 'Failed to upload the file "{fileName}": {error}', {fileName: upload.getFileName(), error: response.message});
13001300
}
1301+
13011302
OC.Notification.show(message || data.errorThrown, {type: 'error'});
13021303
}
13031304

apps/files_trashbin/lib/Storage.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ class Storage extends Wrapper {
3939
/**
4040
* Disable trash logic
4141
*
42+
* NOTE: this public for a very specific purpose to handle broken uploads.
43+
* Don't touch this property unless you know what this is doing! :dancers:
44+
*
4245
* @var bool
4346
*/
44-
private static $disableTrash = false;
47+
public static $disableTrash = false;
4548

4649
/** @var IUserManager */
4750
private $userManager;

changelog/unreleased/40892

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Bugfix: Cleanup storage and database after failed files uploads
2+
3+
Storage and database is cleaned up of any remaining items in case a files upload fails.
4+
5+
https://github.com/owncloud/core/pull/40892

0 commit comments

Comments
 (0)