Use @nextcloud/paths and deprecate OC helpers

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2019-10-03 18:48:35 +02:00 committed by John Molakvoæ (skjnldsv)
parent 5c21d11207
commit a27a4f757d
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
11 changed files with 95 additions and 454 deletions

46
core/js/dist/login.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

76
core/js/dist/main.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -35,213 +35,6 @@ describe('Core base tests', function() {
expect(OC.appswebroots).toBeDefined();
});
});
describe('basename', function() {
it('Returns the nothing if no file name given', function() {
expect(OC.basename('')).toEqual('');
});
it('Returns the nothing if dir is root', function() {
expect(OC.basename('/')).toEqual('');
});
it('Returns the same name if no path given', function() {
expect(OC.basename('some name.txt')).toEqual('some name.txt');
});
it('Returns the base name if root path given', function() {
expect(OC.basename('/some name.txt')).toEqual('some name.txt');
});
it('Returns the base name if double root path given', function() {
expect(OC.basename('//some name.txt')).toEqual('some name.txt');
});
it('Returns the base name if subdir given without root', function() {
expect(OC.basename('subdir/some name.txt')).toEqual('some name.txt');
});
it('Returns the base name if subdir given with root', function() {
expect(OC.basename('/subdir/some name.txt')).toEqual('some name.txt');
});
it('Returns the base name if subdir given with double root', function() {
expect(OC.basename('//subdir/some name.txt')).toEqual('some name.txt');
});
it('Returns the base name if subdir has dot', function() {
expect(OC.basename('/subdir.dat/some name.txt')).toEqual('some name.txt');
});
it('Returns dot if file name is dot', function() {
expect(OC.basename('/subdir/.')).toEqual('.');
});
// TODO: fix the source to make it work like PHP's basename
it('Returns the dir itself if no file name given', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(OC.basename('subdir/')).toEqual('subdir');
expect(OC.basename('subdir/')).toEqual('');
});
it('Returns the dir itself if no file name given with root', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(OC.basename('/subdir/')).toEqual('subdir');
expect(OC.basename('/subdir/')).toEqual('');
});
});
describe('dirname', function() {
it('Returns the nothing if no file name given', function() {
expect(OC.dirname('')).toEqual('');
});
it('Returns the root if dir is root', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(OC.dirname('/')).toEqual('/');
expect(OC.dirname('/')).toEqual('');
});
it('Returns the root if dir is double root', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(OC.dirname('//')).toEqual('/');
expect(OC.dirname('//')).toEqual('/'); // oh no...
});
it('Returns dot if dir is dot', function() {
expect(OC.dirname('.')).toEqual('.');
});
it('Returns dot if no root given', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(OC.dirname('some dir')).toEqual('.');
expect(OC.dirname('some dir')).toEqual('some dir'); // oh no...
});
it('Returns the dir name if file name and root path given', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(OC.dirname('/some name.txt')).toEqual('/');
expect(OC.dirname('/some name.txt')).toEqual('');
});
it('Returns the dir name if double root path given', function() {
expect(OC.dirname('//some name.txt')).toEqual('/'); // how lucky...
});
it('Returns the dir name if subdir given without root', function() {
expect(OC.dirname('subdir/some name.txt')).toEqual('subdir');
});
it('Returns the dir name if subdir given with root', function() {
expect(OC.dirname('/subdir/some name.txt')).toEqual('/subdir');
});
it('Returns the dir name if subdir given with double root', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(OC.dirname('//subdir/some name.txt')).toEqual('/subdir');
expect(OC.dirname('//subdir/some name.txt')).toEqual('//subdir'); // oh...
});
it('Returns the dir name if subdir has dot', function() {
expect(OC.dirname('/subdir.dat/some name.txt')).toEqual('/subdir.dat');
});
it('Returns the dir name if file name is dot', function() {
expect(OC.dirname('/subdir/.')).toEqual('/subdir');
});
it('Returns the dir name if no file name given', function() {
expect(OC.dirname('subdir/')).toEqual('subdir');
});
it('Returns the dir name if no file name given with root', function() {
expect(OC.dirname('/subdir/')).toEqual('/subdir');
});
});
describe('joinPaths', function() {
it('returns empty string with no or empty arguments', function() {
expect(OC.joinPaths()).toEqual('');
expect(OC.joinPaths('')).toEqual('');
expect(OC.joinPaths('', '')).toEqual('');
});
it('returns joined path sections', function() {
expect(OC.joinPaths('abc')).toEqual('abc');
expect(OC.joinPaths('abc', 'def')).toEqual('abc/def');
expect(OC.joinPaths('abc', 'def', 'ghi')).toEqual('abc/def/ghi');
});
it('keeps leading slashes', function() {
expect(OC.joinPaths('/abc')).toEqual('/abc');
expect(OC.joinPaths('/abc', '')).toEqual('/abc');
expect(OC.joinPaths('', '/abc')).toEqual('/abc');
expect(OC.joinPaths('/abc', 'def')).toEqual('/abc/def');
expect(OC.joinPaths('/abc', 'def', 'ghi')).toEqual('/abc/def/ghi');
});
it('keeps trailing slashes', function() {
expect(OC.joinPaths('', 'abc/')).toEqual('abc/');
expect(OC.joinPaths('abc/')).toEqual('abc/');
expect(OC.joinPaths('abc/', '')).toEqual('abc/');
expect(OC.joinPaths('abc', 'def/')).toEqual('abc/def/');
expect(OC.joinPaths('abc', 'def', 'ghi/')).toEqual('abc/def/ghi/');
});
it('splits paths in specified strings and discards extra slashes', function() {
expect(OC.joinPaths('//abc//')).toEqual('/abc/');
expect(OC.joinPaths('//abc//def//')).toEqual('/abc/def/');
expect(OC.joinPaths('//abc//', '//def//')).toEqual('/abc/def/');
expect(OC.joinPaths('//abc//', '//def//', '//ghi//')).toEqual('/abc/def/ghi/');
expect(OC.joinPaths('//abc//def//', '//ghi//jkl/mno/', '//pqr//'))
.toEqual('/abc/def/ghi/jkl/mno/pqr/');
expect(OC.joinPaths('/abc', '/def')).toEqual('/abc/def');
expect(OC.joinPaths('/abc/', '/def')).toEqual('/abc/def');
expect(OC.joinPaths('/abc/', 'def')).toEqual('/abc/def');
});
it('discards empty sections', function() {
expect(OC.joinPaths('abc', '', 'def')).toEqual('abc/def');
});
it('returns root if only slashes', function() {
expect(OC.joinPaths('//')).toEqual('/');
expect(OC.joinPaths('/', '/')).toEqual('/');
expect(OC.joinPaths('/', '//', '/')).toEqual('/');
});
});
describe('isSamePath', function() {
it('recognizes empty paths are equal', function() {
expect(OC.isSamePath('', '')).toEqual(true);
expect(OC.isSamePath('/', '')).toEqual(true);
expect(OC.isSamePath('//', '')).toEqual(true);
expect(OC.isSamePath('/', '/')).toEqual(true);
expect(OC.isSamePath('/', '//')).toEqual(true);
});
it('recognizes path with single sections as equal regardless of extra slashes', function() {
expect(OC.isSamePath('abc', 'abc')).toEqual(true);
expect(OC.isSamePath('/abc', 'abc')).toEqual(true);
expect(OC.isSamePath('//abc', 'abc')).toEqual(true);
expect(OC.isSamePath('abc', '/abc')).toEqual(true);
expect(OC.isSamePath('abc/', 'abc')).toEqual(true);
expect(OC.isSamePath('abc/', 'abc/')).toEqual(true);
expect(OC.isSamePath('/abc/', 'abc/')).toEqual(true);
expect(OC.isSamePath('/abc/', '/abc/')).toEqual(true);
expect(OC.isSamePath('//abc/', '/abc/')).toEqual(true);
expect(OC.isSamePath('//abc//', '/abc/')).toEqual(true);
expect(OC.isSamePath('abc', 'def')).toEqual(false);
expect(OC.isSamePath('/abc', 'def')).toEqual(false);
expect(OC.isSamePath('//abc', 'def')).toEqual(false);
expect(OC.isSamePath('abc', '/def')).toEqual(false);
expect(OC.isSamePath('abc/', 'def')).toEqual(false);
expect(OC.isSamePath('abc/', 'def/')).toEqual(false);
expect(OC.isSamePath('/abc/', 'def/')).toEqual(false);
expect(OC.isSamePath('/abc/', '/def/')).toEqual(false);
expect(OC.isSamePath('//abc/', '/def/')).toEqual(false);
expect(OC.isSamePath('//abc//', '/def/')).toEqual(false);
});
it('recognizes path with multiple sections as equal regardless of extra slashes', function() {
expect(OC.isSamePath('abc/def', 'abc/def')).toEqual(true);
expect(OC.isSamePath('/abc/def', 'abc/def')).toEqual(true);
expect(OC.isSamePath('abc/def', '/abc/def')).toEqual(true);
expect(OC.isSamePath('abc/def/', '/abc/def/')).toEqual(true);
expect(OC.isSamePath('/abc/def/', '/abc/def/')).toEqual(true);
expect(OC.isSamePath('/abc/def/', 'abc/def/')).toEqual(true);
expect(OC.isSamePath('//abc/def/', 'abc/def/')).toEqual(true);
expect(OC.isSamePath('//abc/def//', 'abc/def/')).toEqual(true);
expect(OC.isSamePath('abc/def', 'abc/ghi')).toEqual(false);
expect(OC.isSamePath('/abc/def', 'abc/ghi')).toEqual(false);
expect(OC.isSamePath('abc/def', '/abc/ghi')).toEqual(false);
expect(OC.isSamePath('abc/def/', '/abc/ghi/')).toEqual(false);
expect(OC.isSamePath('/abc/def/', '/abc/ghi/')).toEqual(false);
expect(OC.isSamePath('/abc/def/', 'abc/ghi/')).toEqual(false);
expect(OC.isSamePath('//abc/def/', 'abc/ghi/')).toEqual(false);
expect(OC.isSamePath('//abc/def//', 'abc/ghi/')).toEqual(false);
});
it('recognizes path entries with dot', function() {
expect(OC.isSamePath('.', '')).toEqual(true);
expect(OC.isSamePath('.', '.')).toEqual(true);
expect(OC.isSamePath('.', '/')).toEqual(true);
expect(OC.isSamePath('/.', '/')).toEqual(true);
expect(OC.isSamePath('/./', '/')).toEqual(true);
expect(OC.isSamePath('/./', '/.')).toEqual(true);
expect(OC.isSamePath('/./', '/./')).toEqual(true);
expect(OC.isSamePath('/./', '/./')).toEqual(true);
expect(OC.isSamePath('a/./b', 'a/b')).toEqual(true);
expect(OC.isSamePath('a/b/.', 'a/b')).toEqual(true);
expect(OC.isSamePath('./a/b', 'a/b')).toEqual(true);
});
});
describe('filePath', function() {
beforeEach(function() {
OC.webroot = 'http://localhost';

View File

@ -38,7 +38,7 @@ import {
encodePath,
isSamePath,
joinPaths
} from './path'
} from '@nextcloud/paths'
import {
build as buildQueryString,
parse as parseQueryString
@ -189,10 +189,25 @@ export default {
/*
* Path helpers
*/
/**
* @deprecated 18.0.0 use https://www.npmjs.com/package/@nextcloud/paths
*/
basename,
/**
* @deprecated 18.0.0 use https://www.npmjs.com/package/@nextcloud/paths
*/
encodePath,
/**
* @deprecated 18.0.0 use https://www.npmjs.com/package/@nextcloud/paths
*/
dirname,
/**
* @deprecated 18.0.0 use https://www.npmjs.com/package/@nextcloud/paths
*/
isSamePath,
/**
* @deprecated 18.0.0 use https://www.npmjs.com/package/@nextcloud/paths
*/
joinPaths,
/**

View File

@ -1,123 +0,0 @@
/**
* @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 path
* @returns {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 path
* @returns {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 path
* @returns {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
* @returns {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
*
* @returns {String} joined path, any leading or trailing slash
* will be kept
*
* @since 8.2
*/
export const joinPaths = (...args) => {
if (args.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
}

21
package-lock.json generated
View File

@ -866,6 +866,21 @@
}
}
},
"@nextcloud/paths": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/@nextcloud/paths/-/paths-0.2.0.tgz",
"integrity": "sha512-C3jdAeCmZ6RcAESn4dGtVo914bmMvCgJeHNSyDW9lGJF0OAlP4mSZtPvh9hUVOZGeWuwQkSTyHbzdGTP7aFnTQ==",
"requires": {
"core-js": "3.1.4"
},
"dependencies": {
"core-js": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-3.1.4.tgz",
"integrity": "sha512-YNZN8lt82XIMLnLirj9MhKDFZHalwzzrL9YLt6eb0T5D0EDl4IQ90IGkua8mHbnxNrkj1d8hbdizMc0Qmg1WnQ=="
}
}
},
"@nextcloud/router": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/@nextcloud/router/-/router-0.1.0.tgz",
@ -2831,9 +2846,9 @@
}
},
"electron-to-chromium": {
"version": "1.3.274",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.274.tgz",
"integrity": "sha512-9bWkiXxGylowqMXoF1ec7k6akmsL2nOa1kzZ4CKzBuwK9WVz0VauE1w/RVyYraE1LpJM7+8fNCsW9b7ZSoxWIg=="
"version": "1.3.275",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.275.tgz",
"integrity": "sha512-/YWtW/VapMnuYA1lNOaa1F4GhR1LBf+CUTp60lzDPEEh0XOzyOAyULyYZVF9vziZ3qSbTqCwmKwsyRXp66STbw=="
},
"elliptic": {
"version": "6.5.1",

View File

@ -27,6 +27,7 @@
"@chenfengyuan/vue-qrcode": "^1.0.1",
"@nextcloud/axios": "^0.4.1",
"@nextcloud/event-bus": "^0.2.0",
"@nextcloud/paths": "^0.2.0",
"@nextcloud/router": "^0.1.0",
"autosize": "^4.0.2",
"backbone": "^1.4.0",