Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate space #10024

Merged
merged 11 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog/unreleased/enhancement-duplicate-space
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Enhancement: Duplicate space

We've added a new functionality, where users can simply duplicate spaces, via the context menu or batch actions.
This includes copying the contents, the space name, subtitle, description, and image but not metadata
like tags or members.

https://github.com/owncloud/web/pull/10024
https://github.com/owncloud/web/issues/10016
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { useCapabilitySpacesMaxQuota, useStore, usePreviewService } from '@owncl
import {
useSpaceActionsDelete,
useSpaceActionsDisable,
useSpaceActionsDuplicate,
useSpaceActionsEditDescription,
useSpaceActionsEditQuota,
useSpaceActionsEditReadmeContent,
Expand All @@ -50,6 +51,7 @@ import {
} from '@ownclouders/web-pkg'
import { useSpaceActionsUploadImage } from 'web-app-files/src/composables'
import { useFileActionsDownloadArchive } from '@ownclouders/web-pkg'

export default defineComponent({
name: 'SpaceActions',
components: { ActionMenuItem, QuotaModal, ReadmeContentModal },
Expand All @@ -68,6 +70,7 @@ export default defineComponent({

const { actions: deleteActions } = useSpaceActionsDelete({ store })
const { actions: disableActions } = useSpaceActionsDisable({ store })
const { actions: duplicateActions } = useSpaceActionsDuplicate({ store })
const { actions: editDescriptionActions } = useSpaceActionsEditDescription({ store })
const {
actions: editQuotaActions,
Expand All @@ -91,6 +94,7 @@ export default defineComponent({
[
...unref(downloadArchiveActions),
...unref(renameActions),
...unref(duplicateActions),
...unref(editDescriptionActions),
...unref(uploadImageActions),
...unref(editReadmeContentActions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useSpaceActionsUploadImage } from 'web-app-files/src/composables'
import {
useSpaceActionsDelete,
useSpaceActionsDisable,
useSpaceActionsDuplicate,
useSpaceActionsEditDescription,
useSpaceActionsEditQuota,
useSpaceActionsEditReadmeContent,
Expand Down Expand Up @@ -76,6 +77,7 @@ export default defineComponent({

const { actions: deleteActions } = useSpaceActionsDelete({ store })
const { actions: disableActions } = useSpaceActionsDisable({ store })
const { actions: duplicateActions } = useSpaceActionsDuplicate({ store })
const {
actions: editQuotaActions,
modalOpen: quotaModalIsOpen,
Expand Down Expand Up @@ -108,6 +110,7 @@ export default defineComponent({
const menuItemsPrimaryActions = computed(() => {
const fileHandlers = [
...unref(renameActions),
...unref(duplicateActions),
...unref(editDescriptionActions),
...unref(uploadImageActions)
]
Expand Down
5 changes: 4 additions & 1 deletion packages/web-pkg/src/components/AppBar/AppBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ import {
useFileActionsDownloadFile,
useFileActionsEmptyTrashBin,
useFileActionsMove,
useFileActionsRestore
useFileActionsRestore,
useSpaceActionsDuplicate
} from '../../composables/actions'
import {
useAbility,
Expand Down Expand Up @@ -171,6 +172,7 @@ export default defineComponent({
const { actions: acceptShareActions } = useFileActionsAcceptShare({ store })
const { actions: hideShareActions } = useFileActionsToggleHideShare({ store })
const { actions: copyActions } = useFileActionsCopy({ store })
const { actions: duplicateActions } = useSpaceActionsDuplicate({ store })
const { actions: declineShareActions } = useFileActionsDeclineShare({ store })
const { actions: deleteActions } = useFileActionsDelete({ store })
const { actions: downloadArchiveActions } = useFileActionsDownloadArchive({ store })
Expand Down Expand Up @@ -215,6 +217,7 @@ export default defineComponent({
if (!isSearchLocation.value) {
actions = [
...actions,
...unref(duplicateActions),
...unref(editSpaceQuotaActions),
...unref(restoreSpaceActions),
...unref(deleteSpaceActions),
Expand Down
1 change: 1 addition & 0 deletions packages/web-pkg/src/composables/actions/spaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './useSpaceActionsDelete'
export * from './useSpaceActionsDisable'
export * from './useSpaceActionsDuplicate'
export * from './useSpaceActionsEditDescription'
export * from './useSpaceActionsEditQuota'
export * from './useSpaceActionsEditReadmeContent'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { SpaceResource } from '@ownclouders/web-client'
import { computed } from 'vue'
import { SpaceAction, SpaceActionOptions } from '../types'
import { useGettext } from 'vue3-gettext'
import { useStore } from '../../store'
import { useAbility } from '../../ability'
import { useClientService } from '../../clientService'
import { useLoadingService } from '../../loadingService'
import { Store } from 'vuex'
import { buildSpace, isProjectSpaceResource } from '@ownclouders/web-client/src/helpers'
import { Drive } from '@ownclouders/web-client/src/generated'
import { resolveFileNameDuplicate } from '../../../helpers'
import PQueue from 'p-queue'

export const useSpaceActionsDuplicate = ({
store
}: {
store?: Store<any>
} = {}) => {
store = store || useStore()
const { $gettext } = useGettext()
const ability = useAbility()
const clientService = useClientService()
const loadingService = useLoadingService()

const duplicateSpace = async (existingSpace: SpaceResource) => {
const projectSpaces: SpaceResource[] = store.getters['runtime/spaces/spaces'].filter(
(space: SpaceResource) => isProjectSpaceResource(space)
)
JammingBen marked this conversation as resolved.
Show resolved Hide resolved
const duplicatedSpaceName = resolveFileNameDuplicate(existingSpace.name, '', projectSpaces)

try {
const { data: createdSpace } = await clientService.graphAuthenticated.drives.createDrive(
{
name: duplicatedSpaceName,
description: existingSpace.description
},
{}
)
let duplicatedSpace = buildSpace(createdSpace)

const existingSpaceFiles = await clientService.webdav.listFiles(existingSpace)

if (existingSpaceFiles.children.length) {
const queue = new PQueue({ concurrency: 4 })
const copyOps = []

for (const file of existingSpaceFiles.children) {
copyOps.push(
queue.add(() =>
clientService.webdav.copyFiles(existingSpace, file, duplicatedSpace, {
path: file.name
})
)
)
}
await Promise.all(copyOps)
}

if (existingSpace.spaceReadmeData || existingSpace.spaceImageData) {
const specialRequestData = {
special: []
}

if (existingSpace.spaceReadmeData) {
const newSpaceReadmeFile = await clientService.webdav.getFileInfo(duplicatedSpace, {
path: `.space/${existingSpace.spaceReadmeData.name}`
})
specialRequestData.special.push({
specialFolder: {
name: 'readme'
},
id: newSpaceReadmeFile.id
})
}

if (existingSpace.spaceImageData) {
const newSpaceImageFile = await clientService.webdav.getFileInfo(duplicatedSpace, {
path: `.space/${existingSpace.spaceImageData.name}`
})
specialRequestData.special.push({
specialFolder: {
name: 'image'
},
id: newSpaceImageFile.id
})
}

const { data: updatedDriveData } =
await clientService.graphAuthenticated.drives.updateDrive(
duplicatedSpace.id.toString(),
specialRequestData as Drive,
{}
)
duplicatedSpace = buildSpace(updatedDriveData)
}

store.commit('runtime/spaces/UPSERT_SPACE', duplicatedSpace)
store.dispatch('showMessage', {
title: $gettext('Space "%{space}" was duplicated successfully', {
space: existingSpace.name
})
})
} catch (error) {
console.error(error)
store.dispatch('showErrorMessage', {
title: $gettext('Failed to duplicate space "%{space}"', { space: existingSpace.name }),
error
})
}
}

const handler = async ({ resources }: SpaceActionOptions) => {
for (const resource of resources) {
if (resource.disabled || !isProjectSpaceResource(resource)) {
continue
}
await duplicateSpace(resource)
}
}

const actions = computed((): SpaceAction[] => [
{
name: 'duplicate',
icon: 'folders',
label: () => $gettext('Duplicate'),
handler: (args) => loadingService.addTask(() => handler(args)),
isEnabled: ({ resources }) => {
if (!resources?.length) {
return false
}

if (resources.every((resource) => resource.disabled)) {
return false
}

if (resources.every((resource) => !isProjectSpaceResource(resource))) {
return false
}

return ability.can('create-all', 'Drive')
},
componentType: 'button',
class: 'oc-files-actions-duplicate-trigger'
}
])

return {
actions,
duplicateSpace
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import {
SpaceResource
} from '@ownclouders/web-client/src/helpers'

export const resolveFileNameDuplicate = (name, extension, existingFiles, iteration = 1) => {
export const resolveFileNameDuplicate = (
name: string,
extension: string,
existingResources: Resource[],
iteration = 1
) => {
let potentialName
if (extension.length === 0) {
if (!extension) {
potentialName = `${name} (${iteration})`
} else {
const nameWithoutExtension = extractNameWithoutExtension({ name, extension } as Resource)
potentialName = `${nameWithoutExtension} (${iteration}).${extension}`
}
const hasConflict = existingFiles.some((f) => f.name === potentialName)
const hasConflict = existingResources.some((f) => f.name === potentialName)
if (!hasConflict) {
return potentialName
}
return resolveFileNameDuplicate(name, extension, existingFiles, iteration + 1)
return resolveFileNameDuplicate(name, extension, existingResources, iteration + 1)
}

export const isResourceBeeingMovedToSameLocation = (
Expand Down