Skip to content

Commit

Permalink
Minimalistic implementation of versions integration - needs change in…
Browse files Browse the repository at this point in the history
… the future
  • Loading branch information
DeepDiver1975 committed Oct 19, 2017
1 parent df65671 commit 9da7bf9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/files_versions/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ public static function getVersions($uid, $filename, $userFullPath = '') {
$versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
$versions[$key]['name'] = $versionedFile;
$versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
$versions[$key]['storage_location'] = "$dir/$entryName";
}
}
}
Expand Down
69 changes: 59 additions & 10 deletions lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
use OC\Files\Cache\Updater;
use OC\Files\Filesystem;
use OC\Files\Cache\Watcher;
use OCP\Constants;
use OCP\Files\FileInfo;
use OCP\Files\FileNameTooLongException;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException;
use OCP\Files\ReservedWordException;
use OCP\Files\Storage\ILockingStorage;
use OCP\Files\Storage\IVersionedStorage;
use OCP\Lock\ILockingProvider;

/**
Expand All @@ -62,7 +65,7 @@
* Some \OC\Files\Storage\Common methods call functions which are first defined
* in classes which extend it, e.g. $this->stat() .
*/
abstract class Common implements Storage, ILockingStorage {
abstract class Common implements Storage, ILockingStorage, IVersionedStorage {

use LocalTempFileTrait;

Expand Down Expand Up @@ -151,19 +154,19 @@ public function isSharable($path) {
public function getPermissions($path) {
$permissions = 0;
if ($this->isCreatable($path)) {
$permissions |= \OCP\Constants::PERMISSION_CREATE;
$permissions |= Constants::PERMISSION_CREATE;
}
if ($this->isReadable($path)) {
$permissions |= \OCP\Constants::PERMISSION_READ;
$permissions |= Constants::PERMISSION_READ;
}
if ($this->isUpdatable($path)) {
$permissions |= \OCP\Constants::PERMISSION_UPDATE;
$permissions |= Constants::PERMISSION_UPDATE;
}
if ($this->isDeletable($path)) {
$permissions |= \OCP\Constants::PERMISSION_DELETE;
$permissions |= Constants::PERMISSION_DELETE;
}
if ($this->isSharable($path)) {
$permissions |= \OCP\Constants::PERMISSION_SHARE;
$permissions |= Constants::PERMISSION_SHARE;
}
return $permissions;
}
Expand Down Expand Up @@ -259,7 +262,7 @@ private function addLocalFolder($path, $target) {
$dh = $this->opendir($path);
if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
if (!Filesystem::isIgnoredDir($file)) {
if ($this->is_dir($path . '/' . $file)) {
mkdir($target . '/' . $file);
$this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
Expand All @@ -282,7 +285,7 @@ protected function searchInDir($query, $dir = '') {
$dh = $this->opendir($dir);
if (is_resource($dh)) {
while (($item = readdir($dh)) !== false) {
if (\OC\Files\Filesystem::isIgnoredDir($item)) continue;
if (Filesystem::isIgnoredDir($item)) continue;
if (strstr(strtolower($item), strtolower($query)) !== false) {
$files[] = $dir . '/' . $item;
}
Expand Down Expand Up @@ -446,7 +449,7 @@ public function test() {
* @return int|false
*/
public function free_space($path) {
return \OCP\Files\FileInfo::SPACE_UNKNOWN;
return FileInfo::SPACE_UNKNOWN;
}

/**
Expand Down Expand Up @@ -620,7 +623,7 @@ public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceIntern
*/
public function getMetaData($path) {
$permissions = $this->getPermissions($path);
if (!$permissions & \OCP\Constants::PERMISSION_READ) {
if (!$permissions & Constants::PERMISSION_READ) {
//can't read, nothing we can do
return null;
}
Expand Down Expand Up @@ -684,4 +687,50 @@ public function getAvailability() {
public function setAvailability($isAvailable) {
$this->getStorageCache()->setAvailability($isAvailable);
}
public function getVersions($internalPath) {
// KISS implementation
if (!\OC_App::isEnabled('files_versions')) {
return [];
}
$p = $this->convertInternalPathToGlobalPath($internalPath);

return array_values(
\OCA\Files_Versions\Storage::getVersions($this->getOwner($internalPath), $p));
}

public function getVersion($internalPath, $versionId) {
$versions = $this->getVersions($internalPath);
$versions = array_filter($versions, function ($version) use($versionId){
return $version['version'] === $versionId;
});
return array_shift($versions);
}

public function getContentOfVersion($internalPath, $versionId) {
$v = $this->getVersion($internalPath, $versionId);
return $this->file_get_contents($v['storage_location']);
}

public function restoreVersion($internalPath, $versionId) {
// KISS implementation
if (!\OC_App::isEnabled('files_versions')) {
return;
}
$p = $this->convertInternalPathToGlobalPath($internalPath);
\OCA\Files_Versions\Storage::rollback($p, $versionId);
}

/**
* @param $internalPath
* @return array|string
*/
private function convertInternalPathToGlobalPath($internalPath) {
$mount = \OC::$server->getMountManager()->findByStorageId($this->getId());
$p = $mount[0]->getMountPoint() . $internalPath;
$p = explode('/', ltrim($p, '/'));
array_shift($p);
array_shift($p);
$p = implode('/', $p);
return $p;
}
}
6 changes: 3 additions & 3 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* for local filestore, we only have to map the paths
*/
class Local extends \OC\Files\Storage\Common {
class Local extends Common {
protected $datadir;

protected $dataDirLength;
Expand Down Expand Up @@ -412,15 +412,15 @@ public function getETag($path) {
* @param string $targetInternalPath
* @return bool
*/
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')) {
/**
* @var \OC\Files\Storage\Local $sourceStorage
*/
$rootStorage = new Local(['datadir' => '/']);
return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
} else {
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime);
}
}

Expand Down

0 comments on commit 9da7bf9

Please sign in to comment.