Skip to content

Commit

Permalink
fix(links): Insert /f/<fileId> format for links to files
Browse files Browse the repository at this point in the history
Get rid of the old `/path/?fileId=<id>` format. It never really worked
anyway because the paths were relative to the users' home directory.

Instead, insert links to files/folders as full URLs with the
`/f/<fileId>` format. This makes link previews work both in Text and
Collectives.

Rewrite links in the old format to the new format. But don't touch
absolute link (without origin) to the collectives app to not break
collectives-specific link handling to other pages.

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Feb 24, 2024
1 parent dd4d0e4 commit 1f068eb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
8 changes: 5 additions & 3 deletions cypress/e2e/nodes/Links.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ describe('test link marks', function() {
cy.contains('button', isFolder ? 'Choose' : `Choose ${filename}`).click()
})

return cy.getContent()
.find(`a[href*="${encodeURIComponent(filename)}"]`)
.should('have.text', text === undefined ? filename : text)
cy.getFileId(filename).then((fileId) => {
return cy.getContent()
.find(`a[href*="/f/${fileId}"]`)
.should('have.text', text === undefined ? filename : text)
})
}

beforeEach(() => cy.clearContent())
Expand Down
7 changes: 3 additions & 4 deletions src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import { NcActions, NcActionButton, NcActionInput } from '@nextcloud/vue'
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js'
import { FilePickerType, getFilePickerBuilder } from '@nextcloud/dialogs'
import { generateUrl } from '@nextcloud/router'

import { getMarkAttributes, isActive } from '@tiptap/core'

Expand Down Expand Up @@ -144,10 +145,8 @@ export default {
.then((file) => {
const client = OC.Files.getClient()
client.getFileInfo(file).then((_status, fileInfo) => {
const path = optimalPath(this.relativePath, `${fileInfo.path}/${fileInfo.name}`)
const encodedPath = path.split('/').map(encodeURIComponent).join('/') + (fileInfo.type === 'dir' ? '/' : '')
const href = `${encodedPath}?fileId=${fileInfo.id}`
this.setLink(href, fileInfo.name)
const url = new URL(generateUrl(`/f/${fileInfo.id}`), window.location)
this.setLink(url.href, fileInfo.name)
this.startPath = fileInfo.path + (fileInfo.type === 'dir' ? `/${fileInfo.name}/` : '')
})
this.menuOpen = false
Expand Down
21 changes: 8 additions & 13 deletions src/helpers/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*
*/

import { loadState } from '@nextcloud/initial-state'
import { generateUrl } from '@nextcloud/router'

const absolutePath = function(base, rel) {

Check failure on line 25 in src/helpers/links.js

View workflow job for this annotation

GitHub Actions / NPM lint

'absolutePath' is assigned a value but never used
Expand Down Expand Up @@ -62,22 +61,17 @@ const domHref = function(node, relativePath) {
if (ref.startsWith('#')) {
return ref
}
// Don't rewrite URL in Collectives context
if (loadState('core', 'active-app') === 'collectives') {
// Don't rewrite links to the collectives app
if (ref.includes('/apps/collectives/')) {
return ref
}

// Rewrite links with old format from file picker to `/f/<fileId>`
const match = ref.match(/^([^?]*)\?fileId=(\d+)/)
if (match) {
const [, relPath, id] = match
const currentDir = basedir(relativePath || OCA.Viewer?.file || '/')
const dir = absolutePath(currentDir, basedir(relPath))
if (relPath.length > 1 && relPath.endsWith('/')) {
// is directory
return generateUrl(`/apps/files/?dir=${dir}&fileId=${id}`)
} else {
return generateUrl(`/apps/files/?dir=${dir}&openfile=${id}#relPath=${relPath}`)
}
const [, , id] = match
const url = new URL(generateUrl(`/f/${id}`), window.location)
return url.href
}
return ref
}
Expand All @@ -90,7 +84,8 @@ const parseHref = function(dom) {
const match = ref.match(/\?dir=([^&]*)&openfile=([^&]*)#relPath=([^&]*)/)
if (match) {
const [, , id, path] = match

Check failure on line 86 in src/helpers/links.js

View workflow job for this annotation

GitHub Actions / NPM lint

'path' is assigned a value but never used
return `${path}?fileId=${id}`
const url = new URL(generateUrl(`/f/${id}`), window.location)
return url.href
}
return ref
}
Expand Down
51 changes: 28 additions & 23 deletions src/tests/helpers/links.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { domHref, parseHref } from '../../helpers/links'
import { loadState } from '@nextcloud/initial-state'

global.OCA = {
Viewer: {
Expand All @@ -13,9 +12,6 @@ global.OC = {

global._oc_webroot = ''

jest.mock('@nextcloud/initial-state')
loadState.mockImplementation((app, key) => 'files')

describe('Preparing href attributes for the DOM', () => {

test('leave empty hrefs alone', () => {
Expand All @@ -36,24 +32,24 @@ describe('Preparing href attributes for the DOM', () => {
.toBe('mailTo:[email protected]')
})

test('relative link with fileid', () => {
test('relative link with fileid (old format from file picker)', () => {
expect(domHref({attrs: {href: 'otherfile?fileId=123'}}))
.toBe('/apps/files/?dir=/Wiki&openfile=123#relPath=otherfile')
.toBe('http://localhost/f/123')
})

test('relative path with ../', () => {
test('relative path with ../ (old format from file picker)', () => {
expect(domHref({attrs: {href: '../other/otherfile?fileId=123'}}))
.toBe('/apps/files/?dir=/other&openfile=123#relPath=../other/otherfile')
.toBe('http://localhost/f/123')
})

test('absolute path', () => {
test('absolute path (old format from file picker)', () => {
expect(domHref({attrs: {href: '/other/otherfile?fileId=123'}}))
.toBe('/apps/files/?dir=/other&openfile=123#relPath=/other/otherfile')
.toBe('http://localhost/f/123')
})

test('absolute path', () => {
test('absolute path (old format from file picker)', () => {
expect(domHref({attrs: {href: '/otherfile?fileId=123'}}))
.toBe('/apps/files/?dir=/&openfile=123#relPath=/otherfile')
.toBe('http://localhost/f/123')
})

})
Expand All @@ -74,9 +70,9 @@ describe('Extracting short urls from the DOM', () => {
expect(parseHref(domStub())).toBe(undefined)
})

test('relative link with fileid', () => {
test('relative link with fileid (old format from file picker)', () => {
expect(parseHref(domStub('?dir=/other&openfile=123#relPath=../other/otherfile')))
.toBe('../other/otherfile?fileId=123')
.toBe('http://localhost/f/123')
})

})
Expand All @@ -101,20 +97,29 @@ describe('Inserting hrefs into the dom and extracting them again', () => {
expect(insertAndExtract({})).toBe(undefined)
})

test('default relative link format is unchanged', () => {
test('old relative link format (from file picker) is rewritten', () => {
expect(insertAndExtract({href: 'otherfile?fileId=123'}))
.toBe('otherfile?fileId=123')
.toBe('http://localhost/f/123')
})

})
test('old relative link format with ../ (from file picker) is rewritten', () => {
expect(insertAndExtract({href: '../otherfile?fileId=123'}))
.toBe('http://localhost/f/123')
})

describe('Preparing href attributes for the DOM in Collectives app', () => {
beforeAll(() => {
loadState.mockImplementation((app, key) => 'collectives')
test('old absolute link format (from file picker) is rewritten', () => {
expect(insertAndExtract({href: '/otherfile?fileId=123'}))
.toBe('http://localhost/f/123')
})

test('relative link with fileid in Collectives', () => {
expect(domHref({attrs: {href: 'otherfile?fileId=123'}}))
.toBe('otherfile?fileId=123')
test('default absolute link format is unchanged', () => {
expect(insertAndExtract({href: 'http://localhost/f/123'}))
.toBe('http://localhost/f/123')
})

test('absolute link to collectives page is unchanged', () => {
expect(insertAndExtract({href: '/apps/collectives/page?fileId=123'}))
.toBe('/apps/collectives/page?fileId=123')
})

})

0 comments on commit 1f068eb

Please sign in to comment.