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 <opensource@fthiessen.de>
  • Loading branch information
susnux committed Mar 23, 2024
1 parent 2dd090e commit 0bacc16
Show file tree
Hide file tree
Showing 28 changed files with 200 additions and 305 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'
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
13 changes: 8 additions & 5 deletions src/mixins/FetchFilesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
*
*/

import { davGetClient, davRootPath } from '@nextcloud/files'
import { showError } from '@nextcloud/dialogs'
import { davRootPath } from '@nextcloud/files'
import { joinPaths } from '@nextcloud/paths'

import { davClient } from '../services/DavClient.ts'
import logger from '../services/logger.js'
import getPhotos from '../services/PhotoSearch.js'
import SemaphoreWithPriority from '../utils/semaphoreWithPriority.js'
Expand Down Expand Up @@ -91,7 +94,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 @@ -105,7 +108,7 @@ export default {
const source = joinPaths(davRootPath, store.state.userConfig.photosSourceFolder ?? '/Photos') + '/'
logger.debug('Photo source does not exist, creating it.')
try {
await davGetClient().createDirectory(source)
await davClient.createDirectory(source)
} catch (error) {
logger.error('Fail to create source directory', { error })
}
Expand All @@ -116,8 +119,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æ <skjnldsv@protonmail.com>
* @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@thiessen.de>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Ferdinand Thiessen <opensource@thiessen.de>
*
* @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()
61 changes: 38 additions & 23 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æ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0-or-later
*
Expand All @@ -19,34 +20,48 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
const props = `
<d:getcontentlength />
<d:getcontenttype />
<d:getetag />
<d:getlastmodified />
<d:resourcetype />
<nc:face-detections />
<nc:face-preview-image />
<nc:metadata-photos-size />
<nc:metadata-photos-original_date_time />
<nc:metadata-files-live-photo />
<nc:metadata-blurhash/>
<nc:has-preview />
<nc:realpath />
<nc:hidden />
<oc:favorite />
<oc:fileid />
<oc:permissions />
<nc:nbItems />
`

export { props }
export default `<?xml version="1.0"?>
import { getDavProperties, registerDavProperty } from '@nextcloud/files'

const photoDavProps = [
'face-detections',
'face-preview-image',
'metadata-photos-size',
'metadata-photos-original_date_time',
'metadata-files-live-photo',
'metadata-blurhash',
'realpath',
'nbItems',
]

/**
* Used to cache the props
*/
let props: string|null = null

/**
* Get the default props
*/
const getDefaultDavProps = () => {
if (props === null) {
photoDavProps.forEach(prop => registerDavProperty(prop))
props = getDavProperties()
}
return props
}

/**
* @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}
${getDefaultDavProps()}
${extraProps.join('\n')}
</d:prop>
</d:propfind>`
}
44 changes: 0 additions & 44 deletions src/services/FolderInfo.js

This file was deleted.

Loading

0 comments on commit 0bacc16

Please sign in to comment.