Use cache and add tests
This commit is contained in:
parent
377d7fb8a8
commit
6724f76573
|
@ -29,6 +29,7 @@ namespace OCA\Encryption\Crypto;
|
||||||
|
|
||||||
|
|
||||||
use OC\Encryption\Exceptions\DecryptionFailedException;
|
use OC\Encryption\Exceptions\DecryptionFailedException;
|
||||||
|
use OC\Files\View;
|
||||||
use OCA\Encryption\Exceptions\PublicKeyMissingException;
|
use OCA\Encryption\Exceptions\PublicKeyMissingException;
|
||||||
use OCA\Encryption\Session;
|
use OCA\Encryption\Session;
|
||||||
use OCA\Encryption\Util;
|
use OCA\Encryption\Util;
|
||||||
|
@ -186,7 +187,7 @@ class Encryption implements IEncryptionModule {
|
||||||
$this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
|
$this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->version = (int)$this->keyManager->getVersion($this->realPath);
|
$this->version = (int)$this->keyManager->getVersion($this->realPath, new View());
|
||||||
|
|
||||||
if (
|
if (
|
||||||
$mode === 'w'
|
$mode === 'w'
|
||||||
|
@ -235,7 +236,7 @@ class Encryption implements IEncryptionModule {
|
||||||
} else {
|
} else {
|
||||||
$version = $this->version + 1;
|
$version = $this->version + 1;
|
||||||
}
|
}
|
||||||
$this->keyManager->setVersion($this->path, $this->version+1);
|
$this->keyManager->setVersion($this->path, $this->version+1, new View());
|
||||||
if (!empty($this->writeCache)) {
|
if (!empty($this->writeCache)) {
|
||||||
$result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $version, $position);
|
$result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $version, $position);
|
||||||
$this->writeCache = '';
|
$this->writeCache = '';
|
||||||
|
@ -371,7 +372,7 @@ class Encryption implements IEncryptionModule {
|
||||||
if(empty($this->realPath)) {
|
if(empty($this->realPath)) {
|
||||||
$this->realPath = $path;
|
$this->realPath = $path;
|
||||||
}
|
}
|
||||||
$version = $this->keyManager->getVersion($this->realPath);
|
$version = $this->keyManager->getVersion($this->realPath, new View());
|
||||||
|
|
||||||
if (!empty($fileKey)) {
|
if (!empty($fileKey)) {
|
||||||
|
|
||||||
|
@ -392,7 +393,7 @@ class Encryption implements IEncryptionModule {
|
||||||
|
|
||||||
$this->keyManager->setAllFileKeys($path, $encryptedFileKey);
|
$this->keyManager->setAllFileKeys($path, $encryptedFileKey);
|
||||||
|
|
||||||
$this->keyManager->setVersion($path, $version);
|
$this->keyManager->setVersion($path, $version, new View());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted',
|
$this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted',
|
||||||
|
|
|
@ -418,10 +418,10 @@ class KeyManager {
|
||||||
* Get the current version of a file
|
* Get the current version of a file
|
||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
* @param View $view
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getVersion($path) {
|
public function getVersion($path, View $view) {
|
||||||
$view = new \OC\Files\View();
|
|
||||||
$fileInfo = $view->getFileInfo($path);
|
$fileInfo = $view->getFileInfo($path);
|
||||||
if($fileInfo === false) {
|
if($fileInfo === false) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -433,19 +433,15 @@ class KeyManager {
|
||||||
* Set the current version of a file
|
* Set the current version of a file
|
||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param string $version
|
* @param int $version
|
||||||
|
* @param View $view
|
||||||
*/
|
*/
|
||||||
public function setVersion($path, $version) {
|
public function setVersion($path, $version, View $view) {
|
||||||
$view = new \OC\Files\View();
|
|
||||||
$fileInfo= $view->getFileInfo($path);
|
$fileInfo= $view->getFileInfo($path);
|
||||||
|
|
||||||
if($fileInfo !== false) {
|
if($fileInfo !== false) {
|
||||||
$fileId = $fileInfo->getId();
|
$cache = $fileInfo->getStorage()->getCache();
|
||||||
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
|
$cache->put($path, ['fileid' => $fileInfo->getId(), 'encrypted' => $version, 'encryptedVersion' => $version]);
|
||||||
$qb->update('filecache')
|
|
||||||
->set('encrypted', $qb->createNamedParameter($version))
|
|
||||||
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId)))
|
|
||||||
->execute();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -579,4 +579,31 @@ class KeyManagerTest extends TestCase {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetVersionWithoutFileInfo() {
|
||||||
|
$view = $this->getMockBuilder('\\OC\\Files\\View')
|
||||||
|
->disableOriginalConstructor()->getMock();
|
||||||
|
$view->expects($this->once())
|
||||||
|
->method('getFileInfo')
|
||||||
|
->with('/admin/files/myfile.txt')
|
||||||
|
->willReturn(false);
|
||||||
|
|
||||||
|
$this->assertSame(0, $this->instance->getVersion('/admin/files/myfile.txt', $view));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetVersionWithFileInfo() {
|
||||||
|
$view = $this->getMockBuilder('\\OC\\Files\\View')
|
||||||
|
->disableOriginalConstructor()->getMock();
|
||||||
|
$fileInfo = $this->getMockBuilder('\\OC\\Files\\FileInfo')
|
||||||
|
->disableOriginalConstructor()->getMock();
|
||||||
|
$fileInfo->expects($this->once())
|
||||||
|
->method('getEncryptedVersion')
|
||||||
|
->willReturn(1337);
|
||||||
|
$view->expects($this->once())
|
||||||
|
->method('getFileInfo')
|
||||||
|
->with('/admin/files/myfile.txt')
|
||||||
|
->willReturn($fileInfo);
|
||||||
|
|
||||||
|
$this->assertSame(1337, $this->instance->getVersion('/admin/files/myfile.txt', $view));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue