Skip to content

Commit

Permalink
enh: Migrate to use webdav v5 and @nextcloud/files for DAV handling
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Feb 4, 2024
1 parent dc24021 commit 34ab707
Show file tree
Hide file tree
Showing 27 changed files with 172 additions and 290 deletions.
2 changes: 1 addition & 1 deletion src/components/Albums/CollaboratorsSelectionForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export default {
await this.updateAlbumCollaborators()
await this.fetchCollection(
this.albumFileName,
['<nc:location />', '<nc:dateRange />', '<nc:collaborators />']
['<nc:location />', '<nc:dateRange />', '<nc:collaborators />'],
)
},

Expand Down
2 changes: 1 addition & 1 deletion src/components/FilesListViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import { NcEmptyContent, NcLoadingIcon } from '@nextcloud/vue'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'

import TiledLayout from '../components/TiledLayout/TiledLayout.vue'
import { fetchFile } from '../services/fileFetcher.js'
import { fetchFile } from '../services/fileFetcher.ts'
import VirtualScrolling from '../components/VirtualScrolling.vue'
import EmptyBox from '../assets/Illustrations/empty.svg'
import UserConfig from '../mixins/UserConfig.js'
Expand Down
33 changes: 16 additions & 17 deletions src/mixins/FetchFacesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
*
*/

import { mapActions, mapGetters } from 'vuex'

import { showError } from '@nextcloud/dialogs'
import { getCurrentUser } from '@nextcloud/auth'
import { mapActions, mapGetters } from 'vuex'
import he from 'he'

import client from '../services/DavClient.js'
import logger from '../services/logger.js'
import DavRequest from '../services/DavRequest.js'
import { genFileInfo } from '../utils/fileUtils.js'
import logger from '../services/logger.js'
import AbortControllerMixin from './AbortControllerMixin.js'
import he from 'he'
import { davClient } from '../services/DavClient.ts'
import { getPropFind } from '../services/DavRequest.ts'

export default {
name: 'FetchFacesMixin',
Expand Down Expand Up @@ -76,8 +75,8 @@ export default {
this.loadingFaces = true
this.errorFetchingFaces = null

const { data: faces } = await client.getDirectoryContents(`/recognize/${getCurrentUser()?.uid}/faces/`, {
data: DavRequest,
const { data: faces } = await davClient.getDirectoryContents(`/recognize/${getCurrentUser()?.uid}/faces/`, {
data: getPropFind(),
details: true,
signal: this.abortController.signal,
})
Expand Down Expand Up @@ -111,13 +110,13 @@ export default {
this.errorFetchingFiles = null
this.loadingFiles = true

let { data: fetchedFiles } = await client.getDirectoryContents(
let { data: fetchedFiles } = await davClient.getDirectoryContents(
`/recognize/${getCurrentUser()?.uid}/faces/${faceName}`,
{
data: DavRequest,
data: getPropFind(),
details: true,
signal: this.abortController.signal,
}
},
)

fetchedFiles = fetchedFiles
Expand Down Expand Up @@ -163,13 +162,13 @@ export default {
this.errorFetchingFiles = null
this.loadingFiles = true

let { data: fetchedFiles } = await client.getDirectoryContents(
let { data: fetchedFiles } = await davClient.getDirectoryContents(
`/recognize/${getCurrentUser()?.uid}/unassigned-faces`,
{
data: DavRequest,
data: getPropFind(),
details: true,
signal: this.abortController.signal,
}
},
)

fetchedFiles = fetchedFiles
Expand Down Expand Up @@ -204,13 +203,13 @@ export default {

async fetchUnassignedFacesCount() {
try {
const { data: unassignedFacesRoot } = await client.stat(
const { data: unassignedFacesRoot } = await davClient.stat(
`/recognize/${getCurrentUser()?.uid}/unassigned-faces`,
{
data: DavRequest,
data: getPropFind(),
details: true,
signal: this.abortController.signal,
}
},
)

const count = Number(unassignedFacesRoot.props.nbItems)
Expand Down
11 changes: 6 additions & 5 deletions src/mixins/FetchFilesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*
*/

import { showError } from '@nextcloud/dialogs'
import logger from '../services/logger.js'
import getPhotos from '../services/PhotoSearch.js'
import SemaphoreWithPriority from '../utils/semaphoreWithPriority.js'
Expand Down Expand Up @@ -52,8 +53,8 @@ export default {
/**
* @param {string} path - Path to pass to getPhotos.
* @param {object} options - Options to pass to getPhotos.
* @param {string[]} [blacklist=[]] - Array of ids to filter out.
* @param {boolean} [force=false] - Force fetching even if doneFetchingFiles is true
* @param {string[]} [blacklist] - Array of ids to filter out.
* @param {boolean} [force] - Force fetching even if doneFetchingFiles is true
* @return {Promise<string[]>} - The next batch of data depending on global offset.
*/
async fetchFiles(path = '', options = {}, blacklist = [], force = false) {
Expand Down Expand Up @@ -89,7 +90,7 @@ export default {
this.fetchedFileIds.push(
...fileIds
.map((fileId) => fileId.toString())
.filter((fileId) => !blacklist.includes(fileId))
.filter((fileId) => !blacklist.includes(fileId)),
)

this.$store.dispatch('appendFiles', fetchedFiles)
Expand All @@ -107,8 +108,8 @@ export default {
}

// cancelled request, moving on...
logger.error('Error fetching files', { error })
console.error(error)
showError(t('photos', 'Error fetching files'))
logger.error(error)
} finally {
this.loadingFiles = false
this.fetchSemaphore.release(fetchSemaphoreSymbol)
Expand Down
12 changes: 6 additions & 6 deletions src/services/Albums.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import moment from '@nextcloud/moment'
import { translate as t } from '@nextcloud/l10n'

import defaultClient from '../services/DavClient.js'
import logger from '../services/logger.js'
import DavRequest from '../services/DavRequest.js'
import { genFileInfo } from '../utils/fileUtils.js'
import { davClient } from './DavClient.ts'
import { getPropFind } from './DavRequest.ts'

/**
* @typedef {object} Album
Expand Down Expand Up @@ -68,7 +68,7 @@ function getDavRequest(extraProps = '') {
* @param {import('webdav').WebDAVClient} client - The DAV client to use.
* @return {Promise<Album|null>}
*/
export async function fetchAlbum(path, options, extraProps = '', client = defaultClient) {
export async function fetchAlbum(path, options, extraProps = '', client = davClient) {
try {
const response = await client.stat(path, {
data: getDavRequest(extraProps),
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function fetchAlbum(path, options, extraProps = '', client = defaul
* @param {import('webdav').WebDAVClient} client - The DAV client to use.
* @return {Promise<Album[]>}
*/
export async function fetchAlbums(path, options, extraProps = '', client = defaultClient) {
export async function fetchAlbums(path, options, extraProps = '', client = davClient) {
try {
const response = await client.getDirectoryContents(path, {
data: getDavRequest(extraProps),
Expand Down Expand Up @@ -164,10 +164,10 @@ function formatAlbum(album) {
* @param {import('webdav').WebDAVClient} client - The DAV client to use.
* @return {Promise<Array>}
*/
export async function fetchAlbumContent(path, options, client = defaultClient) {
export async function fetchAlbumContent(path, options, client = davClient) {
try {
const response = await client.getDirectoryContents(path, {
data: DavRequest,
data: getPropFind(),
details: true,
...options,
})
Expand Down
41 changes: 0 additions & 41 deletions src/services/DavClient.js

This file was deleted.

28 changes: 5 additions & 23 deletions src/services/FileInfo.js → src/services/DavClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
* @copyright Copyright (c) 2024 Ferdinand Thiessen <[email protected]>
*
* @author John Molakvoæ <[email protected]>
* @author Ferdinand Thiessen <[email protected]>
*
* @license AGPL-3.0-or-later
*
Expand All @@ -20,25 +20,7 @@
*
*/

import client, { prefixPath } from './DavClient.js'
import request from './DavRequest.js'
import { genFileInfo } from '../utils/fileUtils.js'
import type { WebDAVClient } from 'webdav'
import { davGetClient } from '@nextcloud/files'

/**
* Get a file info
*
* @param {string} path the path relative to the user root
* @return {FileInfo} the file info
*/
export default async function(path) {
// getDirectoryContents doesn't accept / for root
const fixedPath = path === '/' ? '' : path

// fetch listing
const response = await client.stat(prefixPath + fixedPath, {
data: request,
details: true,
})

return genFileInfo(response.data)
}
export const davClient: WebDAVClient = davGetClient()
13 changes: 9 additions & 4 deletions src/services/DavRequest.js → src/services/DavRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
*
* @author John Molakvoæ <[email protected]>
* @author Ferdinand Thiessen <[email protected]>
*
* @license AGPL-3.0-or-later
*
Expand All @@ -19,7 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
const props = `
export const davProps = `
<d:getcontentlength />
<d:getcontenttype />
<d:getetag />
Expand All @@ -39,13 +40,17 @@ const props = `
<nc:nbItems />
`

export { props }
export default `<?xml version="1.0"?>
/**
* @param extraProps - Extra properties to add to the DAV request.
*/
export function getPropFind(extraProps: string[] = []): string {
return `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:"
xmlns:oc="http://owncloud.org/ns"
xmlns:nc="http://nextcloud.org/ns"
xmlns:ocs="http://open-collaboration-services.org/ns">
<d:prop>
${props}
${[...davProps, ...extraProps].join('')}
</d:prop>
</d:propfind>`
}
44 changes: 0 additions & 44 deletions src/services/FolderInfo.js

This file was deleted.

Loading

0 comments on commit 34ab707

Please sign in to comment.