Refactor image handling in character import/export functions to remove unnecessary reencoding
This commit is contained in:
@@ -277,7 +277,7 @@ export async function importCharacterProcess(f:{
|
||||
if(parsed.spec !== 'chara_card_v2' && parsed.spec !== 'chara_card_v3'){
|
||||
const charaData:OldTavernChar = JSON.parse(Buffer.from(readedChara, 'base64').toString('utf-8'))
|
||||
console.log(charaData)
|
||||
const imgp = await saveAsset(await reencodeImage(img))
|
||||
const imgp = await saveAsset(img)
|
||||
db.characters.push(convertOffSpecCards(charaData, imgp))
|
||||
setDatabaseLite(db)
|
||||
alertNormal(language.importedCharacter)
|
||||
@@ -633,7 +633,7 @@ async function importCharacterCardSpec(card:CharacterCardV2Risu|CharacterCardV3,
|
||||
|
||||
const data = card.data
|
||||
console.log(card)
|
||||
let im = img ? await saveAsset(await reencodeImage(img)) : undefined
|
||||
let im = img ? await saveAsset(img) : undefined
|
||||
let db = getDatabase()
|
||||
|
||||
const risuext = safeStructuredClone(data.extensions.risuai)
|
||||
@@ -1140,7 +1140,7 @@ export async function exportCharacterCard(char:character, type:'png'|'json'|'cha
|
||||
const spec:'v2'|'v3' = arg.spec ?? 'v2' //backward compatibility
|
||||
try{
|
||||
char.image = ''
|
||||
img = await reencodeImage(img)
|
||||
img = type === 'png' ? (await reencodeImage(img)) : img
|
||||
const localWriter = arg.writer ?? (new LocalWriter())
|
||||
if(!arg.writer && type !== 'json'){
|
||||
const nameExt = {
|
||||
|
||||
@@ -2,14 +2,11 @@ import { get, writable } from "svelte/store";
|
||||
import { saveImage, setDatabase, type character, type Chat, defaultSdDataFunc, type loreBook, getDatabase, getCharacterByIndex, setCharacterByIndex } from "./storage/database.svelte";
|
||||
import { alertAddCharacter, alertConfirm, alertError, alertNormal, alertSelect, alertStore, alertWait } from "./alert";
|
||||
import { language } from "../lang";
|
||||
import { decode as decodeMsgpack } from "msgpackr";
|
||||
import { checkNullish, findCharacterbyId, getUserName, selectMultipleFile, selectSingleFile, sleep } from "./util";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { MobileGUIStack, OpenRealmStore, selectedCharID } from "./stores.svelte";
|
||||
import { checkCharOrder, downloadFile, getFileSrc } from "./globalApi.svelte";
|
||||
import { reencodeImage } from "./process/files/image";
|
||||
import { updateInlayScreen } from "./process/inlayScreen";
|
||||
import { PngChunk } from "./pngChunk";
|
||||
import { parseMarkdownSafe } from "./parser.svelte";
|
||||
import { translateHTML } from "./translator/translator";
|
||||
import { doingChat } from "./process/index.svelte";
|
||||
@@ -84,7 +81,7 @@ export async function selectCharImg(charIndex:number) {
|
||||
}
|
||||
const img = selected.data
|
||||
let db = getDatabase()
|
||||
const imgp = await saveImage(await reencodeImage(img))
|
||||
const imgp = await saveImage(img)
|
||||
dumpCharImage(charIndex)
|
||||
db.characters[charIndex].image = imgp
|
||||
setDatabase(db)
|
||||
@@ -668,56 +665,6 @@ function dataURLtoBuffer(string:string){
|
||||
return Buffer.from(data, 'base64');
|
||||
}
|
||||
|
||||
export async function addDefaultCharacters() {
|
||||
const imgs = [fetch('/sample/rika.png'),fetch('/sample/yuzu.png')]
|
||||
|
||||
alertStore.set({
|
||||
type: 'wait',
|
||||
msg: `Loading Sample bots...`
|
||||
})
|
||||
|
||||
for(const img of imgs){
|
||||
const imgBuffer = await (await img).arrayBuffer()
|
||||
const readed = PngChunk.read(Buffer.from(imgBuffer), ["risuai"])?.risuai
|
||||
await sleep(10)
|
||||
const va = decodeMsgpack(Buffer.from(readed,'base64')) as any
|
||||
if(va.type !== 101){
|
||||
alertError(language.errors.noData)
|
||||
return
|
||||
}
|
||||
let char:character = va.data
|
||||
let db = getDatabase()
|
||||
if(char.emotionImages && char.emotionImages.length > 0){
|
||||
for(let i=0;i<char.emotionImages.length;i++){
|
||||
await sleep(10)
|
||||
const imgp = await saveImage(char.emotionImages[i][1] as any)
|
||||
char.emotionImages[i][1] = imgp
|
||||
}
|
||||
}
|
||||
char.chats = [{
|
||||
message: [],
|
||||
note: '',
|
||||
name: 'Chat 1',
|
||||
localLore: []
|
||||
}]
|
||||
|
||||
if(checkNullish(char.sdData)){
|
||||
char.sdData = defaultSdDataFunc()
|
||||
}
|
||||
|
||||
char.chatPage = 0
|
||||
char.image = await saveImage(await reencodeImage(Buffer.from(imgBuffer)))
|
||||
char.chaId = uuidv4()
|
||||
db.characters.push(characterFormatUpdate(char))
|
||||
setDatabase(db)
|
||||
}
|
||||
|
||||
alertStore.set({
|
||||
type: 'none',
|
||||
msg: ''
|
||||
})
|
||||
}
|
||||
|
||||
export async function removeChar(index:number,name:string, type:'normal'|'permanent'|'permanentForce' = 'normal'){
|
||||
const db = getDatabase()
|
||||
if(type !== 'permanentForce'){
|
||||
|
||||
Reference in New Issue
Block a user