diff --git a/src/interfaces/ISolidFile.ts b/src/interfaces/ISolidFile.ts index d364051..09e52cb 100644 --- a/src/interfaces/ISolidFile.ts +++ b/src/interfaces/ISolidFile.ts @@ -1,13 +1,14 @@ interface ISolidFile { - //TODO: show length - /** File name of this file. */ + /** Length of this file in bytes, up to 1700000000 or 1.7 GB */ + length: number; + /** File name of this file, e.g. `${product}_${from}to${to}.z01` or `${product}_${from}to${to}.zip`. */ path: [string]; } interface ISolidFileInfo { /** List of files that are part of this torrent. */ 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; /** Concatenated hashes of all pieces. */ pieces: string; diff --git a/src/ssn/verifySolidpkg.ts b/src/ssn/verifySolidpkg.ts index d4f35b6..9bd06bc 100644 --- a/src/ssn/verifySolidpkg.ts +++ b/src/ssn/verifySolidpkg.ts @@ -25,7 +25,21 @@ export default function verifySolidpkg(file: ISolid, { product, from, to }: {pro 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) { throw new Error(`Expected piece length to be "4194304" but it was "${file.info['piece length']}".`);