Fix new group avatar upload

This commit is contained in:
Cohee 2023-10-08 11:34:39 +03:00
parent 57bf499ce9
commit 15cf87ef01

View File

@ -9,6 +9,7 @@ import {
saveBase64AsFile, saveBase64AsFile,
PAGINATION_TEMPLATE, PAGINATION_TEMPLATE,
waitUntilCondition, waitUntilCondition,
getBase64Async,
} from './utils.js'; } from './utils.js';
import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from "./RossAscends-mods.js"; import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from "./RossAscends-mods.js";
import { loadMovingUIState, sortEntitiesList } from './power-user.js'; import { loadMovingUIState, sortEntitiesList } from './power-user.js';
@ -1146,36 +1147,29 @@ async function uploadGroupAvatar(event) {
return; return;
} }
const e = await new Promise((resolve, reject) => { const result = await getBase64Async(file);
const reader = new FileReader();
reader.onload = resolve;
reader.onerror = reject;
reader.readAsDataURL(file);
});
$('#dialogue_popup').addClass('large_dialogue_popup wide_dialogue_popup'); $('#dialogue_popup').addClass('large_dialogue_popup wide_dialogue_popup');
const croppedImage = await callPopup(getCropPopup(e.target.result), 'avatarToCrop'); const croppedImage = await callPopup(getCropPopup(result), 'avatarToCrop');
if (!croppedImage) { if (!croppedImage) {
return; return;
} }
let thumbnail = await createThumbnail(croppedImage, 96, 144); let thumbnail = await createThumbnail(croppedImage, 200, 300);
//remove data:image/whatever;base64 //remove data:image/whatever;base64
thumbnail = thumbnail.replace(/^data:image\/[a-z]+;base64,/, ""); thumbnail = thumbnail.replace(/^data:image\/[a-z]+;base64,/, "");
let _thisGroup = groups.find((x) => x.id == openGroupId); let _thisGroup = groups.find((x) => x.id == openGroupId);
// filename should be group id + human readable timestamp // filename should be group id + human readable timestamp
const filename = `${_thisGroup.id}_${humanizedDateTime()}`; const filename = _thisGroup ? `${_thisGroup.id}_${humanizedDateTime()}` : humanizedDateTime();
let thumbnailUrl = await saveBase64AsFile(thumbnail, openGroupId.toString(), filename, 'jpg'); let thumbnailUrl = await saveBase64AsFile(thumbnail, String(openGroupId ?? ''), filename, 'jpg');
if (!openGroupId) { if (!openGroupId) {
$('#group_avatar_preview img').attr('src', thumbnailUrl); $('#group_avatar_preview img').attr('src', thumbnailUrl);
$('#rm_group_restore_avatar').show(); $('#rm_group_restore_avatar').show();
return; return;
} }
_thisGroup.avatar_url = thumbnailUrl; _thisGroup.avatar_url = thumbnailUrl;
$("#group_avatar_preview").empty().append(getGroupAvatar(_thisGroup)); $("#group_avatar_preview").empty().append(getGroupAvatar(_thisGroup));
$("#rm_group_restore_avatar").show(); $("#rm_group_restore_avatar").show();