Skip to content

Commit 1d51240

Browse files
susnuxbackportbot[bot]
authored andcommitted
feat: add new link endpoint when using globalscale
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> [skip ci]
1 parent c0a2ca4 commit 1d51240

6 files changed

Lines changed: 72 additions & 19 deletions

File tree

apps/files/src/views/FileReferencePickerElement.vue

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import type { IFilePickerButton } from '@nextcloud/dialogs'
1515
1616
import { FilePickerVue as FilePicker } from '@nextcloud/dialogs/filepicker.js'
1717
import { translate as t } from '@nextcloud/l10n'
18-
import { generateUrl } from '@nextcloud/router'
1918
import { defineComponent } from 'vue'
19+
import { generateFileUrl } from '../../../files_sharing/src/utils/generateUrl.ts'
2020
2121
export default defineComponent({
2222
name: 'FileReferencePickerElement',
@@ -52,13 +52,11 @@ export default defineComponent({
5252
5353
buttonFactory(selected: NcNode[]): IFilePickerButton[] {
5454
const buttons = [] as IFilePickerButton[]
55-
if (selected.length === 0) {
55+
const [node] = selected
56+
// Do not allow selecting the users root folder or if no node is selected
57+
if (node === undefined || node.path === '/') {
5658
return []
5759
}
58-
const node = selected.at(0)
59-
if (node.path === '/') {
60-
return [] // Do not allow selecting the users root folder
61-
}
6260
buttons.push({
6361
label: t('files', 'Choose {file}', { file: node.displayname }),
6462
type: 'primary',
@@ -76,10 +74,7 @@ export default defineComponent({
7674
},
7775
7876
onSubmit(node: NcNode) {
79-
const url = new URL(window.location.href)
80-
url.pathname = generateUrl('/f/{fileId}', { fileId: node.fileid! })
81-
url.search = ''
82-
this.$emit('submit', url.href)
77+
this.$emit('submit', generateFileUrl(node.fileid!))
8378
},
8479
},
8580
})

apps/files/src/views/ReferenceFileWidget.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,9 @@ export default defineComponent({
218218
.addButton({
219219
id: 'open',
220220
label: this.t('settings', 'Open in files'),
221-
callback(nodes: Node[]) {
222-
if (nodes[0]) {
223-
window.open(generateUrl('/f/{fileid}', {
224-
fileid: nodes[0].fileid,
225-
}))
221+
callback([node]: Node[]) {
222+
if (node) {
223+
window.open(generateFileUrl(node.fileid!))
226224
}
227225
},
228226
type: 'primary',

apps/files_sharing/src/components/SharingEntryInherited.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ export default {
6363
6464
computed: {
6565
viaFileTargetUrl() {
66-
return generateUrl('/f/{fileid}', {
67-
fileid: this.share.viaFileid,
68-
})
66+
return generateFileUrl(this.share.viaFileid)
6967
},
7068
7169
viaFolderName() {

apps/files_sharing/src/components/SharingEntryInternal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default {
6868
* @return {string}
6969
*/
7070
internalLink() {
71-
return window.location.protocol + '//' + window.location.host + generateUrl('/f/') + this.fileInfo.id
71+
return generateFileUrl(this.fileInfo.id)
7272
},
7373
7474
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { describe, expect, it, vi } from 'vitest'
7+
import { generateFileUrl } from './generateUrl.ts'
8+
9+
const getCapabilities = vi.hoisted(() => vi.fn())
10+
vi.mock('@nextcloud/capabilities', () => ({ getCapabilities }))
11+
12+
describe('generateFileUrl', () => {
13+
it('should work without globalscale', () => {
14+
getCapabilities.mockReturnValue({ globalscale: null })
15+
const url = generateFileUrl(12345)
16+
expect(url).toBe('http://nextcloud.local/index.php/f/12345')
17+
})
18+
19+
it('should work with older globalscale', () => {
20+
getCapabilities.mockReturnValue({ globalscale: { enabled: true } })
21+
const url = generateFileUrl(12345)
22+
expect(url).toBe('http://nextcloud.local/index.php/f/12345')
23+
})
24+
25+
it('should work with globalscale', () => {
26+
getCapabilities.mockReturnValue({ globalscale: { enabled: true, token: 'abc123' } })
27+
const url = generateFileUrl(12345)
28+
expect(url).toBe('http://nextcloud.local/index.php/gf/abc123/12345')
29+
})
30+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { getCapabilities } from '@nextcloud/capabilities'
7+
import { generateUrl } from '@nextcloud/router'
8+
9+
interface IGlobalScaleCapabilities {
10+
token?: string
11+
}
12+
13+
/**
14+
* @param fileid - The file ID to generate the direct file link for
15+
*/
16+
export function generateFileUrl(fileid: number): string {
17+
const baseURL = window.location.protocol + '//' + window.location.host
18+
19+
const { globalscale } = getCapabilities() as { globalscale?: IGlobalScaleCapabilities }
20+
if (globalscale?.token) {
21+
return generateUrl('/gf/{token}/{fileid}', {
22+
token: globalscale.token,
23+
fileid,
24+
}, { baseURL })
25+
}
26+
27+
return generateUrl('/f/{fileid}', {
28+
fileid,
29+
}, {
30+
baseURL,
31+
})
32+
}

0 commit comments

Comments
 (0)