Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileList.js « services « src - github.com/nextcloud/photos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ee843288b27a814041215330a582b5725f28fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import { getSingleValue, getValueForKey, parseXML, propsToStat } from 'webdav/dist/interface/dav'
import { handleResponseCode, processResponsePayload } from 'webdav/dist/response'
import { normaliseHREF, normalisePath } from 'webdav/dist/url'
import client, { remotePath } from './DavClient'
import pathPosix from 'path-posix'
import request from './DavRequest'

/**
 * List files from a folder and filter out unwanted mimes
 *
 * @param {String} path the path relative to the user root
 * @param {Object} [options] optional options for axios
 * @returns {Array} the file list
 */
export default async function(path, options) {
	options = Object.assign({
		method: 'PROPFIND',
		headers: {
			Accept: 'text/plain',
			Depth: options.deep ? 'infinity' : 1,
		},
		responseType: 'text',
		data: request,
		details: true,
	}, options)

	/**
	 * Fetch listing
	 *
	 * we use a custom request because getDirectoryContents filter out the current directory,
	 * but we want this as well to save us an extra request
	 * see https://github.com/perry-mitchell/webdav-client/blob/baf858a4856d44ae19ac12cb10c469b3e6c41ae4/source/interface/directoryContents.js#L11
	 */
	let response = null
	const { data } = await client.customRequest(path, options)
		.then(handleResponseCode)
		.then(res => {
			response = res
			return res.data
		})
		.then(parseXML)
		.then(result => getDirectoryFiles(result, remotePath, options.details))
		.then(files => processResponsePayload(response, files, options.details))

	const list = data
		.map(entry => {
			return Object.assign({
				id: parseInt(entry.props.fileid),
				isFavorite: entry.props.favorite !== '0',
				hasPreview: entry.props['has-preview'] !== 'false',
			}, entry)
		})

	// filter all the files and folders
	let folder = {}
	const folders = []
	const files = []
	for (const entry of list) {
		if (entry.filename === path) {
			folder = entry
		} else if (entry.type === 'directory') {
			folders.push(entry)
		} else if (entry.mime === 'image/jpeg') {
			files.push(entry)
		}
	}

	// return current folder, subfolders and files
	return { folder, folders, files }
}

function getDirectoryFiles(result, serverBasePath, isDetailed = false) {
	const serverBase = pathPosix.join(serverBasePath, '/')
	// Extract the response items (directory contents)
	const multiStatus = getValueForKey('multistatus', result)
	const responseItems = getValueForKey('response', multiStatus)
	return (
		responseItems
		// Map all items to a consistent output structure (results)
			.map(item => {
				// HREF is the file path (in full)
				let href = getSingleValue(getValueForKey('href', item))
				href = normaliseHREF(href)
				// Each item should contain a stat object
				const propStat = getSingleValue(getValueForKey('propstat', item))
				const props = getSingleValue(getValueForKey('prop', propStat))
				// Process the true full filename (minus the base server path)
				const filename
                    = serverBase === '/' ? normalisePath(href) : normalisePath(pathPosix.relative(serverBase, href))
				return propsToStat(props, filename, isDetailed)
			})
	)
}