Skip to content

Commit 5ea61ae

Browse files
committed
encoding detect
1 parent 2f5012d commit 5ea61ae

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
"@neutralinojs/neu": "^11.3.0",
2323
"@types/node": "^20.17.1",
2424
"@yao-pkg/pkg": "^5.16.1",
25+
"chardet": "^2.1.0",
2526
"dejavu-fonts-ttf": "^2.37.3",
2627
"eslint": "^8.57.1",
2728
"eslint-import-resolver-typescript": "^3.6.3",
29+
"iconv-lite": "^0.6.3",
2830
"typescript": "5.4.5"
2931
},
3032
"resolutions": {

packages/server/client/balloon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ async function fetchTask(c) {
107107
if (timer) clearTimeout(timer);
108108
logger.info('Fetching balloon task from tools server...');
109109
try {
110-
const { body } = await post(`${c.server}/client/${c.token}/balloon`).send();
110+
const { body } = await post(`${c.server}client/${c.token}/balloon`).send();
111111
if (body.balloons) {
112112
for (const doc of body.balloons) {
113113
logger.info(`Print balloon task ${doc.teamid}#${doc.balloonid}...`);
114114
await printBalloon(doc, config.balloonLang);
115-
await post(`${c.server}/client/${c.token}/doneballoon/${doc.balloonid}`);
115+
await post(`${c.server}client/${c.token}/doneballoon/${doc.balloonid}`);
116116
logger.info(`Print task ${doc.teamid}#${doc.balloonid} completed.`);
117117
}
118118
} else {

packages/server/client/printer.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable no-await-in-loop */
22
import path from 'path';
3+
import chardet from 'chardet';
4+
import * as iconv from 'iconv-lite';
35
import { PDFDocument } from 'pdf-lib';
46
import superagent from 'superagent';
57
import { config, saveConfig } from '../config';
@@ -35,12 +37,19 @@ const mergePDFs = async (files: string[], output: string) => {
3537
return fs.writeFileSync(output, await pdf.save());
3638
};
3739

38-
export async function ConvertCodeToPDF(code, lang, filename, team, location, codeColor = false) {
40+
function toUtf8(code: Buffer) {
41+
const info = chardet.detect(code);
42+
logger.debug(`detected as ${info}`);
43+
if (!info) return code.toString('utf8');
44+
return iconv.decode(code, info).toString();
45+
}
46+
47+
export async function ConvertCodeToPDF(code: Buffer, lang, filename, team, location, codeColor = false) {
3948
compiler ||= await createTypstCompiler();
4049
const fakeFilename = String.random(8); // cubercsl: do not trust filename from user
4150
const typst = generateTypst(team, location, fakeFilename, filename, lang, codeColor);
4251
compiler.addSource('/main.typst', typst);
43-
compiler.addSource(`/${fakeFilename}`, code);
52+
compiler.addSource(`/${fakeFilename}`, toUtf8(code));
4453
const docs = await compiler.compile({
4554
format: 'pdf',
4655
mainFilePath: '/main.typst',
@@ -58,7 +67,14 @@ export async function printFile(docs) {
5867
const {
5968
_id, tid, code, lang, filename, team, location,
6069
} = doc;
61-
const pdf = await ConvertCodeToPDF(code || 'empty file', lang, filename, team, location, config.printColor);
70+
const pdf = await ConvertCodeToPDF(
71+
code ? Buffer.from(code, 'base64') : Buffer.from('empty file'),
72+
lang,
73+
filename,
74+
team,
75+
location,
76+
config.printColor,
77+
);
6278
fs.writeFileSync(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`), pdf);
6379
files.push(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`));
6480
}

packages/server/handler/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ClientPrintConnectHandler extends Handler {
6161
return;
6262
}
6363
try {
64-
code.code = fs.readFileSync(path.resolve(process.cwd(), 'data/codes', `${code.tid}#${code._id}`)).toString();
64+
code.code = fs.readFileSync(path.resolve(process.cwd(), 'data/codes', `${code.tid}#${code._id}`)).toString('base64');
6565
} catch (e) {
6666
logger.error(e);
6767
}

packages/server/handler/printer.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ class PrintAdminHandler extends AuthHandler {
2323
throw new ValidationError('Code', null, 'Code not found');
2424
}
2525
fs.ensureDirSync(path.resolve(process.cwd(), 'data/.pdf'));
26-
code.code = fs.readFileSync(path.resolve(process.cwd(), 'data/codes', `${code.tid}#${code._id}`)).toString();
27-
const doc = await ConvertCodeToPDF(code.code || 'empty file', code.lang, code.filename, code.team, code.location, params.color ?? true);
26+
const content = fs.readFileSync(path.resolve(process.cwd(), 'data/codes', `${code.tid}#${code._id}`));
27+
const doc = await ConvertCodeToPDF(
28+
content,
29+
code.lang,
30+
code.filename,
31+
code.team,
32+
code.location,
33+
params.color ?? true,
34+
);
2835
this.response.type = 'application/pdf';
2936
this.response.disposition = 'attachment; filename="code.pdf"';
3037
this.response.body = Buffer.from(doc);
@@ -78,11 +85,11 @@ class CodeHandler extends Handler {
7885
printer: '',
7986
done: 0,
8087
});
81-
const codeFile = code || fs.readFileSync(this.request.files.file.filepath).toString();
88+
const codeFile = code || fs.readFileSync(this.request.files.file.filepath);
8289
if (!codeFile) throw new BadRequestError('Code', null, 'Code is empty');
8390
if (codeFile.length > 256 * 1024) throw new BadRequestError('Code', null, 'Code is larger than 256KB');
8491
fs.ensureDirSync(path.resolve(process.cwd(), 'data/codes'));
85-
fs.writeFileSync(path.resolve(process.cwd(), 'data/codes', `${team}#${res._id}`), code || fs.readFileSync(this.request.files.file.filepath));
92+
fs.writeFileSync(path.resolve(process.cwd(), 'data/codes', `${team}#${res._id}`), codeFile);
8693
this.response.body = `The code has been submitted. Code Print ID: ${team}#${res._id}`;
8794
logger.info(`Team(${team}): ${tname} submitted code. Code Print ID: ${team}#${res._id}`);
8895
await this.ctx.parallel('print/newTask');

0 commit comments

Comments
 (0)