Merge pull request #24 from mike-marcacci/stream-comments
Update the handling of streams and add comments.
This commit is contained in:
commit
4e222c046c
@ -31,15 +31,27 @@ const storeUpload = async upload => {
|
|||||||
|
|
||||||
// Store the file in the filesystem.
|
// Store the file in the filesystem.
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
stream
|
// Create a stream to which the upload will be written.
|
||||||
.on('error', error => {
|
const writeStream = createWriteStream(path)
|
||||||
unlink(path, () => {
|
|
||||||
reject(error)
|
// When the upload is fully written, resolve the promise.
|
||||||
})
|
writeStream.on('finish', resolve)
|
||||||
|
|
||||||
|
// If there's an error writing the file, remove the partially written file
|
||||||
|
// and reject the promise.
|
||||||
|
writeStream.on('error', error => {
|
||||||
|
unlink(path, () => {
|
||||||
|
reject(error)
|
||||||
})
|
})
|
||||||
.pipe(createWriteStream(path))
|
})
|
||||||
.on('error', reject)
|
|
||||||
.on('finish', resolve)
|
// In node <= 13, errors are not automatically propagated between piped
|
||||||
|
// streams. If there is an error receiving the upload, destroy the write
|
||||||
|
// stream with the corresponding error.
|
||||||
|
stream.on('error', error => writeStream.destroy(error))
|
||||||
|
|
||||||
|
// Pipe the upload into the write stream.
|
||||||
|
stream.pipe(writeStream)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Record the file metadata in the DB.
|
// Record the file metadata in the DB.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user