* Join all arguments together and normalize the resulting path. * @param {...string} pathnames * @returns {String}
(...pathnames)
| 64 | * @returns {String} |
| 65 | */ |
| 66 | join(...pathnames) { |
| 67 | if (pathnames.length < 2) |
| 68 | throw new Error("Join(), requires at least two parameters"); |
| 69 | |
| 70 | let { url, query } = this.parse(pathnames[0]); |
| 71 | |
| 72 | const protocol = (this.PROTOCOL_PATTERN.exec(url) || [])[0] || ""; |
| 73 | |
| 74 | if (protocol === "content://") { |
| 75 | try { |
| 76 | if (pathnames[1].startsWith("/")) pathnames[1] = pathnames[1].slice(1); |
| 77 | const contentUri = Uri.parse(url); |
| 78 | let [root, pathname] = contentUri.docId.split(":"); |
| 79 | let newDocId = path.join(pathname, ...pathnames.slice(1)); |
| 80 | if (/^content:\/\/com.termux/.test(url)) { |
| 81 | const rootCondition = root.endsWith("/"); |
| 82 | const newDocIdCondition = newDocId.startsWith("/"); |
| 83 | if (rootCondition === newDocIdCondition) { |
| 84 | root = root.slice(0, -1); |
| 85 | } else if (!rootCondition === !newDocIdCondition) { |
| 86 | root += "/"; |
| 87 | } |
| 88 | return `${contentUri.rootUri}::${root}${newDocId}${query}`; |
| 89 | } |
| 90 | |
| 91 | // if pathname is undefined, meaning a docId/volume (e.g :primary:) |
| 92 | // has not been detected, so no newDocId's ":" will be added. |
| 93 | if (!pathname) { |
| 94 | // Ensure proper path separator between root and newDocId |
| 95 | let separator = ""; |
| 96 | if (root.endsWith("/") && newDocId.startsWith("/")) { |
| 97 | // Both have separator, strip one from newDocId |
| 98 | newDocId = newDocId.slice(1); |
| 99 | } else if (!root.endsWith("/") && !newDocId.startsWith("/")) { |
| 100 | // Neither has separator, add one |
| 101 | separator = "/"; |
| 102 | } |
| 103 | return `${contentUri.rootUri}::${root}${separator}${newDocId}${query}`; |
| 104 | } |
| 105 | return `${contentUri.rootUri}::${root}:${newDocId}${query}`; |
| 106 | } catch (error) { |
| 107 | return null; |
| 108 | } |
| 109 | } else if (protocol) { |
| 110 | url = url.replace(new RegExp("^" + protocol), ""); |
| 111 | pathnames[0] = url; |
| 112 | return protocol + path.join(...pathnames) + query; |
| 113 | } else { |
| 114 | return path.join(url, ...pathnames.slice(1)) + query; |
| 115 | } |
| 116 | }, |
| 117 | /** |
| 118 | * Make url safe by encoding url components |
| 119 | * @param {string} url |
no test coverage detected