Skip to content

Commit

Permalink
refactor: consistenly use webdav abstraction layer
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Sep 25, 2023
1 parent b25cdbf commit b095b55
Show file tree
Hide file tree
Showing 34 changed files with 360 additions and 239 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 './router/public'
import { locationSpacesGeneric } from './router/spaces'
import { 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,7 +1,6 @@
import { Store } from 'vuex'
import { isLocationTrashActive } from '../../../router'
import { buildWebDavFilesTrashPath } from '../../../helpers/resources'
import { buildWebDavSpacesTrashPath, SpaceResource } from 'web-client/src/helpers'
import { SpaceResource } from 'web-client/src/helpers'
import { isProjectSpaceResource } from 'web-client/src/helpers'
import { computed, unref } from 'vue'
import {
Expand All @@ -25,12 +24,8 @@ export const useFileActionsEmptyTrashBin = ({ store }: { store?: Store<any> } =
const hasPermanentDeletion = useCapabilityFilesPermanentDeletion()

const emptyTrashBin = ({ space }: { space: SpaceResource }) => {
const path = unref(hasShareJail)
? buildWebDavSpacesTrashPath(space.id)
: buildWebDavFilesTrashPath(store.getters.user.id)

return clientService.owncloudSdk.fileTrash
.clearTrashBin(path)
return clientService.webdav
.clearTrashBin(space, { hasShareJail: unref(hasShareJail), user: store.getters.user })
.then(() => {
store.dispatch('showMessage', {
title: $gettext('All deleted files were removed')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Store } from 'vuex'
import { cloneStateObject } from '../../../helpers/store'
import { isSameResource } from '../../../helpers/resource'
import { buildWebDavFilesTrashPath } from '../../../helpers/resources'
import { buildWebDavSpacesTrashPath, Resource, SpaceResource } from 'web-client/src/helpers'
import { Resource, SpaceResource } from 'web-client/src/helpers'
import PQueue from 'p-queue'
import { isLocationSpacesActive } from '../../../router'
import { dirname } from 'path'
Expand Down Expand Up @@ -107,13 +106,13 @@ export const useFileActionsDeleteResources = ({ store }: { store?: Store<any> })
)
})

const trashbin_deleteOp = (space, resource) => {
const path = unref(hasShareJail)
? buildWebDavSpacesTrashPath(space.id)
: buildWebDavFilesTrashPath(store.getters.user.id)

return owncloudSdk.fileTrash
.clearTrashBin(path, resource.id)
const trashbin_deleteOp = (space: SpaceResource, resource: Resource) => {
return clientService.webdav
.clearTrashBin(space, {
hasShareJail: unref(hasShareJail),
id: resource.id,
user: store.getters.user
})
.then(() => {
store.dispatch('Files/removeFilesFromTrashbin', [resource])
const translated = $gettext(
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
9 changes: 1 addition & 8 deletions packages/web-app-files/src/helpers/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
buildSpaceShare
} from 'web-client/src/helpers/share'
import {
buildWebDavFilesPath,
buildWebDavSpacesPath,
extractDomSelector,
extractExtensionFromFile,
Expand All @@ -28,14 +29,6 @@ export function renameResource(space: SpaceResource, resource: Resource, newPath
return resource
}

export function buildWebDavFilesPath(userId, path) {
return '/' + `files/${userId}/${path}`.split('/').filter(Boolean).join('/')
}

export function buildWebDavFilesTrashPath(userId, path = '') {
return '/' + `trash-bin/${userId}/${path}`.split('/').filter(Boolean).join('/')
}

export function isResourceTxtFileAlmostEmpty(resource: Resource): boolean {
const mimeType = resource.mimeType || ''
return mimeType.startsWith('text/') && (resource.size as number) < 30
Expand Down
38 changes: 13 additions & 25 deletions packages/web-app-files/src/search/sdk/list.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
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'

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 @@ -46,27 +37,24 @@ 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 matchingSpace = this.getMatchingSpace(result.fileInfo[DavProperty.FileParent])
const resource = {
...(matchingSpace ? matchingSpace : buildResource(result)),
highlights: result.fileInfo[DavProperty.Highlights] || ''
} as SearchResource
totalResults,
values: resources.map((resource) => {
const matchingSpace = this.getMatchingSpace(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
}
return { id: resource.id, data: resource }

return { id: resource.id, data }
})
}
}
Expand Down
48 changes: 24 additions & 24 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'

export const previewSearchLimit = 8
Expand Down Expand Up @@ -49,31 +49,31 @@ 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 matchingSpace = this.getMatchingSpace(result.fileInfo[DavProperty.FileParent])
const resource = matchingSpace ? matchingSpace : 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
}

// filter results if hidden files shouldn't be shown due to settings
if (!resource.name.startsWith('.') || areHiddenFilesShown) {
acc.push({ id: resource.id, data: { ...resource } })
}
})

return acc
}, [])
return this.cache.set(term, {
totalResults: range ? parseInt(range?.split('/')[1]) : null,
return {
totalResults,
values: resources
})
.map((resource) => {
const matchingSpace = this.getMatchingSpace(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 (!data.storageId) {
data.storageId = this.store.getters.user.id
}

return { id: resource.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
8 changes: 6 additions & 2 deletions packages/web-app-files/src/services/folder/loaderTrashbin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { Router } from 'vue-router'
import { useTask } from 'vue-concurrency'
import { DavProperties } from 'web-client/src/webdav/constants'
import { isLocationTrashActive } from '../../router'
import { buildDeletedResource, buildWebDavFilesTrashPath } from '../../helpers/resources'
import { buildDeletedResource } from '../../helpers/resources'
import { Store } from 'vuex'
import { Resource } from 'web-client'
import { useCapabilityShareJailEnabled } from 'web-pkg/src/composables'
import { unref } from 'vue'
import { buildResource, buildWebDavSpacesTrashPath } from 'web-client/src/helpers'
import {
buildResource,
buildWebDavFilesTrashPath,
buildWebDavSpacesTrashPath
} from 'web-client/src/helpers'

export class FolderLoaderTrashbin implements FolderLoader {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
Loading

0 comments on commit b095b55

Please sign in to comment.