Skip to content

Commit

Permalink
fix: avoid open write stream if resource exist #1191
Browse files Browse the repository at this point in the history
  • Loading branch information
@jotadeveloper authored and sergiohgz committed Aug 13, 2019
1 parent 1920d99 commit f041d3f
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions plugins/local-storage/src/local-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,56 +171,57 @@ export default class LocalFS implements ILocalPackageManager {
fs.exists(pathName, exists => {
if (exists) {
uploadStream.emit('error', fSError(fileExist));
}

const temporalName = path.join(this.path, `${name}.tmp-${String(Math.random()).replace(/^0\./, '')}`);
const file = fs.createWriteStream(temporalName);
const removeTempFile = () => fs.unlink(temporalName, function() {});
let opened = false;
uploadStream.pipe(file);

uploadStream.done = function() {
const onend = function() {
file.on('close', function() {
renameTmp(temporalName, pathName, function(err) {
if (err) {
uploadStream.emit('error', err);
} else {
uploadStream.emit('success');
}
});
});
file.end();
} else {
const temporalName = path.join(this.path, `${name}.tmp-${String(Math.random()).replace(/^0\./, '')}`);
const file = fs.createWriteStream(temporalName);
const removeTempFile = () => fs.unlink(temporalName, function () {
});
let opened = false;
uploadStream.pipe(file);

uploadStream.done = function () {
const onend = function () {
file.on('close', function () {
renameTmp(temporalName, pathName, function (err) {
if (err) {
uploadStream.emit('error', err);
} else {
uploadStream.emit('success');
}
});
});
file.end();
};
if (_ended) {
onend();
} else {
uploadStream.on('end', onend);
}
};
if (_ended) {
onend();
} else {
uploadStream.on('end', onend);
}
};

uploadStream.abort = function() {
if (opened) {
opened = false;
file.on('close', function() {
removeTempFile();
});
} else {
// if the file does not recieve any byte never is opened and has to be removed anyway.
removeTempFile();
}
file.end();
};
uploadStream.abort = function () {
if (opened) {
opened = false;
file.on('close', function () {
removeTempFile();
});
} else {
// if the file does not recieve any byte never is opened and has to be removed anyway.
removeTempFile();
}
file.end();
};

file.on('open', function() {
opened = true;
// re-emitting open because it's handled in storage.js
uploadStream.emit('open');
});
file.on('open', function () {
opened = true;
// re-emitting open because it's handled in storage.js
uploadStream.emit('open');
});

file.on('error', function(err) {
uploadStream.emit('error', err);
});
file.on('error', function (err) {
uploadStream.emit('error', err);
});
}
});

return uploadStream;
Expand Down

0 comments on commit f041d3f

Please sign in to comment.