fix bad stream pipelines for fs storage backend
This commit is contained in:
parent
f54f3ccaa2
commit
c9ac6329fd
3 changed files with 16 additions and 14 deletions
|
@ -24,10 +24,14 @@ module.exports = async function(req, res) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const limiter = new Limiter(encryptedSize(config.max_file_size));
|
const limiter = new Limiter(encryptedSize(config.max_file_size));
|
||||||
const fileStream = req.pipe(limiter);
|
|
||||||
//this hasn't been updated to expiration time setting yet
|
//this hasn't been updated to expiration time setting yet
|
||||||
//if you want to fallback to this code add this
|
//if you want to fallback to this code add this
|
||||||
await storage.set(newId, fileStream, meta, config.default_expire_seconds);
|
await storage.set(
|
||||||
|
newId,
|
||||||
|
[req, limiter],
|
||||||
|
meta,
|
||||||
|
config.default_expire_seconds
|
||||||
|
);
|
||||||
const url = `${config.deriveBaseUrl(req)}/download/${newId}/`;
|
const url = `${config.deriveBaseUrl(req)}/download/${newId}/`;
|
||||||
res.set('WWW-Authenticate', `send-v1 ${meta.nonce}`);
|
res.set('WWW-Authenticate', `send-v1 ${meta.nonce}`);
|
||||||
res.json({
|
res.json({
|
||||||
|
|
|
@ -85,11 +85,9 @@ module.exports = function(ws, req) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const wsStream = ws.constructor.createWebSocketStream(ws);
|
const wsStream = ws.constructor.createWebSocketStream(ws).pipe(eof);
|
||||||
|
|
||||||
fileStream = wsStream.pipe(eof).pipe(limiter); // limiter needs to be the last in the chain
|
await storage.set(newId, [wsStream, limiter], meta, timeLimit);
|
||||||
|
|
||||||
await storage.set(newId, fileStream, meta, timeLimit);
|
|
||||||
|
|
||||||
if (ws.readyState === 1) {
|
if (ws.readyState === 1) {
|
||||||
// if the socket is closed by a cancelled upload the stream
|
// if the socket is closed by a cancelled upload the stream
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const { pipeline } = require('stream');
|
||||||
const promisify = require('util').promisify;
|
const promisify = require('util').promisify;
|
||||||
|
|
||||||
const stat = promisify(fs.stat);
|
const stat = promisify(fs.stat);
|
||||||
|
@ -26,15 +27,14 @@ class FSStorage {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const filepath = path.join(this.dir, id);
|
const filepath = path.join(this.dir, id);
|
||||||
const fstream = fs.createWriteStream(filepath);
|
const fstream = fs.createWriteStream(filepath);
|
||||||
file.pipe(fstream);
|
pipeline([...file, fstream], err => {
|
||||||
file.on('error', err => {
|
if (err) {
|
||||||
fstream.destroy(err);
|
fs.unlinkSync(filepath);
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
fstream.on('error', err => {
|
|
||||||
fs.unlinkSync(filepath);
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
fstream.on('finish', resolve);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue