Skip to content

Commit

Permalink
Get mimeIconUrl for media attachments without a session
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mejo- committed Mar 15, 2023
1 parent 1fd16fb commit d124e5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/nodes/ImageView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export default {
},
methods: {
async init() {
const candidates = this.$attachmentResolver.resolve(this.src)
const candidates = await this.$attachmentResolver.resolve(this.src)
return this.load(candidates)
},
async load(candidates) {
Expand Down Expand Up @@ -339,20 +339,21 @@ export default {
onLoaded() {
this.loaded = true
},
handleImageClick(src) {
async handleImageClick(src) {
const imageViews = Array.from(document.querySelectorAll('figure[data-component="image-view"].image-view'))
let basename, relativePath

imageViews.forEach(imgv => {
for (const imgv of imageViews) {
relativePath = imgv.getAttribute('data-src')
basename = relativePath.split('/').slice(-1).join()
const { url: source } = this.$attachmentResolver.resolve(relativePath, true).shift()
const response = await this.$attachmentResolver.resolve(relativePath, true)
const { url: source } = response.shift()
this.embeddedImagesList.push({
source,
basename,
relativePath,
})
})
}
this.imageIndex = this.embeddedImagesList.findIndex(image => image.relativePath === src)
this.showImageModal = true
},
Expand Down
21 changes: 19 additions & 2 deletions src/services/AttachmentResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import { generateUrl, generateRemoteUrl } from '@nextcloud/router'
import pathNormalize from 'path-normalize'
import axios from '@nextcloud/axios'

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

Expand Down Expand Up @@ -53,7 +54,7 @@ export default class AttachmentResolver {
*
* Currently returns either one or two urls.
*/
resolve(src, preferRawImage = false) {
async resolve(src, preferRawImage = false) {
if (this.#session && src.startsWith('text://')) {
const imageFileName = getQueryVariable(src, 'imageFileName')
return [{
Expand Down Expand Up @@ -96,7 +97,7 @@ export default class AttachmentResolver {
const imageFileName = this.#relativePath(src)
.replace(/\.attachments\.\d+\//, '')
// try the webdav url and attachment API if it fails
return [
const results = [
{
type: this.ATTACHMENT_TYPE_IMAGE,
url: this.#davUrl(src),
Expand All @@ -111,6 +112,16 @@ export default class AttachmentResolver {
name: imageFileName,
},
]
// Try mimeUrl as last resort for non-image attachments (e.g. in RichTextReader where we don't have a session)
const mimeUrl = await this.#getMimeUrl(this.#davUrl(src))
if (mimeUrl) {
results.push({
type: this.ATTACHMENT_TYPE_MEDIA,
url: await this.#getMimeUrl(this.#davUrl(src)),
name: imageFileName,
})
}
return results
}

return [{
Expand Down Expand Up @@ -249,6 +260,12 @@ export default class AttachmentResolver {
return pathNormalize(f)
}

async #getMimeUrl(src) {
const headResponse = await axios.head(src)
const mimeType = headResponse.headers['content-type']
return mimeType ? OC.MimeType.getIconUrl(mimeType) : null
}

}

/**
Expand Down

0 comments on commit d124e5b

Please sign in to comment.