Verify file entries in solidpkg

This commit is contained in:
C-3PO 2018-06-22 17:35:36 +02:00
parent c7bea67a1c
commit 06762c6107
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 19 additions and 4 deletions

View file

@ -1,13 +1,14 @@
interface ISolidFile { interface ISolidFile {
//TODO: show length /** Length of this file in bytes, up to 1700000000 or 1.7 GB */
/** File name of this file. */ length: number;
/** File name of this file, e.g. `${product}_${from}to${to}.z01` or `${product}_${from}to${to}.zip`. */
path: [string]; path: [string];
} }
interface ISolidFileInfo { interface ISolidFileInfo {
/** List of files that are part of this torrent. */ /** List of files that are part of this torrent. */
files: ISolidFile[]; files: ISolidFile[];
/** Length of one piece in bytes, e.g. 4194304 for 4 MB. */ /** Length of one piece in bytes, e.g. 4194304 for 4 MiB. */
'piece length': number; 'piece length': number;
/** Concatenated hashes of all pieces. */ /** Concatenated hashes of all pieces. */
pieces: string; pieces: string;

View file

@ -25,7 +25,21 @@ export default function verifySolidpkg(file: ISolid, { product, from, to }: {pro
throw new Error(`Expected info field but it was missing.`); throw new Error(`Expected info field but it was missing.`);
} }
//TODO: read file.info.files if (!Array.isArray(file.info.files)) {
throw new Error(`Expected files field to be an array but it isn't.`);
}
for (let i = 0, il = file.info.files.length; i < il; i += 1) {
const fileEntry = file.info.files[i];
if (typeof fileEntry.length !== 'number' && fileEntry.length >= 0 && fileEntry.length <= 1700000000) {
throw new Error(`Expected file length to be a number but it was ${fileEntry.length}.`);
}
if (!Array.isArray(fileEntry.path) || fileEntry.path.length !== 1) {
throw new Error(`Expected valid file name but it was not an array with one element.`);
}
if (typeof fileEntry.path[0] !== 'string' || !fileEntry.path[0].match(new RegExp(`${product}_${from}to${to}\.z(ip|0[1-9]|[1-9][0-9])$`))) {
throw new Error(`Expected valid file name but it was ${fileEntry.path[0]}.`);
}
}
if (file.info['piece length'] !== 4194304) { if (file.info['piece length'] !== 4194304) {
throw new Error(`Expected piece length to be "4194304" but it was "${file.info['piece length']}".`); throw new Error(`Expected piece length to be "4194304" but it was "${file.info['piece length']}".`);