Skip to content

Commit

Permalink
Merge pull request #560 from nextcloud-libraries/fix/do-not-rely-on-g…
Browse files Browse the repository at this point in the history
…lobal-state
  • Loading branch information
skjnldsv committed Jan 24, 2024
2 parents d1e7d53 + 1b59280 commit d98ae33
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 d98ae33

Please sign in to comment.