Skip to content

Commit 1b59280

Browse files
committed
fix: Do not rely on OC.webroots or OC.appwebroots but use own logic
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent d1e7d53 commit 1b59280

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const generateFilePath = (app: string, type: string, file: string) => {
147147
link += file
148148
}
149149
} else if (file.substring(file.length - 3) !== 'php' && !isCore) {
150-
link = window?.OC?.appswebroots?.[app];
150+
link = getAppRootUrl(app)
151151
if (type) {
152152
link += '/' + type + '/'
153153
}
@@ -183,4 +183,26 @@ export const generateFilePath = (app: string, type: string, file: string) => {
183183
*
184184
* @return {string} web root path
185185
*/
186-
export const getRootUrl = () => window?.OC?.webroot || ''
186+
export function getRootUrl(): string {
187+
let webroot = window._oc_webroot
188+
189+
if (typeof webroot === 'undefined') {
190+
webroot = location.pathname
191+
const pos = webroot.indexOf('/index.php/')
192+
if (pos !== -1) {
193+
webroot = webroot.substr(0, pos)
194+
} else {
195+
webroot = webroot.substr(0, webroot.lastIndexOf('/'))
196+
}
197+
}
198+
return webroot
199+
}
200+
201+
/**
202+
* Return the web root path for a given app
203+
* @param {string} app The ID of the app
204+
*/
205+
export function getAppRootUrl(app: string): string {
206+
const webroots = window._oc_appswebroots ?? {}
207+
return webroots[app] ?? ''
208+
}

lib/oc.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/// <reference types="@nextcloud/typings" />
22

33
declare interface Window {
4-
OC: Nextcloud.v25.OC | Nextcloud.v26.OC | Nextcloud.v27.OC;
4+
OC: Nextcloud.v26.OC | Nextcloud.v27.OC;
5+
6+
// Private state directly from server
7+
_oc_webroot?: string
8+
_oc_appswebroots?: Record<string, string|undefined>
59
}

0 commit comments

Comments
 (0)