add-file-to-drive - hashがstreamを受ける時、hashもまたstreamなのだ

This commit is contained in:
otofune 2017-11-14 04:11:53 +09:00
parent f6c63fbe14
commit 54804f4a64
1 changed files with 7 additions and 4 deletions

View File

@ -94,13 +94,16 @@ export default (
((): Promise<string> => new Promise((res, rej) => { ((): Promise<string> => new Promise((res, rej) => {
const readable = fs.createReadStream(path); const readable = fs.createReadStream(path);
const hash = crypto.createHash('md5'); const hash = crypto.createHash('md5');
const chunks = [];
readable readable
.on('error', rej) .on('error', rej)
.on('end', () => {
res(hash.digest('hex'));
})
.pipe(hash) .pipe(hash)
.on('error', rej); .on('error', rej)
.on('data', (chunk) => chunks.push(chunk))
.on('end', () => {
const buffer = Buffer.concat(chunks);
res(buffer.toString('hex'));
});
}))(), }))(),
// mime // mime
((): Promise<[string, string | null]> => new Promise((res, rej) => { ((): Promise<[string, string | null]> => new Promise((res, rej) => {