forked from owncloud/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the required server-side support for delta-sync.
The basic approach is to store zsync metadata files in a folder called `files_zsync/` which stores them based on fileid. These metadata files can be requested by the client via a new route `dav/files/$user/$path?zsync`. They can also be deleted using the same route. This is implemented using a new `ServerPlugin` called `ZsyncPlugin`. Filesystem hooks are used to mirror any `copy/delete` operation on the base file or containing folders onto the metadata files. To ensure any changes server-side changes are will not produce out-of-sync metadata. The upload path is implemented by creating a new plugin `ChunkingPluginZsync`. The chunk file ids are now assumed to be named as the offsets into the original file. Special handling is done when a chunk named `.zsync` is found which is the generated client-side metadata. This means copying the contents to the `files_zsync/` folder. The core reason behind this is to ensure that both the metadata and the file are put in place atomically, as part of the final `MOVE` request. The implemenation adds a new class `AssemblyStreamZsync` which extends `AssemblyStream` with additional support to fill in the data between chunk offsets from a `backingFile`. A new `zsync` capability is added to the dav app, which can be checked by the client to know if delta-sync is supported or not. A zsync dav property is also returned for files which have metadata on the server. This commit closes owncloud#16162.
- Loading branch information
1 parent
c596d4c
commit 7537b4a
Showing
16 changed files
with
1,783 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
/** | ||
* @author Ahmed Ammar <[email protected]> | ||
* | ||
* @copyright Copyright (c) 2017, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
namespace OCA\DAV\Files; | ||
|
||
use OC\AppFramework\Http; | ||
use OCP\Files\NotFoundException; | ||
use OCP\Files\NotPermittedException; | ||
use Sabre\DAV\Server; | ||
use Sabre\DAV\ServerPlugin; | ||
use Sabre\DAV\PropFind; | ||
use Sabre\HTTP\RequestInterface; | ||
use Sabre\HTTP\ResponseInterface; | ||
use OC\Files\View; | ||
use \Exception; | ||
|
||
class ZsyncPlugin extends ServerPlugin { | ||
|
||
// namespace | ||
const ZSYNC_PROPERTYNAME = '{http://owncloud.org/ns}zsync'; | ||
|
||
/** @var OC\Files\View */ | ||
private $view; | ||
|
||
public function __construct(View $view) { | ||
$this->view = $view; | ||
$this->view->mkdir('files_zsync'); | ||
} | ||
|
||
/** | ||
* Initializes the plugin and registers event handlers | ||
* | ||
* @param Server $server | ||
* @return void | ||
*/ | ||
function initialize(Server $server) { | ||
$server->on('method:GET', [$this, 'httpGet'], 90); | ||
$server->on('method:DELETE', [$this, 'httpDelete'], 90); | ||
$server->on('propFind', [$this, 'handleGetProperties']); | ||
} | ||
|
||
/** | ||
* Intercepts GET requests on file urls ending with ?zsync. | ||
* | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
*/ | ||
function httpGet(RequestInterface $request, ResponseInterface $response) { | ||
|
||
$queryParams = $request->getQueryParameters(); | ||
if (!array_key_exists('zsync', $queryParams)) { | ||
return true; | ||
} | ||
|
||
$path = ltrim($request->getPath(), '/'); | ||
/* remove files/$user */ | ||
$path = implode('/', array_slice(explode('/', $path), 2)); | ||
/* If basefile not found this is an error */ | ||
if (!$this->view->file_exists('files/'.$path)) { | ||
$response->setStatus(Http::STATUS_NOT_FOUND); | ||
return false; | ||
} | ||
|
||
$info = $this->view->getFileInfo('files/'.$path); | ||
$zsyncMetadataFile = 'files_zsync/'.$info->getId(); | ||
if ($this->view->file_exists($zsyncMetadataFile)) { | ||
$content = $this->view->file_get_contents($zsyncMetadataFile); | ||
$response->setStatus(Http::STATUS_OK); | ||
$response->setBody($content); | ||
} else { | ||
$response->setStatus(Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Intercepts DELETE requests on file urls ending with ?zsync. | ||
* | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
*/ | ||
function httpDelete(RequestInterface $request, ResponseInterface $response) { | ||
|
||
$queryParams = $request->getQueryParameters(); | ||
if (!array_key_exists('zsync', $queryParams)) { | ||
return true; | ||
} | ||
|
||
$path = ltrim($request->getPath(), '/'); | ||
/* remove files/$user */ | ||
$path = implode('/', array_slice(explode('/', $path), 2)); | ||
/* If basefile not found this is an error */ | ||
if (!$this->view->file_exists('files/'.$path)) { | ||
$response->setStatus(Http::STATUS_NOT_FOUND); | ||
return false; | ||
} | ||
|
||
$info = $this->view->getFileInfo('files/'.$path); | ||
$zsyncMetadataFile = 'files_zsync/'.$info->getId(); | ||
if ($this->view->file_exists($zsyncMetadataFile)) { | ||
$this->view->unlink($zsyncMetadataFile); | ||
$response->setStatus(Http::STATUS_OK); | ||
} else { | ||
$response->setStatus(Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Adds zsync property if metadata exists | ||
* | ||
* @param PropFind $propFind | ||
* @param \Sabre\DAV\INode $node | ||
* @return void | ||
*/ | ||
public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { | ||
if ($node instanceof \OCA\DAV\Connector\Sabre\File) { | ||
if (!$this->view->is_file('files/'.$node->getPath())) | ||
return; | ||
$info = $this->view->getFileInfo('files/'.$node->getPath()); | ||
$zsyncMetadataFile = 'files_zsync/'.$info->getId(); | ||
if ($this->view->file_exists($zsyncMetadataFile)) { | ||
$propFind->handle(self::ZSYNC_PROPERTYNAME, function() use ($node) { | ||
return 'true'; | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.