Skip to content

Commit

Permalink
Add Content-Disposition header to GET responses of versions
Browse files Browse the repository at this point in the history
Allow decryption of versions when being accessed by getContentOfVersion()

behat test for restoring a file

behat test for restoring a file
  • Loading branch information
DeepDiver1975 authored and peterprochaska committed Nov 14, 2017
1 parent d8235c2 commit bca2549
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 2 deletions.
6 changes: 6 additions & 0 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
namespace OCA\DAV\Connector\Sabre;

use OC\AppFramework\Http\Request;
use OCA\DAV\Files\IProvidesAdditionalHeaders;
use OCA\DAV\Meta\MetaFile;
use OCP\Files\ForbiddenException;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
Expand Down Expand Up @@ -255,6 +257,10 @@ function httpGet(RequestInterface $request, ResponseInterface $response) {
// cause memory problems in the nginx process.
$response->addHeader('X-Accel-Buffering', 'no');
}

if ($node instanceof IProvidesAdditionalHeaders) {
$response->addHeaders($node->getHeaders());
}
}

/**
Expand Down
39 changes: 39 additions & 0 deletions apps/dav/lib/Files/IProvidesAdditionalHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @author Thomas Müller <[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;


/**
* Interface IProvidesAdditionalHeaders
* This interface allows to add additional headers to the response
*
* @package OCA\DAV\Files
*/
interface IProvidesAdditionalHeaders {

/**
* @return array
*/
public function getHeaders();

}
19 changes: 18 additions & 1 deletion apps/dav/lib/Meta/MetaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OC\Files\Meta\MetaFileVersionNode;
use OCA\DAV\Files\ICopySource;
use OCA\DAV\Files\IProvidesAdditionalHeaders;
use Sabre\DAV\File;

/**
Expand All @@ -33,7 +34,7 @@
*
* @package OCA\DAV\Meta
*/
class MetaFile extends File implements ICopySource {
class MetaFile extends File implements ICopySource, IProvidesAdditionalHeaders {

/** @var \OCP\Files\File */
private $file;
Expand Down Expand Up @@ -82,14 +83,30 @@ public function getLastModified() {
return $this->file->getMTime();
}

/**
* @inheritdoc
*/
public function getETag() {
return $this->file->getEtag();
}

/**
* @inheritdoc
*/
public function copy($path) {
if ($this->file instanceof MetaFileVersionNode) {
return $this->file->copy($path);
}
return false;
}

/**
* @return array
*/
public function getHeaders() {
if ($this->file instanceof \OCP\Files\IProvidesAdditionalHeaders) {
return $this->file->getHeaders();
}
return [];
}
}
13 changes: 12 additions & 1 deletion lib/private/Files/Meta/MetaFileVersionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OC\Files\Node\AbstractFile;
use OC\Files\Node\File;
use OCP\Files\IProvidesAdditionalHeaders;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IVersionedStorage;
use OCP\Files\Storage;
Expand All @@ -35,7 +36,7 @@
*
* @package OC\Files\Meta
*/
class MetaFileVersionNode extends AbstractFile {
class MetaFileVersionNode extends AbstractFile implements IProvidesAdditionalHeaders {

/** @var string */
private $versionId;
Expand Down Expand Up @@ -124,4 +125,14 @@ public function getEtag() {
public function fopen($mode) {
return $this->storage->getContentOfVersion($this->internalPath, $this->versionId);
}

/**
* @return array
*/
public function getHeaders() {
$fileName = basename($this->internalPath);
return [
'Content-Disposition' => 'attachment; filename="' . rawurlencode($fileName) . '"'
];
}
}
9 changes: 9 additions & 0 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ public function file_get_contents($path) {
return $this->storage->file_get_contents($path);
}

public function getContentOfVersion($internalPath, $versionId) {
$v = $this->storage->getVersion($internalPath, $versionId);
if ($this->encryptionManager->isEnabled() !== false) {
return $this->fopen($v['storage_location'], 'r+');
}

return $this->storage->getContentOfVersion($internalPath, $versionId);
}

/**
* see http://php.net/manual/en/function.file_put_contents.php
*
Expand Down
43 changes: 43 additions & 0 deletions lib/public/Files/IProvidesAdditionalHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @author Thomas Müller <[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 OCP\Files;


/**
* Interface IProvidesAdditionalHeaders
* This interface allows to add additional headers to the response
*
* @package OCP\Files
* @since 10.0.5
*/
interface IProvidesAdditionalHeaders {

/**
* Returns an array of headers.
*
* @return array
* @since 10.0.5
*/
public function getHeaders();

}
13 changes: 13 additions & 0 deletions tests/integration/features/bootstrap/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,19 @@ public function theContentLengthOfFileForUserInVersionsFolderIs($path, $index, $
PHPUnit_Framework_Assert::assertEquals($length, $elements[1]['{DAV:}getcontentlength']);
}

/**
* @When user :user restores version nr :versionIndex of file :path
* @param $user
* @param $versionIndex
* @param $path
*/
public function userRestoresVersionNrOfFile($user, $versionIndex, $path) {
$fileId = $this->getFileIdForPath($user, $path);
$client = $this->getSabreClient($user);
$versions = array_keys($this->listVersionFolder($user, '/meta/'.$fileId.'/v', 1));
$client->request('COPY', $versions[1], null, ['Destination' => $this->makeSabrePath($user, $path)]);
}

/* Returns the elements of a report command
* @param string $user
* @param string $path
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/features/dav-versions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ Feature: dav-versions
And user "user0" deletes file "/davtest.txt"
When user "user0" uploads file "data/davtest.txt" to "/davtest.txt"
Then the version folder of file "/davtest.txt" for user "user0" contains "0" elements

Scenario: Restore a file and check, if the content is now in the current file
Given user "user0" exists
And as an "user0"
And user "user0" uploads file with content "123" to "/davtest.txt"
And user "user0" uploads file with content "12345" to "/davtest.txt"
And the version folder of file "/davtest.txt" for user "user0" contains "1" elements
When user "user0" restores version nr "1" of file "/davtest.txt"
Then downloading file "davtest.txt"
And downloaded content should be "123"

0 comments on commit bca2549

Please sign in to comment.