mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 09:38:19 +08:00
core: change to @zip.js/zip.js
This commit is contained in:
17 files changed
+129
-108
No files matched your search
@@ -1,10 +1,11 @@
|
||||
import AdmZip from 'adm-zip';
|
||||
import { Readable } from 'stream';
|
||||
import { BlobWriter, TextReader, ZipWriter } from '@zip.js/zip.js';
|
||||
import { stringify as toCSV } from 'csv-stringify/sync';
|
||||
import { escapeRegExp, pick } from 'lodash';
|
||||
import moment from 'moment-timezone';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import {
|
||||
Counter, sortFiles, streamToBuffer, Time, yaml,
|
||||
Counter, sortFiles, Time, yaml,
|
||||
} from '@hydrooj/utils/lib/utils';
|
||||
import { Context, Service } from '../context';
|
||||
import {
|
||||
@@ -399,7 +400,7 @@ export class ContestCodeHandler extends Handler {
|
||||
}
|
||||
}
|
||||
}
|
||||
const zip = new AdmZip();
|
||||
const zip = new ZipWriter(new BlobWriter('application/zip'), { bufferedWrite: true });
|
||||
const rdocs = await record.getMulti(domainId, {
|
||||
_id: { $in: Array.from(Object.keys(rnames)).map((id) => new ObjectId(id)) },
|
||||
}).toArray();
|
||||
@@ -407,15 +408,15 @@ export class ContestCodeHandler extends Handler {
|
||||
if (rdoc.files?.code) {
|
||||
const [id, filename] = rdoc.files?.code?.split('#') || [];
|
||||
if (!id) return;
|
||||
zip.addFile(
|
||||
await zip.add(
|
||||
`${rnames[rdoc._id.toHexString()]}.${filename || 'txt'}`,
|
||||
await streamToBuffer(await storage.get(`submission/${id}`)),
|
||||
Readable.toWeb(await storage.get(`submission/${id}`)),
|
||||
);
|
||||
} else if (rdoc.code) {
|
||||
zip.addFile(`${rnames[rdoc._id.toHexString()]}.${rdoc.lang}`, Buffer.from(rdoc.code));
|
||||
await zip.add(`${rnames[rdoc._id.toHexString()]}.${rdoc.lang}`, new TextReader(rdoc.code));
|
||||
}
|
||||
}));
|
||||
this.binary(zip.toBuffer(), `${tdoc.title}.zip`);
|
||||
this.binary(await zip.close(), `${tdoc.title}.zip`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import AdmZip from 'adm-zip';
|
||||
import { createReadStream } from 'fs';
|
||||
import { PassThrough, Readable, Writable } from 'stream';
|
||||
import { Entry, ZipReader } from '@zip.js/zip.js';
|
||||
import { readFile } from 'fs-extra';
|
||||
import {
|
||||
escapeRegExp, flattenDeep, intersection, pick,
|
||||
@@ -709,20 +711,24 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
|
||||
filename ||= file.originalFilename || String.random(16);
|
||||
const files = [];
|
||||
if (filename.endsWith('.zip') && type === 'testdata') {
|
||||
let zip: AdmZip;
|
||||
const zip = new ZipReader(Readable.toWeb(createReadStream(file.filepath)));
|
||||
let entries: Entry[];
|
||||
try {
|
||||
zip = new AdmZip(file.filepath);
|
||||
entries = await zip.getEntries();
|
||||
} catch (e) {
|
||||
throw new ValidationError('zip', null, e.message);
|
||||
}
|
||||
const entries = zip.getEntries();
|
||||
for (const entry of entries) {
|
||||
if (!entry.name || entry.isDirectory) continue;
|
||||
if (!entry.filename || entry.directory) continue;
|
||||
files.push({
|
||||
type,
|
||||
name: sanitize(entry.name),
|
||||
size: entry.header.size,
|
||||
data: () => entry.getData(),
|
||||
name: sanitize(entry.filename),
|
||||
size: entry.uncompressedSize,
|
||||
data: () => {
|
||||
const pass = new PassThrough();
|
||||
entry.getData(Writable.toWeb(pass));
|
||||
return pass;
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user