Load bg thumbnail config from backend

dfc1688eb5f4070d5fec76d55985f92db6deb0af

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +14 -7Ignore whitespace
public/scripts/backgrounds.js+9 -3
@@ -27,6 +27,11 @@ const THUMBNAIL_STORAGE = localforage.createInstance({ name: 'SillyTavern_Thumbn
2727 */
2828const THUMBNAIL_BLOBS = new Map();
2929
30+const THUMBNAIL_CONFIG = {
31+ width: 160,
32+ height: 90,
33+};
34+
3035/**
3136 * Global IntersectionObserver instance for lazy loading backgrounds
3237 * @type {IntersectionObserver|null}
@@ -282,7 +287,7 @@ async function getThumbnailFromStorage(bg) {
282287 }
283288 const imageBlob = await response.blob();
284289 const imageBase64 = await getBase64Async(imageBlob);
285290 const thumbnailBase64 = await createThumbnail(imageBase64, THUMBNAIL_CONFIG.width, THUMBNAIL_CONFIG.height);
286291 const thumbnailBlob = await fetch(thumbnailBase64).then(res => res.blob());
287292 await THUMBNAIL_STORAGE.setItem(bg, thumbnailBlob);
288293 const blobUrl = URL.createObjectURL(thumbnailBlob);
@@ -445,9 +450,10 @@ export async function getBackgrounds() {
445450 body: JSON.stringify({}),
446451 });
447452 if (response.ok) {
448453 const getData{ images, config } = await response.json();
454+ Object.assign(THUMBNAIL_CONFIG, config);
449455 $('#bg_menu_content').children('div').remove();
450456 for (const bg of getDataimages) {
451457 const template = await getBackgroundFromTemplate(bg, false);
452458 $('#bg_menu_content').append(template);
453459 }
src/endpoints/backgrounds.js+4 -3
@@ -4,15 +4,16 @@ import path from 'node:path';
44import express from 'express';
55import sanitize from 'sanitize-filename';
66
77import { dimensions, invalidateThumbnail } from './thumbnails.js';
88import { getImages } from '../util.js';
99import { getFileNameValidationFunction } from '../middleware/validateFileName.js';
1010
1111export const router = express.Router();
1212
1313router.post('/all', function (request, response) {
1414 varconst 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 });
1617});
1718
1819router.post('/delete', getFileNameValidationFunction('bg'), function (request, response) {
src/endpoints/thumbnails.js+1 -1
@@ -15,7 +15,7 @@ const quality = Math.min(100, Math.max(1, parseInt(getConfigValue('thumbnails.qu
1515const pngFormat = String(getConfigValue('thumbnails.format', 'jpg')).toLowerCase().trim() === 'png';
1616
1717/** @type {Record<string, number[]>} */
1818export const dimensions = {
1919 'bg': getConfigValue('thumbnails.dimensions.bg', [160, 90]),
2020 'avatar': getConfigValue('thumbnails.dimensions.avatar', [96, 144]),
2121};