Merge pull request #22240 from nextcloud/backport/22128/stable18

[stable18] Delete chunks if the move on an upload failed
This commit is contained in:
Morris Jobke 2020-08-19 19:31:01 +02:00 committed by GitHub
commit 0deae3116e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 11 deletions

View file

@ -23,6 +23,7 @@
namespace OCA\DAV\Upload;
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
@ -69,13 +70,17 @@ class ChunkingPlugin extends ServerPlugin {
* @return bool|void false to stop handling, void to skip this handler
*/
public function performMove($path, $destination) {
if (!$this->server->tree->nodeExists($destination)) {
// skip and let the default handler do its work
return;
}
$fileExists = $this->server->tree->nodeExists($destination);
// do a move manually, skipping Sabre's default "delete" for existing nodes
$this->server->tree->move($path, $destination);
try {
$this->server->tree->move($path, $destination);
} catch (Forbidden $e) {
$sourceNode = $this->server->tree->getNodeForPath($path);
if ($sourceNode instanceof FutureFile) {
$sourceNode->delete();
}
throw $e;
}
// trigger all default events (copied from CorePlugin::move)
$this->server->emit('afterMove', [$path, $destination]);
@ -84,7 +89,7 @@ class ChunkingPlugin extends ServerPlugin {
$response = $this->server->httpResponse;
$response->setHeader('Content-Length', '0');
$response->setStatus(204);
$response->setStatus($fileExists ? 204 : 201);
return false;
}

View file

@ -101,14 +101,18 @@ class ChunkingPluginTest extends TestCase {
->method('nodeExists')
->with('target')
->will($this->returnValue(false));
$this->response->expects($this->never())
->method('setStatus');
$this->response->expects($this->once())
->method('setHeader')
->with('Content-Length', '0');
$this->response->expects($this->once())
->method('setStatus')
->with(201);
$this->request->expects($this->once())
->method('getHeader')
->with('OC-Total-Length')
->willReturn(4);
$this->assertNull($this->plugin->beforeMove('source', 'target'));
$this->assertFalse($this->plugin->beforeMove('source', 'target'));
}
public function testBeforeMoveFutureFileMoveIt() {
@ -143,7 +147,7 @@ class ChunkingPluginTest extends TestCase {
$this->assertFalse($this->plugin->beforeMove('source', 'target'));
}
public function testBeforeMoveSizeIsWrong() {
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
$this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');