Skip to content

Commit

Permalink
fix: Do not rely on OC.webroots or OC.appwebroots but use own logic
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jan 24, 2024
1 parent d1e7d53 commit 1b59280
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 24 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export const generateFilePath = (app: string, type: string, file: string) => {
link += file
}
} else if (file.substring(file.length - 3) !== 'php' && !isCore) {
link = window?.OC?.appswebroots?.[app];
link = getAppRootUrl(app)
if (type) {
link += '/' + type + '/'
}
Expand Down Expand Up @@ -183,4 +183,26 @@ export const generateFilePath = (app: string, type: string, file: string) => {
*
* @return {string} web root path
*/
export const getRootUrl = () => window?.OC?.webroot || ''
export function getRootUrl(): string {
let webroot = window._oc_webroot

if (typeof webroot === 'undefined') {
webroot = location.pathname
const pos = webroot.indexOf('/index.php/')
if (pos !== -1) {
webroot = webroot.substr(0, pos)
} else {
webroot = webroot.substr(0, webroot.lastIndexOf('/'))
}
}
return webroot
}

/**
* Return the web root path for a given app
* @param {string} app The ID of the app
*/
export function getAppRootUrl(app: string): string {
const webroots = window._oc_appswebroots ?? {}
return webroots[app] ?? ''
}
6 changes: 5 additions & 1 deletion lib/oc.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/// <reference types="@nextcloud/typings" />

declare interface Window {
OC: Nextcloud.v25.OC | Nextcloud.v26.OC | Nextcloud.v27.OC;
OC: Nextcloud.v26.OC | Nextcloud.v27.OC;

// Private state directly from server
_oc_webroot?: string
_oc_appswebroots?: Record<string, string|undefined>
}

0 comments on commit 1b59280

Please sign in to comment.