From e206280de12839d5d1b5c676fd0850ef34464b87 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 6 Jul 2018 00:02:03 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Ignore=20length=20when=20reading?= =?UTF-8?q?=20from=20disk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssn/streams/getFileFromDisks.ts | 32 +++++------------------------ 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/src/ssn/streams/getFileFromDisks.ts b/src/ssn/streams/getFileFromDisks.ts index 1b578f4..81cf273 100644 --- a/src/ssn/streams/getFileFromDisks.ts +++ b/src/ssn/streams/getFileFromDisks.ts @@ -12,7 +12,7 @@ interface IGetFileFromDisksOptions { } function getStream(disks: string[], index: number, offset: number, length: number = Infinity) { - return fs.createReadStream(disks[index], { start: offset, end: offset + length - 1 }); + return fs.createReadStream(disks[index], { start: offset }); //, end: offset + length - 1 }); } /** Takes a list of ReadableStreams (the disks), as well as the offset and length, and returns a stream for just one file. */ @@ -23,37 +23,15 @@ export default async function getFileFromDisks(disks: string[], { diskStart, off let totalRead = 0; //Create new stream that concatenates disks until storedSize is reached, then ends the stream. - const outputStream = new stream.PassThrough({ - /*read(num) { - if (num === undefined) { - throw new Error('Expected to receive number of bytes when reading from stream.'); - } - - totalRead += num; - //end of file reached - if (localFileHeaderLength !== 0 && totalRead >= localFileHeaderLength + storedSize) { - return null; - } - - const chunk = curDisk.read(num); - //transparently switch to next disk as soon as we finished reading current disk - if (chunk === null) { - curDiskIndex += 1; - curDisk = getStream(disks, curDiskIndex, 0, (localFileHeaderLength === 0) ? Infinity : localFileHeaderLength + storedSize - totalRead); - //TODO: await new Promise((resolve) => { curDisk.on('readable', () => { resolve(); }); }); - return curDisk.read(num); - } else { - return chunk; - } - },*/ - }); + const outputStream = new stream.PassThrough(); const onData = (chunk: Buffer) => { outputStream.write(chunk); totalRead += chunk.length; - //TODO: need to end if we have read beyond the file + //need to end if we have read beyond the file + //TODO: need to also shorten chunk if file ended inside it, before writing it to PassThrough. if (localFileHeaderLength !== 0 && totalRead >= localFileHeaderLength + storedSize) { - //TODO + outputStream.end(); } }; const onError = (error: any) => {