|
import fs from 'node:fs'; |
|
import { Buffer } from 'node:buffer'; |
|
|
|
import encode from './png/encode.js'; |
|
import extract from 'png-chunks-extract'; |
|
import PNGtext from 'png-chunk-text'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const write = (image, data) => { |
|
const chunks = extract(new Uint8Array(image)); |
|
const tEXtChunks = chunks.filter(chunk => chunk.name === 'tEXt'); |
|
|
|
|
|
for (const tEXtChunk of tEXtChunks) { |
|
const data = PNGtext.decode(tEXtChunk.data); |
|
if (data.keyword.toLowerCase() === 'chara' || data.keyword.toLowerCase() === 'ccv3') { |
|
chunks.splice(chunks.indexOf(tEXtChunk), 1); |
|
} |
|
} |
|
|
|
|
|
const base64EncodedData = Buffer.from(data, 'utf8').toString('base64'); |
|
chunks.splice(-1, 0, PNGtext.encode('chara', base64EncodedData)); |
|
|
|
|
|
try { |
|
|
|
const v3Data = JSON.parse(data); |
|
v3Data.spec = 'chara_card_v3'; |
|
v3Data.spec_version = '3.0'; |
|
|
|
const base64EncodedData = Buffer.from(JSON.stringify(v3Data), 'utf8').toString('base64'); |
|
chunks.splice(-1, 0, PNGtext.encode('ccv3', base64EncodedData)); |
|
} catch (error) { |
|
|
|
} |
|
|
|
const newBuffer = Buffer.from(encode(chunks)); |
|
return newBuffer; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const read = (image) => { |
|
const chunks = extract(new Uint8Array(image)); |
|
|
|
const textChunks = chunks.filter((chunk) => chunk.name === 'tEXt').map((chunk) => PNGtext.decode(chunk.data)); |
|
|
|
if (textChunks.length === 0) { |
|
console.error('PNG metadata does not contain any text chunks.'); |
|
throw new Error('No PNG metadata.'); |
|
} |
|
|
|
const ccv3Index = textChunks.findIndex((chunk) => chunk.keyword.toLowerCase() === 'ccv3'); |
|
|
|
if (ccv3Index > -1) { |
|
return Buffer.from(textChunks[ccv3Index].text, 'base64').toString('utf8'); |
|
} |
|
|
|
const charaIndex = textChunks.findIndex((chunk) => chunk.keyword.toLowerCase() === 'chara'); |
|
|
|
if (charaIndex > -1) { |
|
return Buffer.from(textChunks[charaIndex].text, 'base64').toString('utf8'); |
|
} |
|
|
|
console.error('PNG metadata does not contain any character data.'); |
|
throw new Error('No PNG metadata.'); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const parse = async (cardUrl, format) => { |
|
let fileFormat = format === undefined ? 'png' : format; |
|
|
|
switch (fileFormat) { |
|
case 'png': { |
|
const buffer = fs.readFileSync(cardUrl); |
|
return read(buffer); |
|
} |
|
} |
|
|
|
throw new Error('Unsupported format'); |
|
}; |
|
|
|
|