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

routing.js « OC « src « core - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0223a6cfd1e816d59d5847d85201cf8ef830b546 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @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 _ from 'underscore'

import OC from './index'
import {coreApps} from './constants'

/**
 * Get an absolute url to a file in an app
 * @param {string} app the id of the app the file belongs to
 * @param {string} file the file path relative to the app folder
 * @return {string} Absolute URL to a file
 */
export const linkTo = (app, file) => filePath(app, '', file)

/**
 * Creates a relative url for remote use
 * @param {string} service id
 * @return {string} the url
 */
export const linkToRemoteBase = service => getRootPath() + '/remote.php/' + service

/**
 * @brief Creates an absolute url for remote use
 * @param {string} service id
 * @return {string} the url
 */
export const linkToRemote = service => window.location.protocol + '//' + window.location.host + linkToRemoteBase(service)

/**
 * Gets the base path for the given OCS API service.
 * @param {string} service name
 * @param {int} version OCS API version
 * @return {string} OCS API base path
 */
export const linkToOCS = (service, version) => {
	version = (version !== 2) ? 1 : 2
	return window.location.protocol + '//' + window.location.host + getRootPath() + '/ocs/v' + version + '.php/' + service + '/'
}

/**
 * Generates the absolute url for the given relative url, which can contain parameters.
 * Parameters will be URL encoded automatically.
 * @param {string} url
 * @param [params] params
 * @param [options] options
 * @param {bool} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
 * @return {string} Absolute URL for the given relative URL
 */
export const generateUrl = (url, params, options) => {
	const defaultOptions = {
			escape: true
		},
		allOptions = options || {};
	_.defaults(allOptions, defaultOptions);

	const _build = function (text, vars) {
		vars = vars || [];
		return text.replace(/{([^{}]*)}/g,
			function (a, b) {
				var r = (vars[b]);
				if (allOptions.escape) {
					return (typeof r === 'string' || typeof r === 'number') ? encodeURIComponent(r) : encodeURIComponent(a);
				} else {
					return (typeof r === 'string' || typeof r === 'number') ? r : a;
				}
			}
		);
	};
	if (url.charAt(0) !== '/') {
		url = '/' + url;

	}

	if (OC.config.modRewriteWorking === true) {
		return getRootPath() + _build(url, params);
	}

	return getRootPath() + '/index.php' + _build(url, params);
}

/**
 * get the absolute path to an image file
 * if no extension is given for the image, it will automatically decide
 * between .png and .svg based on what the browser supports
 *
 * @param {string} app the app id to which the image belongs
 * @param {string} file the name of the image file
 * @return {string}
 */
export const imagePath = (app, file) => {
	if (file.indexOf('.') === -1) {
		//if no extension is given, use svg
		return filePath(app, 'img', file + '.svg')
	}

	return filePath(app, 'img', file)
}

/**
 * Get the absolute url for a file in an app
 * @param {string} app the id of the app
 * @param {string} type the type of the file to link to (e.g. css,img,ajax.template)
 * @param {string} file the filename
 * @return {string} Absolute URL for a file in an app
 */
export const filePath = (app, type, file) => {
	const isCore = coreApps.indexOf(app) !== -1
	let link = getRootPath()
	if (file.substring(file.length - 3) === 'php' && !isCore) {
		link += '/index.php/apps/' + app;
		if (file !== 'index.php') {
			link += '/'
			if (type) {
				link += encodeURI(type + '/')
			}
			link += file
		}
	} else if (file.substring(file.length - 3) !== 'php' && !isCore) {
		link = OC.appswebroots[app];
		if (type) {
			link += '/' + type + '/'
		}
		if (link.substring(link.length - 1) !== '/') {
			link += '/'
		}
		link += file
	} else {
		if ((app === 'core' || app === 'search') && type === 'ajax') {
			link += '/index.php/'
		} else {
			link += '/'
		}
		if (!isCore) {
			link += 'apps/'
		}
		if (app !== '') {
			app += '/'
			link += app
		}
		if (type) {
			link += type + '/'
		}
		link += file
	}
	return link
}

/**
 * Returns the web root path where this Nextcloud instance
 * is accessible, with a leading slash.
 * For example "/nextcloud".
 *
 * @return {string} web root path
 *
 * @since 8.2
 */
export const getRootPath = () => OC.webroot