Skip to content

Commit

Permalink
feat(files_versions): Support moving versions across storages
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Mar 26, 2024
1 parent d3a1f37 commit d7f7e07
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
build
vendor
js/
cypress/downloads/
66 changes: 58 additions & 8 deletions cypress/e2e/files/filesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,83 @@ export const triggerActionForFile = (filename: string, actionId: string) => {
cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).should('exist').click()
}

export const moveFile = (fileName: string, dirName: string) => {
export const moveFile = (fileName: string, dirPath: string) => {
getRowForFile(fileName).should('be.visible')
triggerActionForFile(fileName, 'move-copy')

cy.get('.file-picker').within(() => {
// intercept the copy so we can wait for it
cy.intercept('MOVE', /\/remote.php\/dav\/files\//).as('moveFile')

if (dirName === '/') {
if (dirPath === '/') {
// select home folder
cy.get('button[title="Home"]').should('be.visible').click()
// click move
cy.contains('button', 'Move').should('be.visible').click()
} else if (dirName === '.') {
} else if (dirPath === '.') {
// click move
cy.contains('button', 'Copy').should('be.visible').click()
} else {
// select the folder
cy.get(`[data-filename="${dirName}"]`).should('be.visible').click()
const directories = dirPath.split('/')
directories.forEach((directory) => {
// select the folder
cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
})

// click move
cy.contains('button', `Move to ${dirName}`).should('be.visible').click()
cy.contains('button', `Move to ${directories.at(-1)}`).should('be.visible').click()
}

cy.wait('@moveFile')
})
}

export const navigateToFolder = (folderName: string) => {
getRowForFile(folderName).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
export const copyFile = (fileName: string, dirPath: string) => {
getRowForFile(fileName).should('be.visible')
triggerActionForFile(fileName, 'move-copy')

cy.get('.file-picker').within(() => {
// intercept the copy so we can wait for it
cy.intercept('COPY', /\/remote.php\/dav\/files\//).as('copyFile')

if (dirPath === '/') {
// select home folder
cy.get('button[title="Home"]').should('be.visible').click()
// click copy
cy.contains('button', 'Copy').should('be.visible').click()
} else if (dirPath === '.') {
// click copy
cy.contains('button', 'Copy').should('be.visible').click()
} else {
const directories = dirPath.split('/')
directories.forEach((directory) => {
// select the folder
cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
})

// click copy
cy.contains('button', `Copy to ${directories.at(-1)}`).should('be.visible').click()
}

cy.wait('@copyFile')
})
}

export const navigateToFolder = (dirPath: string) => {
const directories = dirPath.split('/')
directories.forEach((directory) => {
getRowForFile(directory).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
})

}

export const closeSidebar = () => {
// {force: true} as it might be hidden behind toasts
cy.get('[cy-data-sidebar] .app-sidebar__close').click({ force: true })
}

export const clickOnBreadcumbs = (label: string) => {
cy.intercept('PROPFIND', /\/remote.php\/dav\//).as('propfind')
cy.get('[data-cy-files-content-breadcrumbs]').contains(label).click()
cy.wait('@propfind')
}
11 changes: 4 additions & 7 deletions cypress/e2e/files_versions/filesVersionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ export function doesNotHaveAction(index: number, actionName: string) {
toggleVersionMenu(index)
}

export function assertVersionContent(filename: string, index: number, expectedContent: string) {
const downloadsFolder = Cypress.config('downloadsFolder')

export function assertVersionContent(index: number, expectedContent: string) {
cy.intercept({ method: 'GET', times: 1, url: 'remote.php/**' }).as('downloadVersion')
triggerVersionAction(index, 'download')

return cy.readFile(path.join(downloadsFolder, filename))
.then((versionContent) => expect(versionContent).to.equal(expectedContent))
.then(() => cy.exec(`rm ${downloadsFolder}/${filename}`))
cy.wait('@downloadVersion')
.then(({ response }) => expect(response?.body).to.equal(expectedContent))
}
145 changes: 145 additions & 0 deletions cypress/e2e/files_versions/version_cross_storage_move.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
*
* @author Louis Chmn <louis@chmn.me>
*
* @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/>.
*
*/

import type { User } from '@nextcloud/cypress'

import { PERMISSION_DELETE, PERMISSION_READ, PERMISSION_WRITE, addUserToGroup, createGroup, createGroupFolder } from '../groupfoldersUtils'
import { assertVersionContent, nameVersion, openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils'
import { clickOnBreadcumbs, closeSidebar, copyFile, moveFile, navigateToFolder } from '../files/filesUtils'

/**
*
* @param filePath
*/
function assertVersionsContent(filePath: string) {
const path = filePath.split('/').slice(0, -1).join('/')

clickOnBreadcumbs('All files')

if (path !== '') {
navigateToFolder(path)
}

closeSidebar()
openVersionsPanel(filePath)

cy.get('[data-files-versions-version]').should('have.length', 3)
cy.get('[data-files-versions-version]').eq(2).contains('v1')
assertVersionContent(0, 'v3')
assertVersionContent(1, 'v2')
assertVersionContent(2, 'v1')
}

describe('Versions cross storage move', () => {
let randomGroupName: string
let randomGroupFolderName: string
let randomFileName: string
let randomCopiedFileName: string
let user: User

before(() => {
randomGroupName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
randomGroupFolderName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)

cy.createRandomUser().then(_user => { user = _user })
createGroup(randomGroupName)

cy.then(() => {
addUserToGroup(randomGroupName, user.userId)
createGroupFolder(randomGroupFolderName, randomGroupName, [PERMISSION_READ, PERMISSION_WRITE, PERMISSION_DELETE])
})
})

beforeEach(() => {
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
randomFileName = randomString + '.txt'
randomCopiedFileName = randomString + ' (copy).txt'
uploadThreeVersions(user, `${randomGroupFolderName}/${randomFileName}`)

cy.login(user)
cy.visit('/apps/files')
navigateToFolder(randomGroupFolderName)
openVersionsPanel(`${randomGroupFolderName}/${randomFileName}`)
nameVersion(2, 'v1')
})

it('Correctly moves versions to the user\'s FS when the user moves the file out of the groupfolder', () => {
moveFile(randomFileName, '/')

assertVersionsContent(randomFileName)

moveFile(randomFileName, randomGroupFolderName)

assertVersionsContent(`${randomGroupFolderName}/${randomFileName}`)
})

it('Correctly copies versions to the user\'s FS when the user copies the file out of the groupfolder', () => {
copyFile(randomFileName, '/')

assertVersionsContent(randomFileName)

copyFile(randomFileName, randomGroupFolderName)

assertVersionsContent(`${randomGroupFolderName}/${randomCopiedFileName}`)
})

context('When a file is in a subfolder', () => {
let randomSubFolderName
let randomCopiedSubFolderName
let randomSubSubFolderName

beforeEach(() => {
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
randomSubFolderName = randomString
randomCopiedSubFolderName = randomString + ' (copy)'

randomSubSubFolderName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
clickOnBreadcumbs('All files')
cy.mkdir(user, `/${randomGroupFolderName}/${randomSubFolderName}`)
cy.mkdir(user, `/${randomGroupFolderName}/${randomSubFolderName}/${randomSubSubFolderName}`)
cy.login(user)
navigateToFolder(randomGroupFolderName)
})

it('Correctly moves versions when user moves the containing folder out of the groupfolder', () => {
moveFile(randomFileName, `${randomSubFolderName}/${randomSubSubFolderName}`)
moveFile(randomSubFolderName, '/')

assertVersionsContent(`${randomSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)

clickOnBreadcumbs('All files')
moveFile(randomSubFolderName, randomGroupFolderName)
assertVersionsContent(`${randomGroupFolderName}/${randomSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)
})

it('Correctly copies versions when user copies the containing folder out of the groupfolder', () => {
moveFile(randomFileName, `${randomSubFolderName}/${randomSubSubFolderName}`)
copyFile(randomSubFolderName, '/')

assertVersionsContent(`${randomSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)

clickOnBreadcumbs('All files')
copyFile(randomSubFolderName, randomGroupFolderName)
assertVersionsContent(`${randomGroupFolderName}/${randomCopiedSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)
})
})
})
6 changes: 3 additions & 3 deletions cypress/e2e/files_versions/version_download.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('Versions download', () => {
})

it('Download versions and assert their content', () => {
assertVersionContent(randomFileName, 0, 'v3')
assertVersionContent(randomFileName, 1, 'v2')
assertVersionContent(randomFileName, 2, 'v1')
assertVersionContent(0, 'v3')
assertVersionContent(1, 'v2')
assertVersionContent(2, 'v1')
})
})
6 changes: 3 additions & 3 deletions cypress/e2e/files_versions/version_restoration.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ describe('Versions restoration', () => {
})

it('Downloads versions and assert there content', () => {
assertVersionContent(randomFileName, 0, 'v1')
assertVersionContent(randomFileName, 1, 'v3')
assertVersionContent(randomFileName, 2, 'v2')
assertVersionContent(0, 'v1')
assertVersionContent(1, 'v3')
assertVersionContent(2, 'v2')
})
})
64 changes: 58 additions & 6 deletions lib/Versions/VersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\Files_Versions\Versions\IDeletableVersionBackend;
use OCA\Files_Versions\Versions\IMetadataVersion;
use OCA\Files_Versions\Versions\IMetadataVersionBackend;
use OCA\Files_Versions\Versions\INeedSyncVersionBackend;
use OCA\Files_Versions\Versions\IVersion;
use OCA\Files_Versions\Versions\IVersionBackend;
use OCA\Files_Versions\Versions\IVersionsImporterBackend;
use OCA\GroupFolders\Mount\GroupMountPoint;
use OCA\GroupFolders\Mount\MountProvider;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -45,7 +47,7 @@
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

class VersionsBackend implements IVersionBackend, IMetadataVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend {
class VersionsBackend implements IVersionBackend, IMetadataVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend, IVersionsImporterBackend {
public function __construct(
private IRootFolder $rootFolder,
private Folder $appFolder,
Expand Down Expand Up @@ -137,15 +139,12 @@ private function getVersionsForFileFromDB(FileInfo $fileInfo, IUser $user, int $
$mountPoint = $fileInfo->getMountPoint();
/** @var Folder $versionsFolder */
$versionsFolder = $this->getVersionsFolder($folderId)->get((string)$fileInfo->getId());
/** @var Folder */
$folder = $this->appFolder->get((string)$folderId);
$file = $folder->get($fileInfo->getInternalPath());

$versionEntities = $this->groupVersionsMapper->findAllVersionsForFileId($fileInfo->getId());
$mappedVersions = array_map(
function (GroupVersionEntity $versionEntity) use ($versionsFolder, $mountPoint, $file, $fileInfo, $user, $folderId) {
function (GroupVersionEntity $versionEntity) use ($versionsFolder, $mountPoint, $fileInfo, $user, $folderId) {
if ($fileInfo->getMtime() === $versionEntity->getTimestamp()) {
$versionFile = $file;
$versionFile = $fileInfo;
} else {
try {
$versionFile = $versionsFolder->get((string)$versionEntity->getTimestamp());
Expand Down Expand Up @@ -382,4 +381,57 @@ private function currentUserHasPermissions(FileInfo $sourceFile, int $permission

return ($sourceFile->getPermissions() & $permissions) === $permissions;
}

/**
* @inheritdoc
*/
public function importVersionsForFile(IUser $user, Node $source, Node $target, array $versions): void {

Check failure on line 388 in lib/Versions/VersionsBackend.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MethodSignatureMismatch

lib/Versions/VersionsBackend.php:388:87: MethodSignatureMismatch: Argument 4 of OCA\GroupFolders\Versions\VersionsBackend::importVersionsForFile has wrong type 'array<array-key, mixed>', expecting 'array<array-key, OCA\Files_Versions\Versions\IVersion>' as defined by OCA\Files_Versions\Versions\IVersionsImporterBackend::importVersionsForFile (see https://psalm.dev/042)
$mount = $target->getMountPoint();
if (!($mount instanceof GroupMountPoint)) {
return;
}

$folderId = $mount->getFolderId();
$versionsFolder = $this->getVersionsFolder($folderId);

try {
/** @var Folder $versionFolder */
$versionFolder = $versionsFolder->get((string)$target->getId());
} catch (NotFoundException $e) {
$versionFolder = $versionsFolder->newFolder((string)$target->getId());
}

foreach ($versions as $version) {
// 1. Move the file to the new location
if ($version->getTimestamp() !== $source->getMTime()) {
$backend = $version->getBackend();
$versionFile = $backend->getVersionFile($user, $source, $version->getRevisionId());
$versionFolder->newFile($version->getRevisionId(), $versionFile->fopen('r'));
}

// 2. Create the entity in the database
$versionEntity = new GroupVersionEntity();
$versionEntity->setFileId($target->getId());
$versionEntity->setTimestamp($version->getTimestamp());
$versionEntity->setSize($version->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($version->getMimetype()));
if ($version instanceof IMetadataVersion) {
$versionEntity->setDecodedMetadata($version->getMetadata());
}
$this->groupVersionsMapper->insert($versionEntity);
}
}

/**
* @inheritdoc
*/
public function clearVersionsForFile(IUser $user, Node $source, Node $target): void {
$mount = $source->getParent()->getMountPoint();
if (!($mount instanceof GroupMountPoint)) {
return;
}

$folderId = $mount->getFolderId();
$this->deleteAllVersionsForFile($folderId, $target->getId());
}
}
Loading

0 comments on commit d7f7e07

Please sign in to comment.