Load bg thumbnail config from backend
| @@ -27,6 +27,11 @@ const THUMBNAIL_STORAGE = localforage.createInstance({ name: 'SillyTavern_Thumbn | |||
| 27 | */ | 27 | */ |
| 28 | const THUMBNAIL_BLOBS = new Map(); | 28 | const THUMBNAIL_BLOBS = new Map(); |
| 29 | 29 | ||
| 30 | const THUMBNAIL_CONFIG = { | ||
| 31 | width: 160, | ||
| 32 | height: 90, | ||
| 33 | }; | ||
| 34 | |||
| 30 | /** | 35 | /** |
| 31 | * Global IntersectionObserver instance for lazy loading backgrounds | 36 | * Global IntersectionObserver instance for lazy loading backgrounds |
| 32 | * @type {IntersectionObserver|null} | 37 | * @type {IntersectionObserver|null} |
| @@ -282,7 +287,7 @@ async function getThumbnailFromStorage(bg) { | |||
| 282 | } | 287 | } |
| 283 | const imageBlob = await response.blob(); | 288 | const imageBlob = await response.blob(); |
| 284 | const imageBase64 = await getBase64Async(imageBlob); | 289 | const imageBase64 = await getBase64Async(imageBlob); |
| 285 | const thumbnailBase64 = await createThumbnail(imageBase64); | 290 | const thumbnailBase64 = await createThumbnail(imageBase64, THUMBNAIL_CONFIG.width, THUMBNAIL_CONFIG.height); |
| 286 | const thumbnailBlob = await fetch(thumbnailBase64).then(res => res.blob()); | 291 | const thumbnailBlob = await fetch(thumbnailBase64).then(res => res.blob()); |
| 287 | await THUMBNAIL_STORAGE.setItem(bg, thumbnailBlob); | 292 | await THUMBNAIL_STORAGE.setItem(bg, thumbnailBlob); |
| 288 | const blobUrl = URL.createObjectURL(thumbnailBlob); | 293 | const blobUrl = URL.createObjectURL(thumbnailBlob); |
| @@ -445,9 +450,10 @@ export async function getBackgrounds() { | |||
| 445 | body: JSON.stringify({}), | 450 | body: JSON.stringify({}), |
| 446 | }); | 451 | }); |
| 447 | if (response.ok) { | 452 | if (response.ok) { |
| 448 | const getData = await response.json(); | 453 | const { images, config } = await response.json(); |
| 454 | Object.assign(THUMBNAIL_CONFIG, config); | ||
| 449 | $('#bg_menu_content').children('div').remove(); | 455 | $('#bg_menu_content').children('div').remove(); |
| 450 | for (const bg of getData) { | 456 | for (const bg of images) { |
| 451 | const template = await getBackgroundFromTemplate(bg, false); | 457 | const template = await getBackgroundFromTemplate(bg, false); |
| 452 | $('#bg_menu_content').append(template); | 458 | $('#bg_menu_content').append(template); |
| 453 | } | 459 | } |
| @@ -4,15 +4,16 @@ import path from 'node:path'; | |||
| 4 | import express from 'express'; | 4 | import express from 'express'; |
| 5 | import sanitize from 'sanitize-filename'; | 5 | import sanitize from 'sanitize-filename'; |
| 6 | 6 | ||
| 7 | import { invalidateThumbnail } from './thumbnails.js'; | 7 | import { dimensions, invalidateThumbnail } from './thumbnails.js'; |
| 8 | import { getImages } from '../util.js'; | 8 | import { getImages } from '../util.js'; |
| 9 | import { getFileNameValidationFunction } from '../middleware/validateFileName.js'; | 9 | import { getFileNameValidationFunction } from '../middleware/validateFileName.js'; |
| 10 | 10 | ||
| 11 | export const router = express.Router(); | 11 | export const router = express.Router(); |
| 12 | 12 | ||
| 13 | router.post('/all', function (request, response) { | 13 | router.post('/all', function (request, response) { |
| 14 | var images = getImages(request.user.directories.backgrounds); | 14 | const images = getImages(request.user.directories.backgrounds); |
| 15 | response.send(JSON.stringify(images)); | 15 | const config = { width: dimensions.bg[0], height: dimensions.bg[1] }; |
| 16 | response.json({ images, config }); | ||
| 16 | }); | 17 | }); |
| 17 | 18 | ||
| 18 | router.post('/delete', getFileNameValidationFunction('bg'), function (request, response) { | 19 | router.post('/delete', getFileNameValidationFunction('bg'), function (request, response) { |
| @@ -15,7 +15,7 @@ const quality = Math.min(100, Math.max(1, parseInt(getConfigValue('thumbnails.qu | |||
| 15 | const pngFormat = String(getConfigValue('thumbnails.format', 'jpg')).toLowerCase().trim() === 'png'; | 15 | const pngFormat = String(getConfigValue('thumbnails.format', 'jpg')).toLowerCase().trim() === 'png'; |
| 16 | 16 | ||
| 17 | /** @type {Record<string, number[]>} */ | 17 | /** @type {Record<string, number[]>} */ |
| 18 | const dimensions = { | 18 | export const dimensions = { |
| 19 | 'bg': getConfigValue('thumbnails.dimensions.bg', [160, 90]), | 19 | 'bg': getConfigValue('thumbnails.dimensions.bg', [160, 90]), |
| 20 | 'avatar': getConfigValue('thumbnails.dimensions.avatar', [96, 144]), | 20 | 'avatar': getConfigValue('thumbnails.dimensions.avatar', [96, 144]), |
| 21 | }; | 21 | }; |