Skip to content

Commit

Permalink
feat: Rate-limit image previews
Browse files Browse the repository at this point in the history
Image preloading should be rate limited, to not overload the server.
Now it is set to max. 5 requests at the same time.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Oct 29, 2024
1 parent 37bff02 commit c614485
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
26 changes: 9 additions & 17 deletions lib/components/FilePicker/FilePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div :style="canLoadPreview ? { backgroundImage: `url(${previewURL})`} : undefined"
<div :style="previewLoaded ? { backgroundImage: `url(${previewURL})`} : undefined"
:class="fileListIconStyles['file-picker__file-icon']">
<template v-if="!canLoadPreview">
<template v-if="!previewLoaded">
<IconFile v-if="isFile" :size="20" />
<IconFolder v-else :size="20" />
</template>
Expand All @@ -14,14 +14,15 @@

<script setup lang="ts">
import { FileType, type Node } from '@nextcloud/files'
import { computed, ref, watchEffect } from 'vue'
import { getPreviewURL } from '../../composables/preview'
import { computed, ref, toRef } from 'vue'
import { usePreviewURL } from '../../composables/preview'

import IconFile from 'vue-material-design-icons/File.vue'
import IconFolder from 'vue-material-design-icons/Folder.vue'

// CSS modules
import fileListIconStylesModule from './FileListIcon.module.scss'

// workaround for vue2.7 bug, can be removed with vue3
const fileListIconStyles = ref(fileListIconStylesModule)

Expand All @@ -30,21 +31,12 @@ const props = defineProps<{
cropImagePreviews: boolean
}>()

const previewURL = computed(() => getPreviewURL(props.node, { cropPreview: props.cropImagePreviews }))
const {
previewURL,
previewLoaded,
} = usePreviewURL(toRef(props, 'node'), computed(() => ({ cropPreview: props.cropImagePreviews })))

const isFile = computed(() => props.node.type === FileType.File)
const canLoadPreview = ref(false)

watchEffect(() => {
canLoadPreview.value = false

if (previewURL.value) {
const loader = new Image()
loader.src = previewURL.value.href
loader.onerror = () => loader.remove()
loader.onload = () => { canLoadPreview.value = true; loader.remove() }
}
})
</script>

<script lang="ts">
Expand Down
12 changes: 11 additions & 1 deletion lib/composables/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
*/

import type { Node } from '@nextcloud/files'
import type { MaybeRef } from '@vueuse/core'
import type { Ref } from 'vue'

import { generateUrl } from '@nextcloud/router'
import { toValue } from '@vueuse/core'
import { ref, watchEffect } from 'vue'
import { preloadImage } from '../utils/imagePreload'

interface PreviewOptions {
/**
Expand Down Expand Up @@ -66,14 +68,22 @@ export function getPreviewURL(node: Node, options: PreviewOptions = {}) {
}
}

export const usePreviewURL = (node: Node | Ref<Node>, options?: PreviewOptions | Ref<PreviewOptions>) => {
export const usePreviewURL = (node: Node | Ref<Node>, options?: MaybeRef<PreviewOptions>) => {
const previewURL = ref<URL|null>(null)
const previewLoaded = ref(false)

watchEffect(() => {
previewLoaded.value = false
previewURL.value = getPreviewURL(toValue(node), toValue(options || {}))
if (previewURL.value) {
preloadImage(previewURL.value.href).then((success: boolean) => {
previewLoaded.value = success
})
}
})

return {
previewURL,
previewLoaded,
}
}
24 changes: 24 additions & 0 deletions lib/utils/imagePreload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import PQueue from 'p-queue'

const queue = new PQueue({ concurrency: 5 })

/**
* Preload an image URL
* @param url URL of the image
*/
export function preloadImage(url: string): Promise<boolean> {
const { resolve, promise } = Promise.withResolvers<boolean>()

Check failure on line 15 in lib/utils/imagePreload.ts

View workflow job for this annotation

GitHub Actions / test

lib/composables/preview.spec.ts > preview composable > previewURL > is reactive

TypeError: Promise.withResolvers is not a function ❯ Module.preloadImage lib/utils/imagePreload.ts:15:39 ❯ lib/composables/preview.ts:79:4 ❯ invokeWithErrorHandling node_modules/vue/dist/vue.runtime.common.dev.js:2931:30 ❯ call node_modules/vue/dist/vue.runtime.common.dev.js:1396:21 ❯ VueComponent.getter node_modules/vue/dist/vue.runtime.common.dev.js:1448:24 ❯ Watcher.get node_modules/vue/dist/vue.runtime.common.dev.js:3412:33 ❯ doWatch node_modules/vue/dist/vue.runtime.common.dev.js:1557:17 ❯ Module.watchEffect node_modules/vue/dist/vue.runtime.common.dev.js:1360:12 ❯ Module.usePreviewURL lib/composables/preview.ts:75:2 ❯ setup lib/composables/preview.spec.ts:42:29
queue.add(() => {
const image = new Image()
image.onerror = () => resolve(false)
image.onload = () => resolve(true)
image.src = url
})

return promise
}
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@types/toastify-js": "^1.12.3",
"@vueuse/core": "^11.0.3",
"cancelable-promise": "^4.3.1",
"p-queue": "^8.0.1",
"toastify-js": "^1.12.0",
"vue-frag": "^1.4.3",
"webdav": "^5.7.1"
Expand Down

0 comments on commit c614485

Please sign in to comment.