aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/utils/davUtils.ts
blob: cf49df5b278e035f63b700f9f7c404e76ab24746 (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
/**
 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import { getCurrentUser } from '@nextcloud/auth'
import { t } from '@nextcloud/l10n'
import type { WebDAVClientError } from 'webdav'

/**
 * Check whether this is a public share
 * @return {boolean} Whether this is a public share
 */
export function isPublic() {
	return !getCurrentUser()
}

/**
 * Get the sharing token
 * @return {string|null} The sharing token
 */
export function getToken() {
	const tokenElement = document.getElementById('sharingToken') as (HTMLInputElement | null)
	return tokenElement?.value
}

/**
 * Whether error is a WebDAVClientError
 * @param error - Any exception
 * @return {boolean} - Whether error is a WebDAVClientError
 */
function isWebDAVClientError(error: unknown): error is WebDAVClientError {
	return error instanceof Error && 'status' in error && 'response' in error
}

/**
 * Get a localized error message from webdav request
 * @param error - An exception from webdav request
 * @return {string} Localized error message for end user
 */
export function humanizeWebDAVError(error: unknown) {
	if (error instanceof Error) {
		if (isWebDAVClientError(error)) {
			const status = error.status || error.response?.status || 0
			if ([400, 404, 405].includes(status)) {
				return t('files', 'Folder not found')
			} else if (status === 403) {
				return t('files', 'This operation is forbidden')
			} else if (status === 500) {
				return t('files', 'This directory is unavailable, please check the logs or contact the administrator')
			} else if (status === 503) {
				return t('files', 'Storage is temporarily not available')
			}
		}
		return t('files', 'Unexpected error: {error}', { error: error.message })
	}

	return t('files', 'Unknown error')
}