Skip to content

Commit

Permalink
Refactor into RestoreFileFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
lookacat committed Oct 20, 2022
1 parent 23b2e23 commit 97465d0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
9 changes: 1 addition & 8 deletions packages/web-app-files/src/mixins/actions/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,12 @@ export default {
const restorePromises = []
const restoreQueue = new PQueue({ concurrency: 4 })
resources.forEach((resource) => {
const hasShareJail = this.capabilities?.spaces?.share_jail === true
const path = hasShareJail
? buildWebDavSpacesTrashPath(this.space.id)
: buildWebDavFilesTrashPath(this.user.id)
const restorePath = hasShareJail
? buildWebDavSpacesPath(this.space.id, resource.path)
: buildWebDavFilesPath(this.user.id, resource.path)
const overwrite = filesToOverwrite.includes(resource)

restorePromises.push(
restoreQueue.add(async () => {
try {
await this.$client.fileTrash.restore(path, resource.id, restorePath, overwrite)
await this.$clientService.webdav.restoreFile(this.space, this.user.id, resource.id, resource.path, { overwrite })
restoredResources.push(resource)
} catch (e) {
console.error(e)
Expand Down
2 changes: 1 addition & 1 deletion packages/web-client/src/helpers/space/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildWebDavSpacesPath, extractDomSelector, Resource } from '../resource
import { SpacePeopleShareRoles, spaceRoleEditor, spaceRoleManager, spaceRoleViewer } from '../share'
import { PublicSpaceResource, ShareSpaceResource, SpaceResource, SHARE_JAIL_ID } from './types'
import { DavProperty } from 'web-pkg/src/constants'
import { buildWebDavPublicPath } from 'files/src/helpers/resources'
import { buildWebDavPublicPath, buildWebDavSpacesTrashPath } from 'files/src/helpers/resources'
import { urlJoin } from 'web-pkg/src/utils'
import { extractNodeId } from 'files/src/helpers/resource'

Expand Down
1 change: 1 addition & 0 deletions packages/web-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type OwnCloudSdk = {
}
fileTrash: {
list(...args): any
restore(...args): any
}
publicFiles: {
createFolder(...args): any
Expand Down
3 changes: 3 additions & 0 deletions packages/web-client/src/webdav/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ListFilesFactory } from './listFiles'
import { MoveFilesFactory } from './moveFiles'
import { PutFileContentsFactory } from './putFileContents'
import { DeleteFileFactory } from './deleteFile'
import { RestoreFileFactory } from './restoreFile'

export * from './types'

Expand All @@ -29,11 +30,13 @@ export const webdav = (options: WebDavOptions): WebDAV => {
const { moveFiles } = MoveFilesFactory(options)

const { deleteFile } = DeleteFileFactory(options)
const { restoreFile } = RestoreFileFactory(options)

return {
copyFiles,
createFolder,
deleteFile,
restoreFile,
getFileContents,
getFileInfo,
getFileUrl,
Expand Down
28 changes: 28 additions & 0 deletions packages/web-client/src/webdav/restoreFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { buildWebDavFilesPath, buildWebDavFilesTrashPath, buildWebDavSpacesTrashPath } from 'files/src/helpers/resources'
import { urlJoin } from 'web-pkg/src/utils'
import { buildWebDavSpacesPath, FileResource, isPublicSpaceResource, isShareSpaceResource, Resource, SpaceResource } from '../helpers'
import { WebDavOptions } from './types'

export const RestoreFileFactory = ({ sdk }: WebDavOptions) => {
return {
async restoreFile(
space: SpaceResource,
userId: string | number,
id: string | number,
restorePath: string,
{ overwrite }: { overwrite?: boolean },
): Promise<FileResource> {
if (isPublicSpaceResource(space)) {
return
}
debugger;
const path = isShareSpaceResource(space)
? buildWebDavSpacesTrashPath(space.id)
: buildWebDavFilesTrashPath(userId)
const restorePathBuilt = isShareSpaceResource(space)
? buildWebDavSpacesPath(space.id, restorePath)
: buildWebDavFilesPath(userId, restorePath)
return sdk.fileTrash.restore(path, id, restorePathBuilt, overwrite)
}
}
}
2 changes: 2 additions & 0 deletions packages/web-client/src/webdav/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PutFileContentsFactory } from './putFileContents'
import { CopyFilesFactory } from './copyFiles'
import { MoveFilesFactory } from './moveFiles'
import { DeleteFileFactory } from './deleteFile'
import { RestoreFileFactory } from './restoreFile'

export interface WebDavOptions {
sdk: OwnCloudSdk
Expand All @@ -24,4 +25,5 @@ export interface WebDAV {
copyFiles: ReturnType<typeof CopyFilesFactory>['copyFiles']
moveFiles: ReturnType<typeof MoveFilesFactory>['moveFiles']
deleteFile: ReturnType<typeof DeleteFileFactory>['deleteFile']
restoreFile: ReturnType<typeof RestoreFileFactory>['restoreFile']
}
1 change: 1 addition & 0 deletions packages/web-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const renderSuccess = (): void => {
driveType: 'personal',
name: instance.$gettext('All files'),
webDavPath: `/files/${user.id}`,
webDavTrashPath: `/files/trash-bin/${user.id}`,
serverUrl: configurationManager.serverUrl
})
const personalHomeInfo = await (clientService.webdav as WebDAV).getFileInfo(
Expand Down

0 comments on commit 97465d0

Please sign in to comment.