123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace OCA\DAV\Upload;
- use OCA\DAV\Connector\Sabre\Directory;
- use OCA\DAV\Connector\Sabre\Exception\Forbidden;
- use OCP\AppFramework\Http;
- use Sabre\DAV\Exception\BadRequest;
- use Sabre\DAV\Exception\NotFound;
- use Sabre\DAV\INode;
- use Sabre\DAV\Server;
- use Sabre\DAV\ServerPlugin;
- class ChunkingPlugin extends ServerPlugin {
-
- private $server;
-
- private $sourceNode;
-
- public function initialize(Server $server) {
- $server->on('beforeMove', [$this, 'beforeMove']);
- $this->server = $server;
- }
-
- public function beforeMove($sourcePath, $destination) {
- $this->sourceNode = $this->server->tree->getNodeForPath($sourcePath);
- if (!$this->sourceNode instanceof FutureFile) {
-
- return;
- }
- try {
-
- $destinationNode = $this->server->tree->getNodeForPath($destination);
- if ($destinationNode instanceof Directory) {
- throw new BadRequest("The given destination $destination is a directory.");
- }
- } catch (NotFound $e) {
-
- }
- $this->verifySize();
- return $this->performMove($sourcePath, $destination);
- }
-
- public function performMove($path, $destination) {
- $fileExists = $this->server->tree->nodeExists($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;
- }
-
- $this->server->emit('afterMove', [$path, $destination]);
- $this->server->emit('afterUnbind', [$path]);
- $this->server->emit('afterBind', [$destination]);
- $response = $this->server->httpResponse;
- $response->setHeader('Content-Length', '0');
- $response->setStatus($fileExists ? Http::STATUS_NO_CONTENT : Http::STATUS_CREATED);
- return false;
- }
-
- private function verifySize() {
- $expectedSize = $this->server->httpRequest->getHeader('OC-Total-Length');
- if ($expectedSize === null) {
- return;
- }
- $actualSize = $this->sourceNode->getSize();
-
-
- if ((string)$expectedSize !== (string)$actualSize) {
- throw new BadRequest("Chunks on server do not sum up to $expectedSize but to $actualSize bytes");
- }
- }
- }
|