diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-05-13 16:37:38 +0200 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-05-13 16:37:38 +0200 |
commit | e3ae7dc11524bfe9c6f07305fa2761b54a8823bd (patch) | |
tree | 62a8d52940a502a647b767ad43e3176a1fbbbd49 /core/src | |
parent | 74ad4cce838172a51d441ffd53b7cae936f9dbb3 (diff) | |
download | nextcloud-server-e3ae7dc11524bfe9c6f07305fa2761b54a8823bd.tar.gz nextcloud-server-e3ae7dc11524bfe9c6f07305fa2761b54a8823bd.zip |
Move path helpers to the bundle
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/OC/index.js | 22 | ||||
-rw-r--r-- | core/src/OC/path.js | 123 | ||||
-rw-r--r-- | core/src/OC/routing.js | 18 |
3 files changed, 161 insertions, 2 deletions
diff --git a/core/src/OC/index.js b/core/src/OC/index.js index 6a9fea3c7b7..9838e63b0f8 100644 --- a/core/src/OC/index.js +++ b/core/src/OC/index.js @@ -24,6 +24,13 @@ import Apps from './apps' import {AppConfig, appConfig} from './appconfig' import appswebroots from './appswebroots' import Backbone from './backbone' +import { + basename, + dirname, + encodePath, + isSamePath, + joinPaths, +} from './path' import Config from './config' import { coreApps, @@ -55,9 +62,10 @@ import { import {isUserAdmin} from './admin' import L10N from './l10n' import { + filePath, generateUrl, getRootPath, - filePath, + imagePath, linkTo, linkToOCS, linkToRemote, @@ -125,6 +133,15 @@ export default { showMenu, unregisterMenu, + /* + * Path helpers + */ + basename, + encodePath, + dirname, + isSamePath, + joinPaths, + msg, Notification, PasswordConfirmation, @@ -132,11 +149,12 @@ export default { search, Util, debug, + filePath, generateUrl, get: get(window), set: set(window), getRootPath, - filePath, + imagePath, redirect, reload, requestToken: getRequestToken(), diff --git a/core/src/OC/path.js b/core/src/OC/path.js new file mode 100644 index 00000000000..ef58caf4850 --- /dev/null +++ b/core/src/OC/path.js @@ -0,0 +1,123 @@ +/* + * @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/>. + */ + +/** + * URI-Encodes a file path but keep the path slashes. + * + * @param {String} path + * @return {String} encoded path + */ +export const encodePath = path => { + if (!path) { + return path + } + const parts = path.split('/') + const result = [] + for (let i = 0; i < parts.length; i++) { + result.push(encodeURIComponent(parts[i])) + } + return result.join('/') +} + +/** + * Returns the base name of the given path. + * For example for "/abc/somefile.txt" it will return "somefile.txt" + * + * @param {String} path + * @return {String} base name + */ +export const basename = path => path.replace(/\\/g, '/').replace(/.*\//, '') + +/** + * Returns the dir name of the given path. + * For example for "/abc/somefile.txt" it will return "/abc" + * + * @param {String} path + * @return {String} dir name + */ +export const dirname = path => path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '') + +/** + * Returns whether the given paths are the same, without + * leading, trailing or doubled slashes and also removing + * the dot sections. + * + * @param {String} path1 first path + * @param {String} path2 second path + * @return {bool} true if the paths are the same + * + * @since 9.0 + */ +export const isSamePath = (path1, path2) => { + const pathSections1 = (path1 || '').split('/').filter(p => p !== '.') + const pathSections2 = (path2 || '').split('/').filter(p => p !== '.') + path1 = joinPaths.apply(undefined, pathSections1) + path2 = joinPaths.apply(undefined, pathSections2) + + return path1 === path2 +} + +/** + * Join path sections + * + * @param {...String} path sections + * + * @return {String} joined path, any leading or trailing slash + * will be kept + * + * @since 8.2 + */ +export const joinPaths = (...args) => { + if (arguments.length < 1) { + return '' + } + + // discard empty arguments + const nonEmptyArgs = args.filter(arg => arg.length > 0) + if (nonEmptyArgs.length < 1) { + return '' + } + + const lastArg = nonEmptyArgs[nonEmptyArgs.length - 1] + const leadingSlash = nonEmptyArgs[0].charAt(0) === '/' + const trailingSlash = lastArg.charAt(lastArg.length - 1) === '/'; + const sections = nonEmptyArgs.reduce((acc, section) => acc.concat(section.split('/')), []) + + let first = !leadingSlash + const path = sections.reduce((acc, section) => { + if (section === '') { + return acc + } + + if (first) { + first = false + return acc + section + } + + return acc + '/' + section + }, '') + + if (trailingSlash) { + // add it back + return path + '/' + } + return path +} diff --git a/core/src/OC/routing.js b/core/src/OC/routing.js index 8c7a7d7e1cd..145f075c1b2 100644 --- a/core/src/OC/routing.js +++ b/core/src/OC/routing.js @@ -99,6 +99,24 @@ export const generateUrl = (url, params, options) => { } /** + * 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) |