|
1
|
|
|
/** |
|
2
|
|
|
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* @author John Molakvoæ <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @license GNU AGPL version 3 or any later version |
|
7
|
|
|
* |
|
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
11
|
|
|
* License, or (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU Affero General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
import { getSingleValue, getValueForKey, parseXML, propsToStat } from 'webdav/dist/interface/dav' |
|
24
|
|
|
import { handleResponseCode, processResponsePayload } from 'webdav/dist/response' |
|
25
|
|
|
import { normaliseHREF, normalisePath } from 'webdav/dist/url' |
|
26
|
|
|
import client, { remotePath } from './DavClient' |
|
27
|
|
|
import pathPosix from 'path-posix' |
|
28
|
|
|
import request from './DavRequest' |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List files from a folder and filter out unwanted mimes |
|
32
|
|
|
* |
|
33
|
|
|
* @param {String} path the path relative to the user root |
|
34
|
|
|
* @param {Object} [options] optional options for axios |
|
35
|
|
|
* @returns {Array} the file list |
|
36
|
|
|
*/ |
|
37
|
|
|
export default async function(path, options) { |
|
38
|
|
|
options = Object.assign({ |
|
39
|
|
|
method: 'PROPFIND', |
|
40
|
|
|
headers: { |
|
41
|
|
|
Accept: 'text/plain', |
|
42
|
|
|
Depth: options.deep ? 'infinity' : 1 |
|
43
|
|
|
}, |
|
44
|
|
|
responseType: 'text', |
|
45
|
|
|
data: request, |
|
46
|
|
|
details: true |
|
47
|
|
|
}, options) |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Fetch listing |
|
51
|
|
|
* |
|
52
|
|
|
* we use a custom request because getDirectoryContents filter out the current directory, |
|
53
|
|
|
* but we want this as well to save us an extra request |
|
54
|
|
|
* see https://github.com/perry-mitchell/webdav-client/blob/baf858a4856d44ae19ac12cb10c469b3e6c41ae4/source/interface/directoryContents.js#L11 |
|
55
|
|
|
*/ |
|
56
|
|
|
let response = null |
|
57
|
|
|
const { data } = await client.customRequest(path, options) |
|
58
|
|
|
.then(handleResponseCode) |
|
59
|
|
|
.then(res => { |
|
60
|
|
|
response = res |
|
61
|
|
|
return res.data |
|
62
|
|
|
}) |
|
63
|
|
|
.then(parseXML) |
|
64
|
|
|
.then(result => getDirectoryFiles(result, remotePath, options.details)) |
|
65
|
|
|
.then(files => processResponsePayload(response, files, options.details)) |
|
66
|
|
|
|
|
67
|
|
|
const list = data |
|
68
|
|
|
.map(entry => { |
|
69
|
|
|
return Object.assign({ |
|
70
|
|
|
id: parseInt(entry.props.fileid), |
|
71
|
|
|
isFavorite: entry.props.favorite !== '0', |
|
72
|
|
|
hasPreview: entry.props['has-preview'] !== 'false' |
|
73
|
|
|
}, entry) |
|
74
|
|
|
}) |
|
75
|
|
|
|
|
76
|
|
|
// filter all the files and folders |
|
77
|
|
|
let folder = {} |
|
78
|
|
|
const folders = [] |
|
79
|
|
|
const files = [] |
|
80
|
|
|
for (let entry of list) { |
|
81
|
|
|
if (entry.filename === path) { |
|
82
|
|
|
folder = entry |
|
83
|
|
|
} else if (entry.type === 'directory') { |
|
84
|
|
|
folders.push(entry) |
|
85
|
|
|
} else if (entry.mime === 'image/jpeg') { |
|
86
|
|
|
files.push(entry) |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// return current folder, subfolders and files |
|
91
|
|
|
return { folder, folders, files } |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
function getDirectoryFiles(result, serverBasePath, isDetailed = false) { |
|
95
|
|
|
const serverBase = pathPosix.join(serverBasePath, '/') |
|
96
|
|
|
// Extract the response items (directory contents) |
|
97
|
|
|
const multiStatus = getValueForKey('multistatus', result) |
|
98
|
|
|
const responseItems = getValueForKey('response', multiStatus) |
|
99
|
|
|
return ( |
|
100
|
|
|
responseItems |
|
101
|
|
|
// Map all items to a consistent output structure (results) |
|
102
|
|
|
.map(item => { |
|
103
|
|
|
// HREF is the file path (in full) |
|
104
|
|
|
let href = getSingleValue(getValueForKey('href', item)) |
|
105
|
|
|
href = normaliseHREF(href) |
|
106
|
|
|
// Each item should contain a stat object |
|
107
|
|
|
const propStat = getSingleValue(getValueForKey('propstat', item)) |
|
108
|
|
|
const props = getSingleValue(getValueForKey('prop', propStat)) |
|
109
|
|
|
// Process the true full filename (minus the base server path) |
|
110
|
|
|
const filename |
|
111
|
|
|
= serverBase === '/' ? normalisePath(href) : normalisePath(pathPosix.relative(serverBase, href)) |
|
112
|
|
|
return propsToStat(props, filename, isDetailed) |
|
113
|
|
|
}) |
|
114
|
|
|
) |
|
115
|
|
|
} |
|
116
|
|
|
|