Skip to content

Commit

Permalink
Use preview for unsupported images
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Oct 13, 2022
1 parent c776219 commit 3a136c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
15 changes: 9 additions & 6 deletions src/components/Images.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ export default {

asyncComputed: {
data() {
switch (this.mime) {
case 'image/svg+xml':
// Avoid svg xss attack vector
if (this.mime === 'image/svg+xml') {
return this.getBase64FromImage()
case 'image/gif':
return this.davPath
default:
return this.previewpath
}

// Load the raw gif instead of the static preview
if (this.mime === 'image/gif') {
return this.src
}

return this.previewPath
},
},
watch: {
Expand Down
32 changes: 23 additions & 9 deletions src/models/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,37 @@ import Images from '../components/Images.vue'

const enabledPreviewProviders = loadState(appName, 'enabled_preview_providers', [])

const mimes = [
'image/bmp',
/**
* Those mimes needs a proper preview to be displayed
* if they are not enabled on the server, let's not activate them.
*/
const previewSupportedMimes = [
'image/heic',
'image/heif',
'image/tiff',
'image/x-xbitmap',
]

/**
* Those mimes are always supported by the browser
* Since we fallback to the source image if there is no
* preview, we can always include them.
*/
const browserSupportedMimes = [
'image/apng',
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
'image/tiff',
'image/svg+xml',
'image/webp',
'image/x-xbitmap',
'image/x-icon',
]

// Filter out supported mimes that are _not_
// enabled in the preview API
const filterEnabledMimes = () => {
return mimes.filter(filter => {
return previewSupportedMimes.filter(filter => {
return enabledPreviewProviders.findIndex(mimeRegex => {
// Remove leading and trailing slash from string regex
const regex = new RegExp(mimeRegex.replace(/^\/|\/$/g, ''), 'i')
Expand All @@ -50,7 +66,7 @@ const filterEnabledMimes = () => {
}

const enabledMimes = filterEnabledMimes()
const ignoredMimes = mimes.filter(x => !enabledMimes.includes(x))
const ignoredMimes = previewSupportedMimes.filter(x => !enabledMimes.includes(x))
if (ignoredMimes.length > 0) {
logger.warn('Some mimes were ignored because they are not enabled in the server previews config', { ignoredMimes })
}
Expand All @@ -59,9 +75,7 @@ export default {
id: 'images',
group: 'media',
mimes: [
// Gif and svg images does not rely on previews
'image/gif',
'image/svg+xml',
...browserSupportedMimes,
...enabledMimes,
],
component: Images,
Expand Down

0 comments on commit 3a136c8

Please sign in to comment.