Commit bb6ee175 authored by nanahira's avatar nanahira

update archive

parent 71c3fa84
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
......@@ -15,6 +34,8 @@ const DuelLog_1 = require("./entities/DuelLog");
const DuelLogPlayer_1 = require("./entities/DuelLogPlayer");
const User_1 = require("./entities/User");
const RandomDuelScore_1 = require("./entities/RandomDuelScore");
const jszip_1 = __importDefault(require("jszip"));
const fs = __importStar(require("fs"));
class DataManager {
constructor(config, log) {
this.config = config;
......@@ -318,6 +339,36 @@ class DataManager {
const allDuelLogs = await this.getDuelLogFromCondition(data);
return allDuelLogs.map(duelLog => duelLog.replayFileName);
}
async getReplayArchiveStreamFromCondition(rootPath, data) {
const filenames = await this.getReplayFilenamesFromCondition(data);
if (!filenames.length) {
return null;
}
try {
const zip = new jszip_1.default();
for (let fileName of filenames) {
const filePath = `${rootPath}${fileName}`;
try {
await fs.promises.access(filePath);
zip.file(fileName, fs.promises.readFile(filePath));
}
catch (e) {
this.log.warn(`Errored archiving ${filePath}: ${e.toString()}`);
continue;
}
}
return zip.generateNodeStream({
compression: "DEFLATE",
compressionOptions: {
level: 9
}
});
}
catch (e2) {
this.log.warn(`Errored creating archive: ${e2.toString()}`);
return null;
}
}
async clearDuelLog() {
const runner = this.db.createQueryRunner();
try {
......
......@@ -11,6 +11,8 @@ import {Deck} from "./DeckEncoder";
import {DuelLogPlayer} from "./entities/DuelLogPlayer";
import {User} from "./entities/User";
import {RandomDuelScore} from "./entities/RandomDuelScore";
import JSZip from "jszip";
import * as fs from "fs";
interface BasePlayerInfo {
name: string;
......@@ -349,6 +351,34 @@ export class DataManager {
const allDuelLogs = await this.getDuelLogFromCondition(data);
return allDuelLogs.map(duelLog => duelLog.replayFileName);
}
async getReplayArchiveStreamFromCondition(rootPath: string, data: DuelLogQuery) {
const filenames = await this.getReplayFilenamesFromCondition(data);
if(!filenames.length) {
return null;
}
try {
const zip = new JSZip();
for(let fileName of filenames) {
const filePath = `${rootPath}${fileName}`;
try {
await fs.promises.access(filePath);
zip.file(fileName, fs.promises.readFile(filePath));
} catch(e) {
this.log.warn(`Errored archiving ${filePath}: ${e.toString()}`)
continue;
}
}
return zip.generateNodeStream({
compression: "DEFLATE",
compressionOptions: {
level: 9
}
});
} catch(e2) {
this.log.warn(`Errored creating archive: ${e2.toString()}`)
return null;
}
}
async clearDuelLog() {
const runner = this.db.createQueryRunner();
try {
......
......@@ -846,6 +846,11 @@
"minimatch": "^3.0.4"
}
},
"immediate": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz",
"integrity": "sha1-nbHb0Pr43m++D13V5Wu2BigN5ps="
},
"inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
......@@ -954,11 +959,30 @@
"verror": "1.10.0"
}
},
"jszip": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/jszip/-/jszip-3.5.0.tgz",
"integrity": "sha512-WRtu7TPCmYePR1nazfrtuF216cIVon/3GWOvHS9QR5bIwSbnxtdpma6un3jyGGNhHsKCSzn5Ypk+EkDRvTGiFA==",
"requires": {
"lie": "~3.3.0",
"pako": "~1.0.2",
"readable-stream": "~2.3.6",
"set-immediate-shim": "~1.0.1"
}
},
"lazy": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/lazy/-/lazy-1.0.11.tgz",
"integrity": "sha1-2qBoIGKCVCwIgojpdcKXwa53tpA="
},
"lie": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz",
"integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==",
"requires": {
"immediate": "~3.0.5"
}
},
"lines-and-columns": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.1.6.tgz",
......@@ -1370,6 +1394,11 @@
"resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-0.3.1.tgz",
"integrity": "sha1-zWLmCvjX/qinBexP+ZCHHEaHHyc="
},
"pako": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
},
"parent-require": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/parent-require/-/parent-require-1.0.0.tgz",
......@@ -1657,6 +1686,11 @@
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc="
},
"set-immediate-shim": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/set-immediate-shim/-/set-immediate-shim-1.0.1.tgz",
"integrity": "sha1-SysbJ+uAip+NzEgaWOXlb1mfP2E="
},
"sha.js": {
"version": "2.4.11",
"resolved": "https://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz",
......
......@@ -3721,38 +3721,20 @@ if true
return
else
try
archive_name = moment().format('YYYY-MM-DD HH-mm-ss') + ".zip"
archive_args = ["a", "-mx0", "-y", archive_name]
check = false
for filename in await dataManager.getReplayFilenamesFromCondition(getDuelLogQueryFromQs(u.query))
check = true
archive_args.push(filename)
if !check
archiveStream = await dataManager.getReplayArchiveStreamFromCondition(settings.modules.tournament_mode.replay_path, getDuelLogQueryFromQs(u.query))
if !archiveStream
response.writeHead(403)
response.end("Duel logs not found.")
response.end("Replay not found.")
return
archive_process = spawn settings.modules.tournament_mode.replay_archive_tool, archive_args, {cwd: settings.modules.tournament_mode.replay_path}
archive_process.on 'error', (err)=>
response.writeHead(403)
response.end("Failed packing replays. " + err)
return
archive_process.on 'exit', (code)=>
fs.readFile(settings.modules.tournament_mode.replay_path + archive_name, (error, buffer)->
if error
response.writeHead(403)
response.end("Failed sending replays. " + error)
return
else
response.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Disposition": "attachment" })
response.end(buffer)
return
)
archive_process.stdout.setEncoding 'utf8'
archive_process.stdout.on 'data', (data)=>
log.info "archive process: " + data
archive_process.stderr.setEncoding 'utf8'
archive_process.stderr.on 'data', (data)=>
log.warn "archive error: " + data
response.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Disposition": "attachment" })
archiveStream.on "data", (data) ->
response.write data
archiveStream.on "end", () ->
response.end()
archiveStream.on "close", () ->
log.warn("Archive closed")
archiveStream.on "error", (error) ->
log.warn("Archive error: #{error}")
catch error
response.writeHead(403)
response.end("Failed reading replays. " + error)
......
......@@ -4893,7 +4893,7 @@
return callback + "( " + text + " );";
};
httpRequestListener = async function(request, response) {
var archive_args, archive_name, archive_process, buffer, check, death_room_found, duellog, e, err, error, filename, getpath, j, len, parseQueryString, pass_validated, ref, roomsjson, success, u;
var archiveStream, buffer, death_room_found, duellog, e, err, error, filename, getpath, parseQueryString, pass_validated, roomsjson, success, u;
parseQueryString = true;
u = url.parse(request.url, parseQueryString);
//pass_validated = u.query.pass == settings.modules.http.password
......@@ -4966,48 +4966,27 @@
return;
} else {
try {
archive_name = moment().format('YYYY-MM-DD HH-mm-ss') + ".zip";
archive_args = ["a", "-mx0", "-y", archive_name];
check = false;
ref = (await dataManager.getReplayFilenamesFromCondition(getDuelLogQueryFromQs(u.query)));
for (j = 0, len = ref.length; j < len; j++) {
filename = ref[j];
check = true;
archive_args.push(filename);
}
if (!check) {
archiveStream = (await dataManager.getReplayArchiveStreamFromCondition(settings.modules.tournament_mode.replay_path, getDuelLogQueryFromQs(u.query)));
if (!archiveStream) {
response.writeHead(403);
response.end("Duel logs not found.");
response.end("Replay not found.");
return;
}
archive_process = spawn(settings.modules.tournament_mode.replay_archive_tool, archive_args, {
cwd: settings.modules.tournament_mode.replay_path
response.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Disposition": "attachment"
});
archive_process.on('error', (err) => {
response.writeHead(403);
response.end("Failed packing replays. " + err);
archiveStream.on("data", function(data) {
return response.write(data);
});
archive_process.on('exit', (code) => {
return fs.readFile(settings.modules.tournament_mode.replay_path + archive_name, function(error, buffer) {
if (error) {
response.writeHead(403);
response.end("Failed sending replays. " + error);
} else {
response.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Disposition": "attachment"
});
response.end(buffer);
}
});
archiveStream.on("end", function() {
return response.end();
});
archive_process.stdout.setEncoding('utf8');
archive_process.stdout.on('data', (data) => {
return log.info("archive process: " + data);
archiveStream.on("close", function() {
return log.warn("Archive closed");
});
archive_process.stderr.setEncoding('utf8');
archive_process.stderr.on('data', (data) => {
return log.warn("archive error: " + data);
archiveStream.on("error", function(error) {
return log.warn(`Archive error: ${error}`);
});
} catch (error1) {
error = error1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment