changed up the zip package, fixed bad zip issues

This commit is contained in:
oat 2020-12-26 23:25:53 +03:00
parent 564d71c48f
commit 88fc57da82
Signed by: oat
GPG key ID: DD83A9617A252385
3 changed files with 20 additions and 28 deletions

19
package-lock.json generated
View file

@ -174,15 +174,6 @@
"@types/node": "*"
}
},
"@types/mongoose": {
"version": "5.10.3",
"resolved": "https://registry.npmjs.org/@types/mongoose/-/mongoose-5.10.3.tgz",
"integrity": "sha512-VfdnaFImXEJZZiuL2ID/ysZs4inOIjxwrAnUgkr5eum2O2BLhFkiSI0i87AwignVva1qWTJ3H3DyM0Rf4USJ4A==",
"requires": {
"@types/mongodb": "*",
"@types/node": "*"
}
},
"@types/node": {
"version": "14.14.16",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.16.tgz",
@ -365,6 +356,11 @@
"integrity": "sha512-K0Ptm/47OKfQRpNQ2J/oIN/3QYiK6FwW+eJbILhsdxh2WTLdl+30o8aGdTbm5JbffpFFAg/g+zi1E+jvJha5ng==",
"dev": true
},
"adm-zip": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.1.tgz",
"integrity": "sha512-a5ABmIFUJ9OxHV5zrXM9Q41JzpRIflFtdgpL4UQM9DsTHHxQzPRaeyAdnMW7kxL0NRWm/NHafJdj6pO+ty7L2g=="
},
"ajv": {
"version": "6.12.6",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
@ -1593,11 +1589,6 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"node-stream-zip": {
"version": "1.12.0",
"resolved": "https://registry.npmjs.org/node-stream-zip/-/node-stream-zip-1.12.0.tgz",
"integrity": "sha512-HZ3XehqShTFj9gHauRJ3Bri9eiCTOII7/crtXzURtT14NdnOFs9Ia5E82W7z3izVBNx760tqwddxrBJVG52Y1Q=="
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",

View file

@ -12,7 +12,7 @@
"license": "MIT",
"dependencies": {
"@types/express": "github:types/express",
"@types/mongoose": "^5.10.3",
"adm-zip": "^0.5.1",
"axios": "^0.20.0",
"connect-mongo": "^3.2.0",
"dotenv": "^8.2.0",
@ -21,7 +21,6 @@
"express-session": "^1.17.1",
"mongoose": "^5.11.8",
"mongoose-int32": "^0.4.1",
"node-stream-zip": "^1.12.0",
"serve-favicon": "^2.5.0",
"typescript": "^4.1.3",
"uuid": "^8.3.2",

View file

@ -1,6 +1,6 @@
import { tmpdir } from 'os';
import * as fs from 'fs';
const StreamZip = require('node-stream-zip');
import * as AdmZip from 'adm-zip';
import { parseSM } from './lib/smparse';
import { File, User } from './schema';
@ -21,23 +21,21 @@ export function run(app) {
if (file.mimetype !== 'application/zip' && file.mimetype !== 'application/x-zip-compressed') return res.status(400).send('Invalid filetype');
const dir = tmpdir() + '/' + file.md5;
fs.writeFile(dir, file.data, (err) => {
fs.writeFile(dir, file.data, async (err) => {
if (err) throw err;
const zip = new StreamZip({
file: dir,
storeEntries: true
});
try {
const zip = new AdmZip(dir);
const zipEntries = zip.getEntries();
zip.on('ready', async () => {
const smFile = Object.values(zip.entries()).find((f: any) =>
!f.isDirectory && (f.name.endsWith('.sm'))
const smFile: any = Object.values(zipEntries).find((f: any) =>
!f.isDirectory && (f.entryName.endsWith('.sm'))
);
if (!smFile) {
res.status(400).send('No .sm found');
} else {
const data = zip.entryDataSync((smFile as any).name);
const data = smFile.getData().toString('utf8');
const chart = parseSM(data.toString());
logger.info(`${chart.artist} - ${chart.title} was just uploaded`);
@ -63,11 +61,15 @@ export function run(app) {
});
}
zip.close();
fs.unlink(dir, (err) => {
if (err) throw err;
});
});
} catch(err) {
logger.error(err.toString());
console.error(err);
res.status(400);
res.send(err.toString());
}
});
});
}