Skip to content

Commit 31f15b0

Browse files
committed
feat: add new link endpoint when using globalscale
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent d5220d6 commit 31f15b0

6 files changed

Lines changed: 75 additions & 21 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 { Node as NcNode } from '@nextcloud/files'
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',
@@ -57,13 +57,11 @@ export default defineComponent({
5757
5858
buttonFactory(selected: NcNode[]): IFilePickerButton[] {
5959
const buttons = [] as IFilePickerButton[]
60-
if (selected.length === 0) {
60+
const [node] = selected
61+
// Do not allow selecting the users root folder or if no node is selected
62+
if (node === undefined || node.path === '/') {
6163
return []
6264
}
63-
const node = selected.at(0)
64-
if (node.path === '/') {
65-
return [] // Do not allow selecting the users root folder
66-
}
6765
buttons.push({
6866
label: t('files', 'Choose {file}', { file: node.displayname }),
6967
type: 'primary',
@@ -81,10 +79,7 @@ export default defineComponent({
8179
},
8280
8381
onSubmit(node: NcNode) {
84-
const url = new URL(window.location.href)
85-
url.pathname = generateUrl('/f/{fileId}', { fileId: node.fileid! })
86-
url.search = ''
87-
this.$emit('submit', url.href)
82+
this.$emit('submit', generateFileUrl(node.fileid!))
8883
},
8984
},
9085
})

apps/files/src/views/ReferenceFileWidget.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import path from 'path'
6767
import { defineComponent } from 'vue'
6868
import FileIcon from 'vue-material-design-icons/File.vue'
6969
import FolderIcon from 'vue-material-design-icons/Folder.vue'
70+
import { generateFileUrl } from '../../../files_sharing/src/utils/generateUrl.ts'
7071
import logger from '../logger.ts'
7172
7273
// see lib/private/Collaboration/Reference/File/FileReferenceProvider.php
@@ -234,11 +235,9 @@ export default defineComponent({
234235
.addButton({
235236
id: 'open',
236237
label: this.t('settings', 'Open in files'),
237-
callback(nodes: Node[]) {
238-
if (nodes[0]) {
239-
window.open(generateUrl('/f/{fileid}', {
240-
fileid: nodes[0].fileid,
241-
}))
238+
callback([node]: Node[]) {
239+
if (node) {
240+
window.open(generateFileUrl(node.fileid!))
242241
}
243242
},
244243
type: 'primary',

apps/files_sharing/src/components/SharingEntryInherited.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434

3535
<script>
3636
import { basename } from '@nextcloud/paths'
37-
import { generateUrl } from '@nextcloud/router'
3837
import NcActionButton from '@nextcloud/vue/components/NcActionButton'
3938
import NcActionLink from '@nextcloud/vue/components/NcActionLink'
4039
import NcActionText from '@nextcloud/vue/components/NcActionText'
4140
import NcAvatar from '@nextcloud/vue/components/NcAvatar'
4241
import SharingEntrySimple from '../components/SharingEntrySimple.vue'
4342
import SharesMixin from '../mixins/SharesMixin.js'
4443
import Share from '../models/Share.js'
44+
import { generateFileUrl } from '../utils/generateUrl.js'
4545
4646
export default {
4747
name: 'SharingEntryInherited',
@@ -65,9 +65,7 @@ export default {
6565
6666
computed: {
6767
viaFileTargetUrl() {
68-
return generateUrl('/f/{fileid}', {
69-
fileid: this.share.viaFileid,
70-
})
68+
return generateFileUrl(this.share.viaFileid)
7169
},
7270
7371
viaFolderName() {

apps/files_sharing/src/components/SharingEntryInternal.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131

3232
<script>
3333
import { showSuccess } from '@nextcloud/dialogs'
34-
import { generateUrl } from '@nextcloud/router'
3534
import NcActionButton from '@nextcloud/vue/components/NcActionButton'
3635
import CheckIcon from 'vue-material-design-icons/Check.vue'
3736
import ClipboardIcon from 'vue-material-design-icons/ContentCopy.vue'
3837
import SharingEntrySimple from './SharingEntrySimple.vue'
3938
import logger from '../services/logger.ts'
39+
import { generateFileUrl } from '../utils/generateUrl.ts'
4040
4141
export default {
4242
name: 'SharingEntryInternal',
@@ -69,7 +69,7 @@ export default {
6969
* @return {string}
7070
*/
7171
internalLink() {
72-
return window.location.protocol + '//' + window.location.host + generateUrl('/f/') + this.fileInfo.id
72+
return generateFileUrl(this.fileInfo.id)
7373
},
7474
7575
/**
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)