Skip to content

Commit 0bacc16

Browse files
committed
enh: Migrate to use webdav v5 and @nextcloud/files for DAV handling
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 2dd090e commit 0bacc16

28 files changed

+200
-305
lines changed

src/components/Albums/CollaboratorsSelectionForm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export default {
349349
await this.updateAlbumCollaborators()
350350
await this.fetchCollection(
351351
this.albumFileName,
352-
['<nc:location />', '<nc:dateRange />', '<nc:collaborators />']
352+
['<nc:location />', '<nc:dateRange />', '<nc:collaborators />'],
353353
)
354354
},
355355

src/components/FilesListViewer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import { NcEmptyContent, NcLoadingIcon } from '@nextcloud/vue'
8484
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
8585
8686
import TiledLayout from '../components/TiledLayout/TiledLayout.vue'
87-
import { fetchFile } from '../services/fileFetcher.js'
87+
import { fetchFile } from '../services/fileFetcher.ts'
8888
import VirtualScrolling from '../components/VirtualScrolling.vue'
8989
import EmptyBox from '../assets/Illustrations/empty.svg'
9090

src/mixins/FetchFacesMixin.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
*
2121
*/
2222

23-
import { mapActions, mapGetters } from 'vuex'
24-
2523
import { showError } from '@nextcloud/dialogs'
2624
import { getCurrentUser } from '@nextcloud/auth'
25+
import { mapActions, mapGetters } from 'vuex'
26+
import he from 'he'
2727

28-
import client from '../services/DavClient.js'
29-
import logger from '../services/logger.js'
30-
import DavRequest from '../services/DavRequest.js'
3128
import { genFileInfo } from '../utils/fileUtils.js'
29+
import logger from '../services/logger.js'
3230
import AbortControllerMixin from './AbortControllerMixin.js'
33-
import he from 'he'
31+
import { davClient } from '../services/DavClient.ts'
32+
import { getPropFind } from '../services/DavRequest.ts'
3433

3534
export default {
3635
name: 'FetchFacesMixin',
@@ -76,8 +75,8 @@ export default {
7675
this.loadingFaces = true
7776
this.errorFetchingFaces = null
7877

79-
const { data: faces } = await client.getDirectoryContents(`/recognize/${getCurrentUser()?.uid}/faces/`, {
80-
data: DavRequest,
78+
const { data: faces } = await davClient.getDirectoryContents(`/recognize/${getCurrentUser()?.uid}/faces/`, {
79+
data: getPropFind(),
8180
details: true,
8281
signal: this.abortController.signal,
8382
})
@@ -111,13 +110,13 @@ export default {
111110
this.errorFetchingFiles = null
112111
this.loadingFiles = true
113112

114-
let { data: fetchedFiles } = await client.getDirectoryContents(
113+
let { data: fetchedFiles } = await davClient.getDirectoryContents(
115114
`/recognize/${getCurrentUser()?.uid}/faces/${faceName}`,
116115
{
117-
data: DavRequest,
116+
data: getPropFind(),
118117
details: true,
119118
signal: this.abortController.signal,
120-
}
119+
},
121120
)
122121

123122
fetchedFiles = fetchedFiles
@@ -163,13 +162,13 @@ export default {
163162
this.errorFetchingFiles = null
164163
this.loadingFiles = true
165164

166-
let { data: fetchedFiles } = await client.getDirectoryContents(
165+
let { data: fetchedFiles } = await davClient.getDirectoryContents(
167166
`/recognize/${getCurrentUser()?.uid}/unassigned-faces`,
168167
{
169-
data: DavRequest,
168+
data: getPropFind(),
170169
details: true,
171170
signal: this.abortController.signal,
172-
}
171+
},
173172
)
174173

175174
fetchedFiles = fetchedFiles
@@ -204,13 +203,13 @@ export default {
204203

205204
async fetchUnassignedFacesCount() {
206205
try {
207-
const { data: unassignedFacesRoot } = await client.stat(
206+
const { data: unassignedFacesRoot } = await davClient.stat(
208207
`/recognize/${getCurrentUser()?.uid}/unassigned-faces`,
209208
{
210-
data: DavRequest,
209+
data: getPropFind(),
211210
details: true,
212211
signal: this.abortController.signal,
213-
}
212+
},
214213
)
215214

216215
const count = Number(unassignedFacesRoot.props.nbItems)

src/mixins/FetchFilesMixin.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
*
2121
*/
2222

23-
import { davGetClient, davRootPath } from '@nextcloud/files'
23+
import { showError } from '@nextcloud/dialogs'
24+
import { davRootPath } from '@nextcloud/files'
2425
import { joinPaths } from '@nextcloud/paths'
26+
27+
import { davClient } from '../services/DavClient.ts'
2528
import logger from '../services/logger.js'
2629
import getPhotos from '../services/PhotoSearch.js'
2730
import SemaphoreWithPriority from '../utils/semaphoreWithPriority.js'
@@ -91,7 +94,7 @@ export default {
9194
this.fetchedFileIds.push(
9295
...fileIds
9396
.map((fileId) => fileId.toString())
94-
.filter((fileId) => !blacklist.includes(fileId))
97+
.filter((fileId) => !blacklist.includes(fileId)),
9598
)
9699

97100
this.$store.dispatch('appendFiles', fetchedFiles)
@@ -105,7 +108,7 @@ export default {
105108
const source = joinPaths(davRootPath, store.state.userConfig.photosSourceFolder ?? '/Photos') + '/'
106109
logger.debug('Photo source does not exist, creating it.')
107110
try {
108-
await davGetClient().createDirectory(source)
111+
await davClient.createDirectory(source)
109112
} catch (error) {
110113
logger.error('Fail to create source directory', { error })
111114
}
@@ -116,8 +119,8 @@ export default {
116119
}
117120

118121
// cancelled request, moving on...
119-
logger.error('Error fetching files', { error })
120-
console.error(error)
122+
showError(t('photos', 'Error fetching files'))
123+
logger.error(error)
121124
} finally {
122125
this.loadingFiles = false
123126
this.fetchSemaphore.release(fetchSemaphoreSymbol)

src/services/Albums.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import moment from '@nextcloud/moment'
2424
import { translate as t } from '@nextcloud/l10n'
2525

26-
import defaultClient from '../services/DavClient.js'
2726
import logger from '../services/logger.js'
28-
import DavRequest from '../services/DavRequest.js'
2927
import { genFileInfo } from '../utils/fileUtils.js'
28+
import { davClient } from './DavClient.ts'
29+
import { getPropFind } from './DavRequest.ts'
3030

3131
/**
3232
* @typedef {object} Album
@@ -68,7 +68,7 @@ function getDavRequest(extraProps = '') {
6868
* @param {import('webdav').WebDAVClient} client - The DAV client to use.
6969
* @return {Promise<Album|null>}
7070
*/
71-
export async function fetchAlbum(path, options, extraProps = '', client = defaultClient) {
71+
export async function fetchAlbum(path, options, extraProps = '', client = davClient) {
7272
try {
7373
const response = await client.stat(path, {
7474
data: getDavRequest(extraProps),
@@ -96,7 +96,7 @@ export async function fetchAlbum(path, options, extraProps = '', client = defaul
9696
* @param {import('webdav').WebDAVClient} client - The DAV client to use.
9797
* @return {Promise<Album[]>}
9898
*/
99-
export async function fetchAlbums(path, options, extraProps = '', client = defaultClient) {
99+
export async function fetchAlbums(path, options, extraProps = '', client = davClient) {
100100
try {
101101
const response = await client.getDirectoryContents(path, {
102102
data: getDavRequest(extraProps),
@@ -164,10 +164,10 @@ function formatAlbum(album) {
164164
* @param {import('webdav').WebDAVClient} client - The DAV client to use.
165165
* @return {Promise<Array>}
166166
*/
167-
export async function fetchAlbumContent(path, options, client = defaultClient) {
167+
export async function fetchAlbumContent(path, options, client = davClient) {
168168
try {
169169
const response = await client.getDirectoryContents(path, {
170-
data: DavRequest,
170+
data: getPropFind(),
171171
details: true,
172172
...options,
173173
})

src/services/DavClient.js

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
2+
* @copyright Copyright (c) 2024 Ferdinand Thiessen <[email protected]>
33
*
4-
* @author John Molakvoæ <[email protected]>
4+
* @author Ferdinand Thiessen <[email protected]>
55
*
66
* @license AGPL-3.0-or-later
77
*
@@ -20,25 +20,7 @@
2020
*
2121
*/
2222

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

27-
/**
28-
* Get a file info
29-
*
30-
* @param {string} path the path relative to the user root
31-
* @return {FileInfo} the file info
32-
*/
33-
export default async function(path) {
34-
// getDirectoryContents doesn't accept / for root
35-
const fixedPath = path === '/' ? '' : path
36-
37-
// fetch listing
38-
const response = await client.stat(prefixPath + fixedPath, {
39-
data: request,
40-
details: true,
41-
})
42-
43-
return genFileInfo(response.data)
44-
}
26+
export const davClient: WebDAVClient = davGetClient()

src/services/DavRequest.js renamed to src/services/DavRequest.ts

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
33
*
44
* @author John Molakvoæ <[email protected]>
5+
* @author Ferdinand Thiessen <[email protected]>
56
*
67
* @license AGPL-3.0-or-later
78
*
@@ -19,34 +20,48 @@
1920
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2021
*
2122
*/
22-
const props = `
23-
<d:getcontentlength />
24-
<d:getcontenttype />
25-
<d:getetag />
26-
<d:getlastmodified />
27-
<d:resourcetype />
28-
<nc:face-detections />
29-
<nc:face-preview-image />
30-
<nc:metadata-photos-size />
31-
<nc:metadata-photos-original_date_time />
32-
<nc:metadata-files-live-photo />
33-
<nc:metadata-blurhash/>
34-
<nc:has-preview />
35-
<nc:realpath />
36-
<nc:hidden />
37-
<oc:favorite />
38-
<oc:fileid />
39-
<oc:permissions />
40-
<nc:nbItems />
41-
`
4223

43-
export { props }
44-
export default `<?xml version="1.0"?>
24+
import { getDavProperties, registerDavProperty } from '@nextcloud/files'
25+
26+
const photoDavProps = [
27+
'face-detections',
28+
'face-preview-image',
29+
'metadata-photos-size',
30+
'metadata-photos-original_date_time',
31+
'metadata-files-live-photo',
32+
'metadata-blurhash',
33+
'realpath',
34+
'nbItems',
35+
]
36+
37+
/**
38+
* Used to cache the props
39+
*/
40+
let props: string|null = null
41+
42+
/**
43+
* Get the default props
44+
*/
45+
const getDefaultDavProps = () => {
46+
if (props === null) {
47+
photoDavProps.forEach(prop => registerDavProperty(prop))
48+
props = getDavProperties()
49+
}
50+
return props
51+
}
52+
53+
/**
54+
* @param extraProps - Extra properties to add to the DAV request.
55+
*/
56+
export function getPropFind(extraProps: string[] = []): string {
57+
return `<?xml version="1.0"?>
4558
<d:propfind xmlns:d="DAV:"
4659
xmlns:oc="http://owncloud.org/ns"
4760
xmlns:nc="http://nextcloud.org/ns"
4861
xmlns:ocs="http://open-collaboration-services.org/ns">
4962
<d:prop>
50-
${props}
63+
${getDefaultDavProps()}
64+
${extraProps.join('\n')}
5165
</d:prop>
5266
</d:propfind>`
67+
}

src/services/FolderInfo.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)