Thumbnail user avatars

This commit is contained in:
SillyLossy
2023-03-24 21:12:57 +02:00
parent e609ed719e
commit fd2c74b8ec
3 changed files with 14 additions and 12 deletions

View File

@ -565,9 +565,9 @@ function printCharacters() {
$("#rm_print_characters_block").empty();
//console.log('printCharacters() -- sees '+characters.length+' characters.');
characters.forEach(function (item, i, arr) {
var this_avatar = default_avatar;
let this_avatar = default_avatar;
if (item.avatar != "none") {
this_avatar = "characters/" + item.avatar + "?" + Date.now();
this_avatar = `/thumbnail?type=avatar&file=${encodeURIComponent(item.avatar)}&${Date.now()}`;
} //RossAscends: changed 'prepend' to 'append' to make alphabetical sorting display correctly.
$("#rm_print_characters_block").append(
@ -625,7 +625,7 @@ async function getBackgrounds() {
//background = getData;
//console.log(getData.length);
for (const bg of getData) {
const thumbPath = `/thumbnail?type=bg&file=${bg}`;
const thumbPath = `/thumbnail?type=bg&file=${encodeURIComponent(bg)}`;
$("#bg_menu_content").append(
`<div class="bg_example" bgfile="${bg}" class="bg_example_img" style="background-image: url('${thumbPath}');">
<div bgfile="${bg}" class="bg_example_cross">
@ -814,9 +814,9 @@ function addOneMessage(mes, type = "normal") {
avatarImg = system_avatar;
} else {
if (characters[this_chid].avatar != "none") {
avatarImg = "characters/" + characters[this_chid].avatar;
avatarImg = `/thumbnail?type=avatar&file=${encodeURIComponent(characters[this_chid].avatar)}`;
if (is_mes_reload_avatar !== false) {
avatarImg += "?" + is_mes_reload_avatar;
avatarImg += "&" + is_mes_reload_avatar;
}
} else {
avatarImg = default_avatar;
@ -2546,7 +2546,7 @@ function select_selected_character(chid) {
//$("#avatar_div").css("display", "none");
var this_avatar = default_avatar;
if (characters[chid].avatar != "none") {
this_avatar = "characters/" + characters[chid].avatar;
this_avatar = "characters/" + encodeURIComponent(characters[chid].avatar);
}
$("#avatar_load_preview").attr("src", this_avatar + "?" + Date.now());
$("#name_div").css("display", "none");
@ -2698,7 +2698,7 @@ function read_bg_load(input) {
"url(" + e.target.result + ")"
);
$("#form_bg_download").after(
`<div class=bg_example bgfile="${html}" style="background-image: url('/thumbnail?type=bg&file=${html}');">
`<div class=bg_example bgfile="${html}" style="background-image: url('/thumbnail?type=bg&file=${encodeURIComponent(html)}');">
<img class=bg_example_cross src="img/cross.png">
</div>`
);

View File

@ -126,7 +126,7 @@ async function getGroupChat(id) {
: default_ch_mes;
mes["force_avatar"] =
character.avatar != "none"
? `characters/${character.avatar}?${Date.now()}`
? `/thumbnail?type=avatar&file=${encodeURIComponent(character.avatar)}&${Date.now()}`
: default_avatar;
chat.push(mes);
addOneMessage(mes);
@ -201,7 +201,7 @@ function getGroupAvatar(group) {
for (const member of group.members) {
const charIndex = characters.findIndex((x) => x.name === member);
if (charIndex !== -1 && characters[charIndex].avatar !== "none") {
const avatar = `characters/${characters[charIndex].avatar}#${Date.now()}`;
const avatar = `/thumbnail?type=avatar&file=${encodeURIComponent(characters[charIndex].avatar)}&${Date.now()}`;
memberAvatars.push(avatar);
}
if (memberAvatars.length === 4) {
@ -533,7 +533,7 @@ function select_group_chats(chat_id) {
for (let character of characters) {
const avatar =
character.avatar != "none"
? `characters/${character.avatar}#${Date.now()}`
? `/thumbnail?type=avatar&file=${encodeURIComponent(character.avatar)}&${Date.now()}`
: default_avatar;
const template = $("#group_member_template .group_member").clone();
template.data("id", character.name);

View File

@ -622,6 +622,7 @@ app.post("/editcharacter", urlencodedParser, async function (request, response)
img_path = "uploads/";
img_file = filedata.filename;
invalidateThumbnail('avatar', request.body.avatar_url);
await charaWrite(img_path + img_file, char, target_img, response, 'Character saved');
//response.send('Character saved');
}
@ -646,6 +647,7 @@ app.post("/deletecharacter", urlencodedParser, function (request, response) {
}
fs.rmSync(avatarPath);
invalidateThumbnail('avatar', request.body.avatar_url);
let dir_name = (request.body.avatar_url.replace('.png', ''));
if (dir_name !== sanitize(dir_name)) {
@ -1722,11 +1724,11 @@ async function generateThumbnail(type, file) {
return null;
}
const imageSizes = { 'bg': [160, 90], 'avatar': [42, 42] };
const imageSizes = { 'bg': [160, 90], 'avatar': [48, 48] };
const mySize = imageSizes[type];
const image = await jimp.read(pathToOriginalFile);
await image.cover(mySize[0], mySize[1]).quality(60).writeAsync(pathToCachedFile);
await image.cover(mySize[0], mySize[1]).quality(95).writeAsync(pathToCachedFile);
return pathToCachedFile;
}