Skip to content

Commit

Permalink
Merge pull request #9728 from owncloud/use-webdav-abstraction
Browse files Browse the repository at this point in the history
refactor: consistenly use webdav abstraction layer
  • Loading branch information
JammingBen authored Sep 28, 2023
2 parents 59cdb9f + f07f687 commit 79fdf4a
Show file tree
Hide file tree
Showing 32 changed files with 371 additions and 268 deletions.
9 changes: 7 additions & 2 deletions packages/web-app-files/src/HandleUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { locationPublicLink } from 'web-pkg/src/router/public'
import { locationSpacesGeneric } from 'web-pkg/src/router/spaces'
import { isPersonalSpaceResource, isShareSpaceResource } from 'web-client/src/helpers'
import { ClientService } from 'web-pkg/types'
import { queryItemAsString } from 'web-pkg/src/composables'

export interface HandleUploadOptions {
clientService: ClientService
Expand Down Expand Up @@ -112,8 +113,12 @@ export class HandleUpload extends BasePlugin {

if (!this.currentFolder && unref(this.route)?.params?.token) {
// public file drop
const publicLinkToken = unref(this.route).params.token
let endpoint = this.clientService.owncloudSdk.publicFiles.getFileUrl(publicLinkToken) + '/'
const publicLinkToken = queryItemAsString(unref(this.route).params.token)
let endpoint = urlJoin(
this.clientService.webdav.getPublicFileUrl(this.space, publicLinkToken),
{ trailingSlash: true }
)

for (const file of files) {
if (!this._uppy.getPlugin('Tus')) {
endpoint = urlJoin(endpoint, encodeURIComponent(file.name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ import { isShareSpaceResource, Resource, SpaceResource } from 'web-client/src/he
import { SharePermissions } from 'web-client/src/helpers/share'
import { useDownloadFile } from 'web-pkg/src/composables/download/useDownloadFile'
import { formatDateFromJSDate } from 'web-pkg/src/helpers'
import { useClientService } from 'web-pkg/src/composables'
export default defineComponent({
name: 'FileVersions',
setup() {
const clientService = useClientService()
const loading = ref(false)
return {
...useDownloadFile(),
space: inject<Ref<SpaceResource>>('space'),
resource: inject<Ref<Resource>>('resource'),
clientService,
loading
}
},
Expand Down Expand Up @@ -118,8 +121,12 @@ export default defineComponent({
this.loading = false
},
async revertVersion(file) {
const { fileId, id, path } = this.resource
await this.$client.fileVersions.restoreFileVersion(fileId, this.currentVersionId(file), path)
const { id } = this.resource
await this.clientService.webdav.restoreFileVersion(
this.space,
this.resource,
this.currentVersionId(file)
)
const resource = await (this.$clientService.webdav as WebDAV).getFileInfo(
this.space,
this.resource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { computed, unref, VNodeRef } from 'vue'
import { Store } from 'vuex'
import { SpaceResource } from 'web-client/src'
import { Resource, SpaceResource } from 'web-client/src'
import { Drive } from 'web-client/src/generated'
import {
useClientService,
Expand Down Expand Up @@ -57,30 +57,32 @@ export const useSpaceActionsUploadImage = ({
}

return loadingService.addTask(() => {
return clientService.owncloudSdk.files
.putFileContents(`/spaces/${selectedSpace.id}/.space/${file.name}`, file, {
return clientService.webdav
.putFileContents(selectedSpace, {
path: `/.space/${file.name}`,
content: file,
headers: extraHeaders,
overwrite: true
})
.then((image) => {
.then(({ fileId }: Resource) => {
return graphClient.drives
.updateDrive(
selectedSpace.id as string,
selectedSpace.id.toString(),
{
special: [
{
specialFolder: {
name: 'image'
},
id: image['OC-FileId']
id: fileId
}
]
} as Drive,
{}
)
.then(({ data }) => {
store.commit('runtime/spaces/UPDATE_SPACE_FIELD', {
id: selectedSpace.id as string,
id: selectedSpace.id.toString(),
field: 'spaceImageData',
value: data.special.find((special) => special.specialFolder.name === 'image')
})
Expand Down
42 changes: 15 additions & 27 deletions packages/web-app-files/src/search/sdk/list.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { SearchList, SearchResult } from 'web-app-search/src/types'
import ListComponent from '../../components/Search/List.vue'
import { ClientService } from 'web-pkg/src/services'
import {
ProjectSpaceResource,
buildResource,
isProjectSpaceResource,
Resource
} from 'web-client/src/helpers'
import { ProjectSpaceResource, isProjectSpaceResource } from 'web-client/src/helpers'
import { Component, computed, Ref, unref } from 'vue'
import { DavProperties, DavProperty } from 'web-client/src/webdav/constants'
import { DavProperties } from 'web-client/src/webdav/constants'
import { Store } from 'vuex'
import { ConfigurationManager } from 'web-pkg/src'
import { urlJoin } from 'web-client/src/utils'

export const searchLimit = 200

export interface SearchResource extends Resource {
highlights: string
}

export default class List implements SearchList {
public readonly component: Component
private readonly store: Store<any>
Expand Down Expand Up @@ -60,30 +51,27 @@ export default class List implements SearchList {
}
}

const { range, results } = await this.clientService.owncloudSdk.files.search(
term,
const { resources, totalResults } = await this.clientService.webdav.search(term, {
searchLimit,
DavProperties.Default,
davProperties: DavProperties.Default,
useSpacesEndpoint
)
})

return {
totalResults: range ? parseInt(range?.split('/')[1]) : null,
values: results.map((result) => {
const projectSpace = this.getProjectSpace(result.fileInfo[DavProperty.FileParent])
const resource = {
...(projectSpace ? projectSpace : buildResource(result)),
highlights: result.fileInfo[DavProperty.Highlights] || ''
} as SearchResource
totalResults,
values: resources.map((resource) => {
const matchingSpace = this.getProjectSpace(resource.parentFolderId)
const data = matchingSpace ? matchingSpace : resource

// info: in oc10 we have no storageId in resources. All resources are mounted into the personal space.
if (!resource.storageId) {
resource.storageId = this.store.getters.user.id
if (!data.storageId) {
data.storageId = this.store.getters.user.id
}
if (this.configurationManager.options.routing.fullShareOwnerPaths && resource.shareRoot) {
resource.path = urlJoin(resource.shareRoot, resource.path)
if (this.configurationManager.options.routing.fullShareOwnerPaths && data.shareRoot) {
data.path = urlJoin(data.shareRoot, data.path)
}
return { id: resource.id, data: resource }

return { id: data.id, data }
})
}
}
Expand Down
53 changes: 26 additions & 27 deletions packages/web-app-files/src/search/sdk/preview.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SearchPreview, SearchResult } from 'web-app-search/src/types'
import PreviewComponent from '../../components/Search/Preview.vue'
import { ClientService } from 'web-pkg/src/services'
import { ProjectSpaceResource, buildResource, isProjectSpaceResource } from 'web-client/src/helpers'
import { ProjectSpaceResource, isProjectSpaceResource } from 'web-client/src/helpers'
import { Cache } from 'web-pkg/src/helpers/cache'
import { Component, computed, Ref, unref } from 'vue'
import { Router } from 'vue-router'
import { DavProperties, DavProperty } from 'web-client/src/webdav/constants'
import { DavProperties } from 'web-client/src/webdav/constants'
import { Store } from 'vuex'
import { ConfigurationManager } from 'web-pkg/src'
import { urlJoin } from 'web-client/src/utils'
Expand Down Expand Up @@ -64,35 +64,34 @@ export default class Preview implements SearchPreview {
const areHiddenFilesShown = this.store.state.Files?.areHiddenFilesShown
const useSpacesEndpoint = this.store.getters.capabilities?.spaces?.enabled === true

const { range, results } = await this.clientService.owncloudSdk.files.search(
term,
previewSearchLimit, // todo: add configuration option, other places need that too... needs consolidation
DavProperties.Default,
const { resources, totalResults } = await this.clientService.webdav.search(term, {
searchLimit: previewSearchLimit,
davProperties: DavProperties.Default,
useSpacesEndpoint
)
const resources = results.reduce((acc, result) => {
const projectSpace = this.getProjectSpace(result.fileInfo[DavProperty.FileParent])
const resource = projectSpace ? projectSpace : buildResource(result)
// info: in oc10 we have no storageId in resources. All resources are mounted into the personal space.
if (!resource.storageId) {
resource.storageId = this.store.getters.user.id
}
})

if (this.configurationManager.options.routing.fullShareOwnerPaths && resource.shareRoot) {
resource.path = urlJoin(resource.shareRoot, resource.path)
}
return {
totalResults,
values: resources
.map((resource) => {
const matchingSpace = this.getProjectSpace(resource.parentFolderId)
const data = matchingSpace ? matchingSpace : resource

// filter results if hidden files shouldn't be shown due to settings
if (!resource.name.startsWith('.') || areHiddenFilesShown) {
acc.push({ id: resource.id, data: { ...resource } })
}
// info: in oc10 we have no storageId in resources. All resources are mounted into the personal space.
if (!data.storageId) {
data.storageId = this.store.getters.user.id
}
if (this.configurationManager.options.routing.fullShareOwnerPaths && data.shareRoot) {
data.path = urlJoin(data.shareRoot, data.path)
}

return acc
}, [])
return this.cache.set(term, {
totalResults: range ? parseInt(range?.split('/')[1]) : null,
values: resources
})
return { id: data.id, data }
})
.filter(({ data }) => {
// filter results if hidden files shouldn't be shown due to settings
return !data.name.startsWith('.') || areHiddenFilesShown
})
}
}

public get available(): boolean {
Expand Down
24 changes: 13 additions & 11 deletions packages/web-app-files/src/views/FilesDrop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

<script lang="ts">
import { mapGetters } from 'vuex'
import { DavProperties, DavProperty } from 'web-client/src/webdav/constants'
import { createLocationPublic, createLocationSpaces } from 'web-pkg/src/router'
import ResourceUpload from '../components/AppBar/Upload/ResourceUpload.vue'
Expand All @@ -54,7 +53,6 @@ import { useUpload } from 'web-runtime/src/composables/upload'
import { useGettext } from 'vue3-gettext'
import {
useClientService,
usePublicLinkPassword,
usePublicLinkToken,
useStore,
useRouter,
Expand All @@ -72,6 +70,8 @@ import { UppyService } from 'web-runtime/src/services/uppyService'
import { useAuthService } from 'web-pkg/src/composables/authContext/useAuthService'
import { HandleUpload } from 'web-app-files/src/HandleUpload'
import { createFileRouteOptions } from 'web-pkg/src/helpers/router'
import { SpaceResource } from 'web-client/src'
import { PublicSpaceResource } from 'web-client/src/helpers'
export default defineComponent({
components: {
Expand All @@ -87,7 +87,6 @@ export default defineComponent({
const authService = useAuthService()
const clientService = useClientService()
const publicToken = usePublicLinkToken({ store })
const publicLinkPassword = usePublicLinkPassword({ store })
const isUserContext = useUserContext({ store })
const { getInternalSpace } = useGetMatchingSpace()
useUpload({ uppyService })
Expand All @@ -111,7 +110,7 @@ export default defineComponent({
})
}
const share = ref()
const share = ref<PublicSpaceResource>()
const dragareaEnabled = ref(false)
const loading = ref(true)
const errorMessage = ref(null)
Expand Down Expand Up @@ -142,7 +141,7 @@ export default defineComponent({
if (unref(isUserContext) && unref(fileId)) {
try {
const path = await clientService.owncloudSdk.files.getPathForFileId(unref(fileId))
const path = await clientService.webdav.getPathForFileId(unref(fileId))
await resolveToInternalLocation(path)
loading.value = false
return
Expand All @@ -151,11 +150,14 @@ export default defineComponent({
}
}
clientService.owncloudSdk.publicFiles
.list(unref(publicToken), unref(publicLinkPassword), DavProperties.PublicLink, '0')
.then((files) => {
const spaces: SpaceResource[] = store.getters['runtime/spaces/spaces']
const space = spaces.find((s) => s.driveAlias === `public/${unref(publicToken)}`)
clientService.webdav
.listFiles(space, {}, { depth: 0 })
.then(({ resource }) => {
// Redirect to files list if the link doesn't have role "uploader"
const sharePermissions = parseInt(files[0].getProperty(DavProperty.PublicLinkPermission))
const sharePermissions = (resource as PublicSpaceResource).publicLinkPermission
if (linkRoleUploaderFolder.bitmask(false) !== sharePermissions) {
router.replace(
createLocationPublic('files-public-link', {
Expand All @@ -164,7 +166,7 @@ export default defineComponent({
)
return
}
share.value = files[0]
share.value = resource as PublicSpaceResource
})
.catch((error) => {
// likely missing password, redirect to public link password prompt
Expand Down Expand Up @@ -220,7 +222,7 @@ export default defineComponent({
if (this.share) {
return this.$gettext(
'%{owner} shared this folder with you for uploading',
{ owner: this.share.getProperty(this.$client.publicFiles.PUBLIC_LINK_SHARE_OWNER) },
{ owner: this.share.publicLinkShareOwner },
true
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web-app-files/src/views/spaces/DriveResolver.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default defineComponent({
const isRunningOnEos = store.getters.configuration?.options?.runningOnEos
if (unref(isUserContext) && unref(fileId) && !isRunningOnEos) {
try {
const path = await clientService.owncloudSdk.files.getPathForFileId(unref(fileId))
const path = await clientService.webdav.getPathForFileId(unref(fileId))
await resolveToInternalLocation(path)
loading.value = false
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'web-test-helpers'
import { unref, VNodeRef } from 'vue'
import { useStore } from 'web-pkg/src/composables'
import { SpaceResource } from 'web-client/src'
import { Resource, SpaceResource } from 'web-client/src'
import { Drive } from 'web-client/src/generated'

describe('uploadImage', () => {
Expand All @@ -22,9 +22,9 @@ describe('uploadImage', () => {
clientService.graphAuthenticated.drives.updateDrive.mockResolvedValue(
mockAxiosResolve(driveMock)
)
clientService.owncloudSdk.files.putFileContents.mockImplementation(() =>
Promise.resolve({
'OC-FileId':
clientService.webdav.putFileContents.mockResolvedValue(
mock<Resource>({
fileId:
'YTE0ODkwNGItNTZhNy00NTQ4LTk2N2MtZjcwZjhhYTY0Y2FjOmQ4YzMzMmRiLWUxNWUtNDRjMy05NGM2LTViYjQ2MGMwMWJhMw=='
})
)
Expand All @@ -43,9 +43,7 @@ describe('uploadImage', () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
getWrapper({
setup: async ({ uploadImageSpace }, { storeOptions, clientService }) => {
clientService.owncloudSdk.files.putFileContents.mockImplementation(() =>
Promise.reject(new Error(''))
)
clientService.webdav.putFileContents.mockRejectedValue(new Error(''))

await uploadImageSpace({
currentTarget: {
Expand Down
Loading

0 comments on commit 79fdf4a

Please sign in to comment.