mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01: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,7 +1,7 @@
|
||||
import child from 'child_process';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import AdmZip from 'adm-zip';
|
||||
import { BlobReader, ZipReader } from '@zip.js/zip.js';
|
||||
import { CAC } from 'cac';
|
||||
import fs from 'fs-extra';
|
||||
import superagent from 'superagent';
|
||||
@@ -34,7 +34,7 @@ function downloadAndExtractTgz(url: string, dest: string) {
|
||||
}
|
||||
async function downloadAndExtractZip(url: string, dest: string) {
|
||||
const res = await superagent.get(url).responseType('arraybuffer');
|
||||
await extractZip(new AdmZip(res.body), dest, true, true);
|
||||
await extractZip(new ZipReader(new BlobReader(new Blob([res.body]))), dest, { strip: true, overwrite: true });
|
||||
}
|
||||
const types = {
|
||||
'.tgz': downloadAndExtractTgz,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AdmZip from 'adm-zip';
|
||||
import * as Zip from '@zip.js/zip.js';
|
||||
import _AdmZip from 'adm-zip';
|
||||
import _ from 'lodash';
|
||||
import { ObjectId as ObjectID } from 'mongodb';
|
||||
import Schema from 'schemastery';
|
||||
@@ -10,11 +11,14 @@ export * from './utils';
|
||||
export * from './interface';
|
||||
export * from './typeutils';
|
||||
export {
|
||||
Schema, AdmZip, superagent, _, ObjectID,
|
||||
Schema, superagent, _, ObjectID, Zip,
|
||||
};
|
||||
/** @deprecated Use ZipReader/ZipWriter instead */
|
||||
export const AdmZip = _AdmZip;
|
||||
export const definePlugin = <T = never>(args: {
|
||||
using?: keyof Context[];
|
||||
inject?: keyof Context[] | Record<keyof Context, any>;
|
||||
apply: (ctx: Context, config: T) => Promise<void> | void;
|
||||
schema?: Schema<T>;
|
||||
name?: string;
|
||||
Config?: Schema<T>;
|
||||
}) => args;
|
||||
@@ -2,14 +2,16 @@
|
||||
import child from 'child_process';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import AdmZip from 'adm-zip';
|
||||
import { Readable } from 'stream';
|
||||
import { Entry, ZipReader } from '@zip.js/zip.js';
|
||||
import fs from 'fs-extra';
|
||||
import yaml from 'js-yaml';
|
||||
import { keyBy, pick } from 'lodash';
|
||||
import { Filter, ObjectId } from 'mongodb';
|
||||
import type { Readable } from 'stream';
|
||||
import { ProblemConfigFile, ProblemType } from '@hydrooj/common';
|
||||
import { Logger, size, streamToBuffer } from '@hydrooj/utils/lib/utils';
|
||||
import {
|
||||
extractZip, Logger, size, streamToBuffer,
|
||||
} from '@hydrooj/utils/lib/utils';
|
||||
import { Context } from '../context';
|
||||
import { FileUploadError, ProblemNotFoundError, ValidationError } from '../error';
|
||||
import type {
|
||||
@@ -456,18 +458,14 @@ export class ProblemModel {
|
||||
try {
|
||||
if (filepath.endsWith('.zip')) {
|
||||
tmpdir = path.join(os.tmpdir(), 'hydro', `${Math.random()}.import`);
|
||||
let zip: AdmZip;
|
||||
const zip = new ZipReader(Readable.toWeb(fs.createReadStream(filepath)));
|
||||
let entries: Entry[];
|
||||
try {
|
||||
zip = new AdmZip(filepath);
|
||||
entries = await zip.getEntries();
|
||||
} catch (e) {
|
||||
throw new ValidationError('zip', null, e.message);
|
||||
}
|
||||
await new Promise((resolve, reject) => {
|
||||
zip.extractAllToAsync(tmpdir, true, false, (err) => {
|
||||
if (err) reject(err);
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
await extractZip(entries, tmpdir);
|
||||
} else if (fs.statSync(filepath).isDirectory()) {
|
||||
tmpdir = filepath;
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user