* Parse content uri to rootUri and docID * * eg. *```js * parse("content://.../AA98-181D%3A::.../index.html") *``` * `returns` {rootUri: "content://.../AA98-181D%3A", docId: "...index.html"} * * @param {string} contentUri * @returns {{rootUri: string, docId: string, isFileUri: bool
(contentUri)
| 24 | * @returns {{rootUri: string, docId: string, isFileUri: boolean}} |
| 25 | */ |
| 26 | parse(contentUri) { |
| 27 | let rootUri, |
| 28 | docId = ""; |
| 29 | |
| 30 | const DOC_PROVIDER = |
| 31 | /^content:\/\/com\.((?![:<>"\/\\\|\?\*]).)*\.documents\//; |
| 32 | const TREE_URI = |
| 33 | /^content:\/\/com\.((?![:<>"\/\\\|\?\*]).)*\.documents\/tree\//; |
| 34 | const SINGLE_URI = |
| 35 | /^content:\/\/com\.(((?![:<>"\/\\\|\?\*]).)*)\.documents\/document/; |
| 36 | |
| 37 | if (DOC_PROVIDER.test(contentUri)) { |
| 38 | if (TREE_URI.test(contentUri)) { |
| 39 | if (/::/.test(contentUri)) { |
| 40 | [rootUri, docId] = contentUri.split("::"); |
| 41 | } else { |
| 42 | rootUri = contentUri; |
| 43 | docId = decodeURIComponent(contentUri.split("/").slice(-1)[0]); |
| 44 | } |
| 45 | } else if (SINGLE_URI.test(contentUri)) { |
| 46 | const [provider, providerId] = SINGLE_URI.exec(contentUri); |
| 47 | docId = decodeURIComponent(contentUri); //DecodeUri |
| 48 | docId = docId.replace(provider, ""); //replace single to tree |
| 49 | docId = path.normalize(docId); //normalize docid |
| 50 | |
| 51 | if (docId.startsWith("/")) docId = docId.slice(1); // remove leading '/' |
| 52 | |
| 53 | rootUri = |
| 54 | `content://com.${providerId}.documents/tree/` + |
| 55 | docId.split(":")[0] + |
| 56 | "%3A"; |
| 57 | } |
| 58 | |
| 59 | return { |
| 60 | rootUri, |
| 61 | docId, |
| 62 | isFileUri: /^file:\/\/\//.test(rootUri), |
| 63 | }; |
| 64 | } else { |
| 65 | throw new Error("Invalid uri format."); |
| 66 | } |
| 67 | }, |
| 68 | /** |
| 69 | * Formats the five contentUri object to string |
| 70 | * @param {{rootUri: string, docId: string} | String} contentUriObject or rootId |