Skip to content

Commit 0f6805c

Browse files
committed
Get mimeIconUrl for media attachments without a session
In order to have non-image attachments rendered in editors without a session, `AttachmentResolver.resolve()` should return a candidate with the mimeType icon as url as last resort - because all endpoints from Text `AttachmentController` require a session. This fixes rendering of non-image attachments in RichTextReader. Fixes: #2919 Signed-off-by: Jonas <jonas@freesources.org>
1 parent 06f4888 commit 0f6805c

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/nodes/ImageView.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export default {
279279
},
280280
methods: {
281281
async init() {
282-
const candidates = this.$attachmentResolver.resolve(this.src)
282+
const candidates = await this.$attachmentResolver.resolve(this.src)
283283
return this.load(candidates)
284284
},
285285
async load(candidates) {
@@ -339,20 +339,21 @@ export default {
339339
onLoaded() {
340340
this.loaded = true
341341
},
342-
handleImageClick(src) {
342+
async handleImageClick(src) {
343343
const imageViews = Array.from(document.querySelectorAll('figure[data-component="image-view"].image-view'))
344344
let basename, relativePath
345345
346-
imageViews.forEach(imgv => {
346+
for (const imgv of imageViews) {
347347
relativePath = imgv.getAttribute('data-src')
348348
basename = relativePath.split('/').slice(-1).join()
349-
const { url: source } = this.$attachmentResolver.resolve(relativePath, true).shift()
349+
const response = await this.$attachmentResolver.resolve(relativePath, true)
350+
const { url: source } = response.shift()
350351
this.embeddedImagesList.push({
351352
source,
352353
basename,
353354
relativePath,
354355
})
355-
})
356+
}
356357
this.imageIndex = this.embeddedImagesList.findIndex(image => image.relativePath === src)
357358
this.showImageModal = true
358359
},

src/services/AttachmentResolver.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import { generateUrl, generateRemoteUrl } from '@nextcloud/router'
2424
import pathNormalize from 'path-normalize'
25+
import axios from '@nextcloud/axios'
2526

2627
import { logger } from '../helpers/logger.js'
2728

@@ -53,7 +54,7 @@ export default class AttachmentResolver {
5354
*
5455
* Currently returns either one or two urls.
5556
*/
56-
resolve(src, preferRawImage = false) {
57+
async resolve(src, preferRawImage = false) {
5758
if (this.#session && src.startsWith('text://')) {
5859
const imageFileName = getQueryVariable(src, 'imageFileName')
5960
return [{
@@ -96,7 +97,7 @@ export default class AttachmentResolver {
9697
const imageFileName = this.#relativePath(src)
9798
.replace(/\.attachments\.\d+\//, '')
9899
// try the webdav url and attachment API if it fails
99-
return [
100+
const results = [
100101
{
101102
type: this.ATTACHMENT_TYPE_IMAGE,
102103
url: this.#davUrl(src),
@@ -111,6 +112,16 @@ export default class AttachmentResolver {
111112
name: imageFileName,
112113
},
113114
]
115+
// Try mimeUrl as last resort for non-image attachments (e.g. in RichTextReader where we don't have a session)
116+
const mimeUrl = await this.#getMimeUrl(this.#davUrl(src))
117+
if (mimeUrl) {
118+
results.push({
119+
type: this.ATTACHMENT_TYPE_MEDIA,
120+
url: await this.#getMimeUrl(this.#davUrl(src)),
121+
name: imageFileName,
122+
})
123+
}
124+
return results
114125
}
115126

116127
return [{
@@ -249,6 +260,12 @@ export default class AttachmentResolver {
249260
return pathNormalize(f)
250261
}
251262

263+
async #getMimeUrl(src) {
264+
const headResponse = await axios.head(src)
265+
const mimeType = headResponse.headers['content-type']
266+
return mimeType ? OC.MimeType.getIconUrl(mimeType) : null
267+
}
268+
252269
}
253270

254271
/**

0 commit comments

Comments
 (0)