Support getting and patching version-label

Signed-off-by: Louis Chemineau <louis@chmn.me>
pull/35160/head
Louis Chemineau 3 years ago committed by Louis (Rebase PR Action)
parent e82bfba114
commit 629de6c8c9
  1. 3
      apps/files_versions/composer/composer/autoload_classmap.php
  2. 3
      apps/files_versions/composer/composer/autoload_static.php
  3. 4
      apps/files_versions/lib/AppInfo/Application.php
  4. 17
      apps/files_versions/lib/Capabilities.php
  5. 11
      apps/files_versions/lib/Db/VersionEntity.php
  6. 49
      apps/files_versions/lib/Hooks.php
  7. 30
      apps/files_versions/lib/Sabre/Plugin.php
  8. 20
      apps/files_versions/lib/Sabre/VersionFile.php
  9. 22
      apps/files_versions/lib/Versions/INameableVersion.php
  10. 36
      apps/files_versions/lib/Versions/INameableVersionBackend.php
  11. 100
      apps/files_versions/lib/Versions/LegacyVersionsBackend.php
  12. 12
      apps/files_versions/lib/Versions/Version.php
  13. 9
      apps/files_versions/lib/Versions/VersionManager.php
  14. 302
      apps/files_versions/src/components/Version.vue
  15. 2
      apps/files_versions/src/files_versions_tab.js
  16. 1
      apps/files_versions/src/utils/davRequest.js
  17. 89
      apps/files_versions/src/utils/versions.js
  18. 197
      apps/files_versions/src/views/VersionTab.vue

@ -31,6 +31,9 @@ return array(
'OCA\\Files_Versions\\Sabre\\VersionRoot' => $baseDir . '/../lib/Sabre/VersionRoot.php', 'OCA\\Files_Versions\\Sabre\\VersionRoot' => $baseDir . '/../lib/Sabre/VersionRoot.php',
'OCA\\Files_Versions\\Storage' => $baseDir . '/../lib/Storage.php', 'OCA\\Files_Versions\\Storage' => $baseDir . '/../lib/Storage.php',
'OCA\\Files_Versions\\Versions\\BackendNotFoundException' => $baseDir . '/../lib/Versions/BackendNotFoundException.php', 'OCA\\Files_Versions\\Versions\\BackendNotFoundException' => $baseDir . '/../lib/Versions/BackendNotFoundException.php',
'OCA\\Files_Versions\\Versions\\IDeletableVersionBackend' => $baseDir . '/../lib/Versions/IDeletableVersionBackend.php',
'OCA\\Files_Versions\\Versions\\INameableVersion' => $baseDir . '/../lib/Versions/INameableVersion.php',
'OCA\\Files_Versions\\Versions\\INameableVersionBackend' => $baseDir . '/../lib/Versions/INameableVersionBackend.php',
'OCA\\Files_Versions\\Versions\\IVersion' => $baseDir . '/../lib/Versions/IVersion.php', 'OCA\\Files_Versions\\Versions\\IVersion' => $baseDir . '/../lib/Versions/IVersion.php',
'OCA\\Files_Versions\\Versions\\IVersionBackend' => $baseDir . '/../lib/Versions/IVersionBackend.php', 'OCA\\Files_Versions\\Versions\\IVersionBackend' => $baseDir . '/../lib/Versions/IVersionBackend.php',
'OCA\\Files_Versions\\Versions\\IVersionManager' => $baseDir . '/../lib/Versions/IVersionManager.php', 'OCA\\Files_Versions\\Versions\\IVersionManager' => $baseDir . '/../lib/Versions/IVersionManager.php',

@ -46,6 +46,9 @@ class ComposerStaticInitFiles_Versions
'OCA\\Files_Versions\\Sabre\\VersionRoot' => __DIR__ . '/..' . '/../lib/Sabre/VersionRoot.php', 'OCA\\Files_Versions\\Sabre\\VersionRoot' => __DIR__ . '/..' . '/../lib/Sabre/VersionRoot.php',
'OCA\\Files_Versions\\Storage' => __DIR__ . '/..' . '/../lib/Storage.php', 'OCA\\Files_Versions\\Storage' => __DIR__ . '/..' . '/../lib/Storage.php',
'OCA\\Files_Versions\\Versions\\BackendNotFoundException' => __DIR__ . '/..' . '/../lib/Versions/BackendNotFoundException.php', 'OCA\\Files_Versions\\Versions\\BackendNotFoundException' => __DIR__ . '/..' . '/../lib/Versions/BackendNotFoundException.php',
'OCA\\Files_Versions\\Versions\\IDeletableVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/IDeletableVersionBackend.php',
'OCA\\Files_Versions\\Versions\\INameableVersion' => __DIR__ . '/..' . '/../lib/Versions/INameableVersion.php',
'OCA\\Files_Versions\\Versions\\INameableVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/INameableVersionBackend.php',
'OCA\\Files_Versions\\Versions\\IVersion' => __DIR__ . '/..' . '/../lib/Versions/IVersion.php', 'OCA\\Files_Versions\\Versions\\IVersion' => __DIR__ . '/..' . '/../lib/Versions/IVersion.php',
'OCA\\Files_Versions\\Versions\\IVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/IVersionBackend.php', 'OCA\\Files_Versions\\Versions\\IVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/IVersionBackend.php',
'OCA\\Files_Versions\\Versions\\IVersionManager' => __DIR__ . '/..' . '/../lib/Versions/IVersionManager.php', 'OCA\\Files_Versions\\Versions\\IVersionManager' => __DIR__ . '/..' . '/../lib/Versions/IVersionManager.php',

@ -47,10 +47,11 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent; use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent; use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent; use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Files\Events\Node\NodeCopiedEvent; use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Events\Node\NodeDeletedEvent; use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent; use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IServerContainer; use OCP\IServerContainer;
@ -105,6 +106,7 @@ class Application extends App implements IBootstrap {
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class); $context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
$context->registerEventListener(BeforeNodeWrittenEvent::class, Hooks::class); $context->registerEventListener(BeforeNodeWrittenEvent::class, Hooks::class);
$context->registerEventListener(NodeWrittenEvent::class, Hooks::class);
$context->registerEventListener(BeforeNodeDeletedEvent::class, Hooks::class); $context->registerEventListener(BeforeNodeDeletedEvent::class, Hooks::class);
$context->registerEventListener(NodeDeletedEvent::class, Hooks::class); $context->registerEventListener(NodeDeletedEvent::class, Hooks::class);
$context->registerEventListener(NodeRenamedEvent::class, Hooks::class); $context->registerEventListener(NodeRenamedEvent::class, Hooks::class);

@ -24,9 +24,21 @@
*/ */
namespace OCA\Files_Versions; namespace OCA\Files_Versions;
use OCP\App\IAppManager;
use OCP\Capabilities\ICapability; use OCP\Capabilities\ICapability;
use OCP\IConfig;
class Capabilities implements ICapability { class Capabilities implements ICapability {
private IConfig $config;
private IAppManager $appManager;
public function __construct(
IConfig $config,
IAppManager $appManager
) {
$this->config = $config;
$this->appManager = $appManager;
}
/** /**
* Return this classes capabilities * Return this classes capabilities
@ -34,9 +46,12 @@ class Capabilities implements ICapability {
* @return array * @return array
*/ */
public function getCapabilities() { public function getCapabilities() {
$groupFolderOrS3VersioningInstalled = $this->appManager->isInstalled('groupfolders') || !$this->appManager->isInstalled('groupfolders');
return [ return [
'files' => [ 'files' => [
'versioning' => true 'versioning' => true,
'version_labeling' => !$groupFolderOrS3VersioningInstalled && $this->config->getSystemValueBool('enable_version_labeling', true),
] ]
]; ];
} }

@ -59,7 +59,7 @@ class VersionEntity extends Entity implements JsonSerializable {
$this->addType('metadata', Types::JSON); $this->addType('metadata', Types::JSON);
} }
public function jsonSerialize() { public function jsonSerialize(): array {
return [ return [
'id' => $this->id, 'id' => $this->id,
'file_id' => $this->fileId, 'file_id' => $this->fileId,
@ -69,4 +69,13 @@ class VersionEntity extends Entity implements JsonSerializable {
'metadata' => $this->metadata, 'metadata' => $this->metadata,
]; ];
} }
public function getLabel(): string {
return $this->metadata['label'] ?? '';
}
public function setLabel(string $label): void {
$this->metadata['label'] = $label;
$this->markFieldUpdated('metadata');
}
} }

@ -32,6 +32,8 @@ namespace OCA\Files_Versions;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Files\Mount\MoveableMount; use OC\Files\Mount\MoveableMount;
use OC\Files\View; use OC\Files\View;
use OCA\Files_Versions\Db\VersionEntity;
use OCA\Files_Versions\Db\VersionsMapper;
use OCP\EventDispatcher\Event; use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener; use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent; use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
@ -41,16 +43,28 @@ use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Files\Events\Node\NodeCopiedEvent; use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Events\Node\NodeDeletedEvent; use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent; use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\Node; use OCP\Files\Node;
class Hooks implements IEventListener { class Hooks implements IEventListener {
public Folder $userFolder; private Folder $userFolder;
private VersionsMapper $versionsMapper;
/**
* @var array<int, bool>
*/
private array $versionsCreated = [];
private IMimeTypeLoader $mimeTypeLoader;
public function __construct( public function __construct(
Folder $userFolder Folder $userFolder,
VersionsMapper $versionsMapper,
IMimeTypeLoader $mimeTypeLoader
) { ) {
$this->userFolder = $userFolder; $this->userFolder = $userFolder;
$this->versionsMapper = $versionsMapper;
$this->mimeTypeLoader = $mimeTypeLoader;
} }
public function handle(Event $event): void { public function handle(Event $event): void {
@ -58,6 +72,10 @@ class Hooks implements IEventListener {
$this->write_hook($event->getNode()); $this->write_hook($event->getNode());
} }
if ($event instanceof NodeWrittenEvent) {
$this->post_write_hook($event->getNode());
}
if ($event instanceof BeforeNodeDeletedEvent) { if ($event instanceof BeforeNodeDeletedEvent) {
$this->pre_remove_hook($event->getNode()); $this->pre_remove_hook($event->getNode());
} }
@ -88,9 +106,34 @@ class Hooks implements IEventListener {
*/ */
public function write_hook(Node $node): void { public function write_hook(Node $node): void {
$path = $this->userFolder->getRelativePath($node->getPath()); $path = $this->userFolder->getRelativePath($node->getPath());
Storage::store($path); $result = Storage::store($path);
if ($result === false) {
return;
}
// Store the result of the version creation so it can be used in post_write_hook.
$this->versionsCreated[$node->getId()] = true;
} }
/**
* listen to post_write event.
*/
public function post_write_hook(Node $node): void {
if (!array_key_exists($node->getId(), $this->versionsCreated)) {
return;
}
unset($this->versionsCreated[$node->getId()]);
$versionEntity = new VersionEntity();
$versionEntity->setFileId($node->getId());
$versionEntity->setTimestamp($node->getMTime());
$versionEntity->setSize($node->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($node->getMimetype()));
$versionEntity->setMetadata([]);
$this->versionsMapper->insert($versionEntity);
}
/** /**
* Erase versions of deleted file * Erase versions of deleted file

@ -29,19 +29,23 @@ namespace OCA\Files_Versions\Sabre;
use OC\AppFramework\Http\Request; use OC\AppFramework\Http\Request;
use OCP\IRequest; use OCP\IRequest;
use Sabre\DAV\Exception\NotFound; use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
use Sabre\DAV\Server; use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin; use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface; use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface; use Sabre\HTTP\ResponseInterface;
class Plugin extends ServerPlugin { class Plugin extends ServerPlugin {
private Server $server;
private IRequest $request;
/** @var Server */ public const VERSION_LABEL = '{http://nextcloud.org/ns}version-label';
private $server;
/** @var IRequest */
private $request;
public function __construct(IRequest $request) { public function __construct(
IRequest $request
) {
$this->request = $request; $this->request = $request;
} }
@ -49,6 +53,8 @@ class Plugin extends ServerPlugin {
$this->server = $server; $this->server = $server;
$server->on('afterMethod:GET', [$this, 'afterGet']); $server->on('afterMethod:GET', [$this, 'afterGet']);
$server->on('propFind', [$this, 'propFind']);
$server->on('propPatch', [$this, 'propPatch']);
} }
public function afterGet(RequestInterface $request, ResponseInterface $response) { public function afterGet(RequestInterface $request, ResponseInterface $response) {
@ -81,4 +87,18 @@ class Plugin extends ServerPlugin {
. '; filename="' . rawurlencode($filename) . '"'); . '; filename="' . rawurlencode($filename) . '"');
} }
} }
public function propFind(PropFind $propFind, INode $node): void {
if ($node instanceof VersionFile) {
$propFind->handle(self::VERSION_LABEL, fn() => $node->getLabel());
}
}
public function propPatch($path, PropPatch $propPatch): void {
$node = $this->server->tree->getNodeForPath($path);
if ($node instanceof VersionFile) {
$propPatch->handle(self::VERSION_LABEL, fn ($label) => $node->setLabel($label));
}
}
} }

@ -26,6 +26,8 @@ declare(strict_types=1);
*/ */
namespace OCA\Files_Versions\Sabre; namespace OCA\Files_Versions\Sabre;
use OCA\Files_Versions\Versions\INameableVersion;
use OCA\Files_Versions\Versions\INameableVersionBackend;
use OCA\Files_Versions\Versions\IVersion; use OCA\Files_Versions\Versions\IVersion;
use OCA\Files_Versions\Versions\IVersionManager; use OCA\Files_Versions\Versions\IVersionManager;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
@ -70,6 +72,7 @@ class VersionFile implements IFile {
} }
public function delete() { public function delete() {
// TODO: implement version deletion
throw new Forbidden(); throw new Forbidden();
} }
@ -81,6 +84,23 @@ class VersionFile implements IFile {
throw new Forbidden(); throw new Forbidden();
} }
public function getLabel(): ?string {
if ($this->version instanceof INameableVersion) {
return $this->version->getLabel();
} else {
return null;
}
}
public function setLabel($label): bool {
if ($this->versionManager instanceof INameableVersionBackend) {
$this->versionManager->setVersionLabel($this->version, $label);
return true;
} else {
return false;
}
}
public function getLastModified(): int { public function getLastModified(): int {
return $this->version->getTimestamp(); return $this->version->getTimestamp();
} }

@ -1,7 +1,9 @@
<?php
declare(strict_types=1);
/** /**
* @copyright 2022 Carl Schwan <carl@carlschwan.eu> * @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
*
* @author Carl Schwan <carl@carlschwan.eu>
* *
* @license GNU AGPL version 3 or any later version * @license GNU AGPL version 3 or any later version
* *
@ -19,3 +21,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
namespace OCA\Files_Versions\Versions;
/**
* @since 26.0.0
*/
interface INameableVersion {
/**
* Get the user created label
*
* @return string
* @since 26.0.0
*/
public function getLabel(): string;
}

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_Versions\Versions;
/**
* @since 26.0.0
*/
interface INameableVersionBackend {
/**
* Set the label for a version.
*
* @since 26.0.0
*/
public function setVersionLabel(IVersion $version, string $label): void;
}

@ -28,25 +28,36 @@ namespace OCA\Files_Versions\Versions;
use OC\Files\View; use OC\Files\View;
use OCA\Files_Sharing\SharedStorage; use OCA\Files_Sharing\SharedStorage;
use OCA\Files_Versions\Db\VersionEntity;
use OCA\Files_Versions\Db\VersionsMapper;
use OCA\Files_Versions\Storage; use OCA\Files_Versions\Storage;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\FileInfo; use OCP\Files\FileInfo;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IStorage;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
class LegacyVersionsBackend implements IVersionBackend { class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend, IDeletableVersionBackend {
/** @var IRootFolder */ private IRootFolder $rootFolder;
private $rootFolder; private IUserManager $userManager;
/** @var IUserManager */ private VersionsMapper $versionsMapper;
private $userManager; private IMimeTypeLoader $mimeTypeLoader;
public function __construct(IRootFolder $rootFolder, IUserManager $userManager) { public function __construct(
IRootFolder $rootFolder,
IUserManager $userManager,
VersionsMapper $versionsMapper,
IMimeTypeLoader $mimeTypeLoader
) {
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->versionsMapper = $versionsMapper;
$this->mimeTypeLoader = $mimeTypeLoader;
} }
public function useBackendForStorage(IStorage $storage): bool { public function useBackendForStorage(IStorage $storage): bool {
@ -63,21 +74,60 @@ class LegacyVersionsBackend implements IVersionBackend {
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); $userFolder = $this->rootFolder->getUserFolder($user->getUID());
$nodes = $userFolder->getById($file->getId()); $nodes = $userFolder->getById($file->getId());
$file2 = array_pop($nodes); $file2 = array_pop($nodes);
$versions = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file2->getPath()));
$versions = $this->getVersionsForFileFromDB($file2, $user);
return array_map(function (array $data) use ($file, $user) {
return new Version( if (count($versions) > 0) {
(int)$data['version'], return $versions;
(int)$data['version'], }
$data['name'],
(int)$data['size'], // Insert the entry in the DB for the current version.
$data['mimetype'], if ($file2->getSize() > 0) {
$data['path'], $versionEntity = new VersionEntity();
$versionEntity->setFileId($file2->getId());
$versionEntity->setTimestamp($file2->getMTime());
$versionEntity->setSize($file2->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file2->getMimetype()));
$versionEntity->setMetadata([]);
$this->versionsMapper->insert($versionEntity);
}
// Insert entries in the DB for existing versions.
$versionsOnFS = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file2->getPath()));
foreach ($versionsOnFS as $version) {
$versionEntity = new VersionEntity();
$versionEntity->setFileId($file2->getId());
$versionEntity->setTimestamp((int)$version['version']);
$versionEntity->setSize((int)$version['size']);
$versionEntity->setMimetype($this->mimeTypeLoader->getId($version['mimetype']));
$versionEntity->setMetadata([]);
$this->versionsMapper->insert($versionEntity);
}
return $this->getVersionsForFileFromDB($file2, $user);
}
/**
* @return IVersion[]
*/
private function getVersionsForFileFromDB(Node $file, IUser $user): array {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
return array_map(
fn (VersionEntity $versionEntity) => new Version(
$versionEntity->getTimestamp(),
$versionEntity->getTimestamp(),
$file->getName(),
$versionEntity->getSize(),
$this->mimeTypeLoader->getMimetypeById($versionEntity->getMimetype()),
$userFolder->getRelativePath($file->getPath()),
$file, $file,
$this, $this,
$user $user,
$versionEntity->getLabel(),
),
$this->versionsMapper->findAllVersionsForFileId($file->getId())
); );
}, $versions);
} }
public function createVersion(IUser $user, FileInfo $file) { public function createVersion(IUser $user, FileInfo $file) {
@ -125,4 +175,16 @@ class LegacyVersionsBackend implements IVersionBackend {
$file = $versionFolder->get($userFolder->getRelativePath($sourceFile->getPath()) . '.v' . $revision); $file = $versionFolder->get($userFolder->getRelativePath($sourceFile->getPath()) . '.v' . $revision);
return $file; return $file;
} }
public function setVersionLabel(IVersion $version, string $label): void {
$versionEntity = $this->versionsMapper->findVersionForFileId(
$version->getSourceFile()->getId(),
$version->getTimestamp(),
);
if (trim($label) === '') {
$label = null;
}
$versionEntity->setLabel($label ?? '');
$this->versionsMapper->update($versionEntity);
}
} }

@ -28,7 +28,7 @@ namespace OCA\Files_Versions\Versions;
use OCP\Files\FileInfo; use OCP\Files\FileInfo;
use OCP\IUser; use OCP\IUser;
class Version implements IVersion { class Version implements IVersion, INameableVersion {
/** @var int */ /** @var int */
private $timestamp; private $timestamp;
@ -38,6 +38,8 @@ class Version implements IVersion {
/** @var string */ /** @var string */
private $name; private $name;
private string $label;
/** @var int */ /** @var int */
private $size; private $size;
@ -65,11 +67,13 @@ class Version implements IVersion {
string $path, string $path,
FileInfo $sourceFileInfo, FileInfo $sourceFileInfo,
IVersionBackend $backend, IVersionBackend $backend,
IUser $user IUser $user,
string $label = ''
) { ) {
$this->timestamp = $timestamp; $this->timestamp = $timestamp;
$this->revisionId = $revisionId; $this->revisionId = $revisionId;
$this->name = $name; $this->name = $name;
$this->label = $label;
$this->size = $size; $this->size = $size;
$this->mimetype = $mimetype; $this->mimetype = $mimetype;
$this->path = $path; $this->path = $path;
@ -102,6 +106,10 @@ class Version implements IVersion {
return $this->name; return $this->name;
} }
public function getLabel(): string {
return $this->label;
}
public function getMimeType(): string { public function getMimeType(): string {
return $this->mimetype; return $this->mimetype;
} }

@ -30,7 +30,7 @@ use OCP\Files\FileInfo;
use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IStorage;
use OCP\IUser; use OCP\IUser;
class VersionManager implements IVersionManager { class VersionManager implements IVersionManager, INameableVersionBackend {
/** @var (IVersionBackend[])[] */ /** @var (IVersionBackend[])[] */
private $backends = []; private $backends = [];
@ -110,4 +110,11 @@ class VersionManager implements IVersionManager {
public function useBackendForStorage(IStorage $storage): bool { public function useBackendForStorage(IStorage $storage): bool {
return false; return false;
} }
public function setVersionLabel(IVersion $version, string $label): void {
$backend = $this->getBackendForStorage($version->getSourceFile()->getStorage());
if ($backend instanceof INameableVersionBackend) {
$backend->setVersionLabel($version, $label);
}
}
} }

@ -0,0 +1,302 @@
<!--
- @copyright 2022 Carl Schwan <carl@carlschwan.eu>
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- 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
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<div>
<NcListItem class="version"
:title="versionLabel"
:href="downloadURL"
:force-display-actions="true">
<template #icon>
<img lazy="true"
:src="previewURL"
alt=""
height="256"
width="256"
class="version__image">
</template>
<template #subtitle>
<div class="version__info">
<span v-tooltip="formattedDate">{{ version.mtime | humanDateFromNow }}</span>
<!-- Separate dot to improve alignement -->
<span class="version__info__size"></span>
<span class="version__info__size">{{ version.size | humanReadableSize }}</span>
</div>
</template>
<template #actions>
<NcActionButton v-if="capabilities.files.version_labeling === true"
:close-after-click="true"
@click="openVersionLabelModal">
<template #icon>
<Pencil :size="22" />
</template>
{{ version.label === '' ? t('files_versions', 'Name this version') : t('files_versions', 'Edit version name') }}
</NcActionButton>
<NcActionButton v-if="!isCurrent"
:close-after-click="true"
@click="restoreVersion">
<template #icon>
<BackupRestore :size="22" />
</template>
{{ t('files_versions', 'Restore version') }}
</NcActionButton>
<NcActionLink :href="downloadURL"
:close-after-click="true"
:download="downloadURL">
<template #icon>
<Download :size="22" />
</template>
{{ t('files_versions', 'Download version') }}
</NcActionLink>
<NcActionButton v-if="!isCurrent"
:close-after-click="true"
@click="deleteVersion">
<template #icon>
<Delete :size="22" />
</template>
{{ t('files_versions', 'Delete version') }}
</NcActionButton>
</template>
</NcListItem>
<NcModal v-if="showVersionLabelForm"
:title="t('files_versions', 'Name this version')"
@close="showVersionLabelForm = false">
<form class="version-label-modal"
@submit.prevent="setVersionLabel(formVersionLabelValue)">
<label>
<div class="version-label-modal__title">{{ t('photos', 'Version name') }}</div>
<NcTextField ref="labelInput"
:value.sync="formVersionLabelValue"
:placeholder="t('photos', 'Version name')"
:label-outside="true" />
</label>
<div class="version-label-modal__info">
{{ t('photos', 'Named versions are persisted, and excluded from automatic cleanups when your storage quota is full.') }}
</div>
<div class="version-label-modal__actions">
<NcButton :disabled="formVersionLabelValue.trim().length === 0" @click="setVersionLabel('')">
{{ t('files_versions', 'Remove version name') }}
</NcButton>
<NcButton type="primary" native-type="submit">
<template #icon>
<Check />
</template>
{{ t('files_versions', 'Save version name') }}
</NcButton>
</div>
</form>
</NcModal>
</div>
</template>
<script>
import BackupRestore from 'vue-material-design-icons/BackupRestore.vue'
import Download from 'vue-material-design-icons/Download.vue'
import Pencil from 'vue-material-design-icons/Pencil.vue'
import Check from 'vue-material-design-icons/Check.vue'
import Delete from 'vue-material-design-icons/Delete'
import { NcActionButton, NcActionLink, NcListItem, NcModal, NcButton, NcTextField, Tooltip } from '@nextcloud/vue'
import moment from '@nextcloud/moment'
import { translate } from '@nextcloud/l10n'
import { joinPaths } from '@nextcloud/paths'
import { generateUrl } from '@nextcloud/router'
import { loadState } from '@nextcloud/initial-state'
export default {
name: 'Version',
components: {
NcActionLink,
NcActionButton,
NcListItem,
NcModal,
NcButton,
NcTextField,
BackupRestore,
Download,
Pencil,
Check,
Delete,
},
directives: {
tooltip: Tooltip,
},
filters: {
/**
* @param {number} bytes
* @return {string}
*/
humanReadableSize(bytes) {
return OC.Util.humanFileSize(bytes)
},
/**
* @param {number} timestamp
* @return {string}
*/
humanDateFromNow(timestamp) {
return moment(timestamp).fromNow()
},
},
props: {
/** @type {Vue.PropOptions<import('../utils/versions.js').Version>} */
version: {
type: Object,
required: true,
},
fileInfo: {
type: Object,
required: true,
},
isCurrent: {
type: Boolean,
default: false,
},
isFirstVersion: {
type: Boolean,
default: false,
},
},
data() {
return {
showVersionLabelForm: false,
formVersionLabelValue: this.version.label,
capabilities: loadState('core', 'capabilities', { files: { version_labeling: false } }),
}
},
computed: {
/**
* @return {string}
*/
versionLabel() {
if (this.isCurrent) {
if (this.version.label === '') {
return translate('files_versions', 'Current version')
} else {
return `${this.version.label} (${translate('files_versions', 'Current version')})`
}
}
if (this.isFirstVersion && this.version.label === '') {
return translate('files_versions', 'Initial version')
}
return this.version.label
},
/**
* @return {string}
*/
downloadURL() {
if (this.isCurrent) {
return joinPaths('/remote.php/webdav', this.fileInfo.path, this.fileInfo.name)
} else {
return this.version.url
}
},
/**
* @return {string}
*/
previewURL() {
if (this.isCurrent) {
return generateUrl('/core/preview?fileId={fileId}&c={fileEtag}&x=250&y=250&forceIcon=0&a=0', {
fileId: this.fileInfo.id,
fileEtag: this.fileInfo.etag,
})
} else {
return this.version.preview
}
},
},
methods: {
openVersionLabelModal() {
this.showVersionLabelForm = true
this.$nextTick(() => {
this.$refs.labelInput.$el.getElementsByTagName('input')[0].focus()
})
},
restoreVersion() {
this.$emit('restore', this.version)
},
setVersionLabel(label) {
this.formVersionLabelValue = label
this.showVersionLabelForm = false
this.$emit('label-update', this.version, label)
},
deleteVersion() {
this.$emit('delete', this.version)
},
formattedDate() {
return moment(this.version.mtime)
},
},
}
</script>
<style scoped lang="scss">
.version {
display: flex;
flex-direction: row;
&__info {
display: flex;
flex-direction: row;
align-items: center;
gap: 0.5rem;
&__size {
color: var(--color-text-lighter);
}
}
&__image {
width: 3rem;
height: 3rem;
border: 1px solid var(--color-border);
border-radius: var(--border-radius-large);
}
}
.version-label-modal {
display: flex;
justify-content: space-between;
flex-direction: column;
height: 250px;
padding: 16px;
&__title {
margin-bottom: 12px;
font-weight: 600;
}
&__info {
margin-top: 12px;
color: var(--color-text-maxcontrast);
}
&__actions {
display: flex;
justify-content: space-between;
margin-top: 64px;
}
}
</style>

@ -41,7 +41,7 @@ window.addEventListener('DOMContentLoaded', function() {
OCA.Files.Sidebar.registerTab(new OCA.Files.Sidebar.Tab({ OCA.Files.Sidebar.registerTab(new OCA.Files.Sidebar.Tab({
id: 'version_vue', id: 'version_vue',
name: t('files_versions', 'Version'), name: t('files_versions', 'Versions'),
iconSvg: BackupRestore, iconSvg: BackupRestore,
async mount(el, fileInfo, context) { async mount(el, fileInfo, context) {

@ -29,5 +29,6 @@ export default `<?xml version="1.0"?>
<d:getcontentlength /> <d:getcontentlength />
<d:getcontenttype /> <d:getcontenttype />
<d:getlastmodified /> <d:getlastmodified />
<nc:version-label />
</d:prop> </d:prop>
</d:propfind>` </d:propfind>`

@ -23,14 +23,14 @@ import { getCurrentUser } from '@nextcloud/auth'
import client from '../utils/davClient.js' import client from '../utils/davClient.js'
import davRequest from '../utils/davRequest.js' import davRequest from '../utils/davRequest.js'
import logger from '../utils/logger.js' import logger from '../utils/logger.js'
import { basename, joinPaths } from '@nextcloud/paths' import { joinPaths } from '@nextcloud/paths'
import { generateUrl } from '@nextcloud/router' import { generateUrl } from '@nextcloud/router'
import { translate } from '@nextcloud/l10n'
import moment from '@nextcloud/moment' import moment from '@nextcloud/moment'
/** /**
* @typedef {object} Version * @typedef {object} Version
* @property {string} title - 'Current version' or '' * @property {string} fileId - The id of the file associated to the version.
* @property {string} label - 'Current version' or ''
* @property {string} fileName - File name relative to the version DAV endpoint * @property {string} fileName - File name relative to the version DAV endpoint
* @property {string} mimeType - Empty for the current version, else the actual mime type of the version * @property {string} mimeType - Empty for the current version, else the actual mime type of the version
* @property {string} size - Human readable size * @property {string} size - Human readable size
@ -39,7 +39,6 @@ import moment from '@nextcloud/moment'
* @property {string} preview - Preview URL of the version * @property {string} preview - Preview URL of the version
* @property {string} url - Download URL of the version * @property {string} url - Download URL of the version
* @property {string|null} fileVersion - The version id, null for the current version * @property {string|null} fileVersion - The version id, null for the current version
* @property {boolean} isCurrent - Whether this is the current version of the file
*/ */
/** /**
@ -50,11 +49,15 @@ export async function fetchVersions(fileInfo) {
const path = `/versions/${getCurrentUser()?.uid}/versions/${fileInfo.id}` const path = `/versions/${getCurrentUser()?.uid}/versions/${fileInfo.id}`
try { try {
/** @type {import('webdav').FileStat[]} */ /** @type {import('webdav').ResponseDataDetailed<import('webdav').FileStat[]>} */
const response = await client.getDirectoryContents(path, { const response = await client.getDirectoryContents(path, {
data: davRequest, data: davRequest,
details: true,
}) })
return response.map(version => formatVersion(version, fileInfo)) return response.data
// Filter out root
.filter(({ mime }) => mime !== '')
.map(version => formatVersion(version, fileInfo))
} catch (exception) { } catch (exception) {
logger.error('Could not fetch version', { exception }) logger.error('Could not fetch version', { exception })
throw exception throw exception
@ -65,13 +68,12 @@ export async function fetchVersions(fileInfo) {
* Restore the given version * Restore the given version
* *
* @param {Version} version * @param {Version} version
* @param {object} fileInfo
*/ */
export async function restoreVersion(version, fileInfo) { export async function restoreVersion(version) {
try { try {
logger.debug('Restoring version', { url: version.url }) logger.debug('Restoring version', { url: version.url })
await client.moveFile( await client.moveFile(
`/versions/${getCurrentUser()?.uid}/versions/${fileInfo.id}/${version.fileVersion}`, `/versions/${getCurrentUser()?.uid}/versions/${version.fileId}/${version.fileVersion}`,
`/versions/${getCurrentUser()?.uid}/restore/target` `/versions/${getCurrentUser()?.uid}/restore/target`
) )
} catch (exception) { } catch (exception) {
@ -88,37 +90,50 @@ export async function restoreVersion(version, fileInfo) {
* @return {Version} * @return {Version}
*/ */
function formatVersion(version, fileInfo) { function formatVersion(version, fileInfo) {
const isCurrent = version.mime === ''
const fileVersion = isCurrent ? null : basename(version.filename)
let url = null
let preview = null
if (isCurrent) {
// https://nextcloud_server2.test/remote.php/webdav/welcome.txt?downloadStartSecret=hl5awd7tbzg
url = joinPaths('/remote.php/webdav', fileInfo.path, fileInfo.name)
preview = generateUrl('/core/preview?fileId={fileId}&c={fileEtag}&x=250&y=250&forceIcon=0&a=0', {
fileId: fileInfo.id,
fileEtag: fileInfo.etag,
})
} else {
url = joinPaths('/remote.php/dav', version.filename)
preview = generateUrl('/apps/files_versions/preview?file={file}&version={fileVersion}', {
file: joinPaths(fileInfo.path, fileInfo.name),
fileVersion,
})
}
return { return {
title: isCurrent ? translate('files_versions', 'Current version') : '', fileId: fileInfo.id,
label: version.props['version-label'],
fileName: version.filename, fileName: version.filename,
mimeType: version.mime, mimeType: version.mime,
size: isCurrent ? fileInfo.size : version.size, size: version.size,
type: version.type, type: version.type,
mtime: moment(isCurrent ? fileInfo.mtime : version.lastmod).unix(), mtime: moment(version.lastmod).unix() * 1000,
preview, preview: generateUrl('/apps/files_versions/preview?file={file}&version={fileVersion}', {
url, file: joinPaths(fileInfo.path, fileInfo.name),
fileVersion, fileVersion: version.basename,
isCurrent, }),
url: joinPaths('/remote.php/dav', version.filename),
fileVersion: version.basename,
}
} }
/**
* @param {Version} version
* @param {string} newLabel
*/
export async function setVersionLabel(version, newLabel) {
return await client.customRequest(
version.fileName,
{
method: 'PROPPATCH',
data: `<?xml version="1.0"?>
<d:propertyupdate xmlns:d="DAV:"
xmlns:oc="http://owncloud.org/ns"
xmlns:nc="http://nextcloud.org/ns"
xmlns:ocs="http://open-collaboration-services.org/ns">
<d:set>
<d:prop>
<nc:version-label>${newLabel}</nc:version-label>
</d:prop>
</d:set>
</d:propertyupdate>`,
}
)
}
/**
* @param {Version} version
*/
export async function deleteVersion(version) {
await client.deleteFile(version.fileName)
} }

@ -16,84 +16,28 @@
- along with this program. If not, see <http://www.gnu.org/licenses/>. - along with this program. If not, see <http://www.gnu.org/licenses/>.
--> -->
<template> <template>
<div>
<ul> <ul>
<NcListItem v-for="version in versions" <Version v-for="version in orderedVersions"
:key="version.mtime" :key="version.mtime"
class="version" :version="version"
:title="version.title" :file-info="fileInfo"
:href="version.url"> :is-current="version.mtime === fileInfo.mtime"
<template #icon> :is-first-version="version.mtime === initialVersionMtime"
<img lazy="true" @restore="handleRestore"
:src="version.preview" @label-update="handleLabelUpdate"
alt="" @delete="handleDelete" />
height="256"
width="256"
class="version__image">
</template>
<template #subtitle>
<div class="version__info">
<span>{{ version.mtime | humanDateFromNow }}</span>
<!-- Separate dot to improve alignement -->
<span class="version__info__size"></span>
<span class="version__info__size">{{ version.size | humanReadableSize }}</span>
</div>
</template>
<template v-if="!version.isCurrent" #actions>
<NcActionLink :href="version.url"
:download="version.url">
<template #icon>
<Download :size="22" />
</template>
{{ t('files_versions', 'Download version') }}
</NcActionLink>
<NcActionButton @click="restoreVersion(version)">
<template #icon>
<BackupRestore :size="22" />
</template>
{{ t('files_versions', 'Restore version') }}
</NcActionButton>
</template>
</NcListItem>
<NcEmptyContent v-if="!loading && versions.length === 1"
:title="t('files_version', 'No versions yet')">
<!-- length === 1, since we don't want to show versions if there is only the current file -->
<template #icon>
<BackupRestore />
</template>
</NcEmptyContent>
</ul> </ul>
</div>
</template> </template>
<script> <script>
import BackupRestore from 'vue-material-design-icons/BackupRestore.vue'
import Download from 'vue-material-design-icons/Download.vue'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcActionLink from '@nextcloud/vue/dist/Components/NcActionLink.js'
import NcListItem from '@nextcloud/vue/dist/Components/NcListItem.js'
import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
import { showError, showSuccess } from '@nextcloud/dialogs' import { showError, showSuccess } from '@nextcloud/dialogs'
import { fetchVersions, restoreVersion } from '../utils/versions.js' import { fetchVersions, deleteVersion, restoreVersion, setVersionLabel } from '../utils/versions.js'
import moment from '@nextcloud/moment' import Version from '../components/Version.vue'
export default { export default {
name: 'VersionTab', name: 'VersionTab',
components: { components: {
NcEmptyContent, Version,
NcActionLink,
NcActionButton,
NcListItem,
BackupRestore,
Download,
},
filters: {
humanReadableSize(bytes) {
return OC.Util.humanFileSize(bytes)
},
humanDateFromNow(timestamp) {
return moment(timestamp * 1000).fromNow()
},
}, },
data() { data() {
return { return {
@ -103,6 +47,35 @@ export default {
loading: false, loading: false,
} }
}, },
computed: {
/**
* Order versions by mtime.
* Put the current version at the top.
*
* @return {import('../utils/versions.js').Version[]}
*/
orderedVersions() {
return [...this.versions].sort((a, b) => {
if (a.mtime === this.fileInfo.mtime) {
return -1
} else if (b.mtime === this.fileInfo.mtime) {
return 1
} else {
return b.mtime - a.mtime
}
})
},
/**
* Return the mtime of the first version to display "Initial version" label
* @return {number}
*/
initialVersionMtime() {
return this.versions
.map(version => version.mtime)
.reduce((a, b) => Math.min(a, b))
},
},
methods: { methods: {
/** /**
* Update current fileInfo and fetch new data * Update current fileInfo and fetch new data
@ -128,55 +101,77 @@ export default {
}, },
/** /**
* Restore the given version * Handle restored event from Version.vue
* *
* @param version * @param {import('../utils/versions.js').Version} version
*/ */
async restoreVersion(version) { async handleRestore(version) {
// Update local copy of fileInfo as rendering depends on it.
const oldFileInfo = this.fileInfo
this.fileInfo = {
...this.fileInfo,
size: version.size,
mtime: version.mtime,
}
try { try {
await restoreVersion(version, this.fileInfo) await restoreVersion(version)
// File info is not updated so we manually update its size and mtime if the restoration went fine. if (version.label !== '') {
this.fileInfo.size = version.size showSuccess(t('files_versions', `${version.label} restored`))
this.fileInfo.mtime = version.lastmod } else if (version.mtime === this.initialVersionMtime) {
showSuccess(t('files_versions', 'Initial version restored'))
} else {
showSuccess(t('files_versions', 'Version restored')) showSuccess(t('files_versions', 'Version restored'))
}
await this.fetchVersions() await this.fetchVersions()
} catch (exception) { } catch (exception) {
this.fileInfo = oldFileInfo
showError(t('files_versions', 'Could not restore version')) showError(t('files_versions', 'Could not restore version'))
} }
}, },
/** /**
* Reset the current view to its default state * Handle label-updated event from Version.vue
*
* @param {import('../utils/versions.js').Version} version
* @param {string} newName
*/ */
resetState() { async handleLabelUpdate(version, newName) {
this.versions = [] const oldLabel = version.label
}, version.label = newName
},
}
</script>
<style scopped lang="scss"> try {
.version { await setVersionLabel(version, newName)
display: flex; } catch (exception) {
flex-direction: row; version.label = oldLabel
showError(t('files_versions', 'Could not set version name'))
}
},
&__info { /**
display: flex; * Handle deleted event from Version.vue
flex-direction: row; *
align-items: center; * @param {import('../utils/versions.js').Version} version
gap: 0.5rem; * @param {string} newName
*/
async handleDelete(version) {
const index = this.versions.indexOf(version)
this.versions.splice(index, 1)
&__size { try {
color: var(--color-text-lighter); await deleteVersion(version)
} } catch (exception) {
this.versions.push(version)
showError(t('files_versions', 'Could not delete version'))
} }
},
&__image { /**
width: 3rem; * Reset the current view to its default state
height: 3rem; */
border: 1px solid var(--color-border); resetState() {
margin-right: 1rem; this.$set(this, 'versions', [])
border-radius: var(--border-radius-large); },
} },
} }
</style> </script>

Loading…
Cancel
Save