Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
feat : cp949ToUTF8 for c
Browse files Browse the repository at this point in the history
  • Loading branch information
kasterra committed May 27, 2024
1 parent d7ff95d commit 803119d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
18 changes: 16 additions & 2 deletions app/API/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,35 @@ import {
NotFoundError,
RequestTooLongError,
} from "~/util/errors";
import { cp949ToUTF8 } from "~/util/file";

export async function submit(
token: string,
problem_id: string,
formdata: FormData
): Promise<SubmissionResponse> {
if (formdata.get("language") === "c") {
const fileList = formdata.getAll("codes") as File[];
if (fileList.length > 0) {
formdata.delete("codes");
const processedFiles = await Promise.all(
fileList.map(async (file) => {
return await cp949ToUTF8(file);
})
);
processedFiles.forEach((file) => formdata.append("codes", file));
}
}
if (formdata.get("language") === "java") {
const fileList = formdata.getAll("codes") as File[];
const code = formdata.get("code") as string;
if (fileList.length > 0) {
formdata.delete("codes");
const processedFiles = await Promise.all(
fileList.map(async (file) => await removePackageStatementFromFile(file))
fileList.map(async (file) => {
return await removePackageStatementFromFile(file);
})
);

processedFiles.forEach((file) => formdata.append("codes", file));
} else if (code !== "") {
formdata.delete("code");
Expand Down
25 changes: 25 additions & 0 deletions app/util/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Buffer } from "buffer";
import iconv from "iconv-lite";
import isUtf8 from "isutf8";

export async function cp949ToUTF8(file: File): Promise<File> {
return new Promise((resolve, reject) => {
const reader = new FileReader();

reader.onload = async () => {
const result = reader.result as ArrayBuffer;
const buf = Buffer.from(result);
if (isUtf8(buf)) {
resolve(file);
}
const decoded = iconv.decode(buf, "cp949");
console.log(decoded);
resolve(new File([decoded], file.name, { type: file.type }));
};

reader.onerror = () => {
reject(reader.error);
};
reader.readAsArrayBuffer(file);
});
}
44 changes: 38 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
"@remix-run/node": "^2.9.2",
"@remix-run/react": "^2.9.2",
"@remix-run/serve": "^2.9.2",
"buffer": "^6.0.3",
"cross-env": "^7.0.3",
"exceljs": "^4.4.0",
"file-saver": "^2.0.5",
"iconv-lite": "^0.6.3",
"isbot": "^3.6.8",
"isutf8": "^4.0.0",
"jszip": "^3.10.1",
"prismjs": "^1.29.0",
"react": "^18.2.0",
Expand Down

0 comments on commit 803119d

Please sign in to comment.