2019-09-25 19:19:42 +03:00
|
|
|
/**
|
2019-04-29 18:42:27 +03:00
|
|
|
* @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'
|
2019-05-02 17:03:58 +03:00
|
|
|
|
2019-04-29 18:42:27 +03:00
|
|
|
import OC from './index'
|
2019-09-25 19:19:42 +03:00
|
|
|
import { coreApps } from './constants'
|
2019-04-29 18:42:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string} Absolute URL to a file
|
2019-04-29 18:42:27 +03:00
|
|
|
*/
|
|
|
|
export const linkTo = (app, file) => filePath(app, '', file)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a relative url for remote use
|
|
|
|
* @param {string} service id
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string} the url
|
2019-04-29 18:42:27 +03:00
|
|
|
*/
|
2019-05-02 17:03:58 +03:00
|
|
|
export const linkToRemoteBase = service => getRootPath() + '/remote.php/' + service
|
2019-04-29 18:42:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Creates an absolute url for remote use
|
|
|
|
* @param {string} service id
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string} the url
|
2019-04-29 18:42:27 +03:00
|
|
|
*/
|
|
|
|
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
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string} OCS API base path
|
2019-04-29 18:42:27 +03:00
|
|
|
*/
|
|
|
|
export const linkToOCS = (service, version) => {
|
|
|
|
version = (version !== 2) ? 1 : 2
|
2019-05-02 17:03:58 +03:00
|
|
|
return window.location.protocol + '//' + window.location.host + getRootPath() + '/ocs/v' + version + '.php/' + service + '/'
|
2019-04-29 18:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the absolute url for the given relative url, which can contain parameters.
|
|
|
|
* Parameters will be URL encoded automatically.
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {string} url the url
|
|
|
|
* @param {Object} [params] params
|
|
|
|
* @param {Object} [options] destructuring object
|
2019-04-29 18:42:27 +03:00
|
|
|
* @param {bool} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string} Absolute URL for the given relative URL
|
2019-04-29 18:42:27 +03:00
|
|
|
*/
|
|
|
|
export const generateUrl = (url, params, options) => {
|
|
|
|
const defaultOptions = {
|
2019-11-13 15:05:10 +03:00
|
|
|
escape: true,
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
|
|
|
const allOptions = options || {}
|
|
|
|
_.defaults(allOptions, defaultOptions)
|
2019-04-29 18:42:27 +03:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
const _build = function(text, vars) {
|
|
|
|
vars = vars || []
|
2019-04-29 18:42:27 +03:00
|
|
|
return text.replace(/{([^{}]*)}/g,
|
2019-09-25 19:19:42 +03:00
|
|
|
function(a, b) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const r = (vars[b])
|
2019-04-29 18:42:27 +03:00
|
|
|
if (allOptions.escape) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return (typeof r === 'string' || typeof r === 'number') ? encodeURIComponent(r) : encodeURIComponent(a)
|
2019-04-29 18:42:27 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
return (typeof r === 'string' || typeof r === 'number') ? r : a
|
2019-04-29 18:42:27 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
)
|
|
|
|
}
|
2019-04-29 18:42:27 +03:00
|
|
|
if (url.charAt(0) !== '/') {
|
2019-09-25 19:19:42 +03:00
|
|
|
url = '/' + url
|
2019-04-29 18:42:27 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:23:08 +03:00
|
|
|
if (OC.config.modRewriteWorking === true) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return getRootPath() + _build(url, params)
|
2019-04-29 18:42:27 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
return getRootPath() + '/index.php' + _build(url, params)
|
2019-04-29 18:42:27 +03:00
|
|
|
}
|
|
|
|
|
2019-05-13 17:37:38 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string}
|
2019-05-13 17:37:38 +03:00
|
|
|
*/
|
|
|
|
export const imagePath = (app, file) => {
|
|
|
|
if (file.indexOf('.') === -1) {
|
2019-09-25 19:19:42 +03:00
|
|
|
// if no extension is given, use svg
|
2019-05-13 17:37:38 +03:00
|
|
|
return filePath(app, 'img', file + '.svg')
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePath(app, 'img', file)
|
|
|
|
}
|
|
|
|
|
2019-04-29 18:42:27 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string} Absolute URL for a file in an app
|
2019-04-29 18:42:27 +03:00
|
|
|
*/
|
|
|
|
export const filePath = (app, type, file) => {
|
2019-05-07 19:11:08 +03:00
|
|
|
const isCore = coreApps.indexOf(app) !== -1
|
2019-05-02 17:03:58 +03:00
|
|
|
let link = getRootPath()
|
2019-04-29 18:42:27 +03:00
|
|
|
if (file.substring(file.length - 3) === 'php' && !isCore) {
|
2019-09-25 19:19:42 +03:00
|
|
|
link += '/index.php/apps/' + app
|
2019-04-29 18:42:27 +03:00
|
|
|
if (file !== 'index.php') {
|
|
|
|
link += '/'
|
|
|
|
if (type) {
|
|
|
|
link += encodeURI(type + '/')
|
|
|
|
}
|
|
|
|
link += file
|
|
|
|
}
|
|
|
|
} else if (file.substring(file.length - 3) !== 'php' && !isCore) {
|
2019-09-25 19:19:42 +03:00
|
|
|
link = OC.appswebroots[app]
|
2019-04-29 18:42:27 +03:00
|
|
|
if (type) {
|
|
|
|
link += '/' + type + '/'
|
|
|
|
}
|
|
|
|
if (link.substring(link.length - 1) !== '/') {
|
|
|
|
link += '/'
|
|
|
|
}
|
|
|
|
link += file
|
|
|
|
} else {
|
2019-09-17 17:33:27 +03:00
|
|
|
if ((app === 'core' || app === 'search') && type === 'ajax') {
|
2019-04-29 18:42:27 +03:00
|
|
|
link += '/index.php/'
|
|
|
|
} else {
|
|
|
|
link += '/'
|
|
|
|
}
|
|
|
|
if (!isCore) {
|
|
|
|
link += 'apps/'
|
|
|
|
}
|
|
|
|
if (app !== '') {
|
|
|
|
app += '/'
|
|
|
|
link += app
|
|
|
|
}
|
|
|
|
if (type) {
|
|
|
|
link += type + '/'
|
|
|
|
}
|
|
|
|
link += file
|
|
|
|
}
|
|
|
|
return link
|
|
|
|
}
|
2019-05-02 17:03:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the web root path where this Nextcloud instance
|
|
|
|
* is accessible, with a leading slash.
|
|
|
|
* For example "/nextcloud".
|
|
|
|
*
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string} web root path
|
2019-05-02 17:03:58 +03:00
|
|
|
*
|
|
|
|
* @since 8.2
|
|
|
|
*/
|
|
|
|
export const getRootPath = () => OC.webroot
|