54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
|
/**
|
||
|
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
|
||
|
*
|
||
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
||
|
*
|
||
|
* @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/>.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* Get an url encoded path
|
||
|
*
|
||
|
* @param {String} path the full path
|
||
|
* @returns {string} url encoded file path
|
||
|
*/
|
||
|
const encodeFilePath = function(path) {
|
||
|
const pathSections = (path.startsWith('/') ? path : `/${path}`).split('/')
|
||
|
let relativePath = ''
|
||
|
pathSections.forEach((section) => {
|
||
|
if (section !== '') {
|
||
|
relativePath += '/' + encodeURIComponent(section)
|
||
|
}
|
||
|
})
|
||
|
return relativePath
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Extract dir and name from file path
|
||
|
*
|
||
|
* @param {String} path the full path
|
||
|
* @returns {String[]} [dirPath, fileName]
|
||
|
*/
|
||
|
const extractFilePaths = function(path) {
|
||
|
const pathSections = path.split('/')
|
||
|
const fileName = pathSections[pathSections.length - 1]
|
||
|
const dirPath = pathSections.slice(0, pathSections.length - 1).join('/')
|
||
|
return [dirPath, fileName]
|
||
|
}
|
||
|
|
||
|
export { encodeFilePath, extractFilePaths }
|