Skip to content

Commit f041d3f

Browse files
@jotadevelopersergiohgz
authored andcommitted
fix: avoid open write stream if resource exist #1191
1 parent 1920d99 commit f041d3f

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

plugins/local-storage/src/local-fs.ts

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -171,56 +171,57 @@ export default class LocalFS implements ILocalPackageManager {
171171
fs.exists(pathName, exists => {
172172
if (exists) {
173173
uploadStream.emit('error', fSError(fileExist));
174-
}
175-
176-
const temporalName = path.join(this.path, `${name}.tmp-${String(Math.random()).replace(/^0\./, '')}`);
177-
const file = fs.createWriteStream(temporalName);
178-
const removeTempFile = () => fs.unlink(temporalName, function() {});
179-
let opened = false;
180-
uploadStream.pipe(file);
181-
182-
uploadStream.done = function() {
183-
const onend = function() {
184-
file.on('close', function() {
185-
renameTmp(temporalName, pathName, function(err) {
186-
if (err) {
187-
uploadStream.emit('error', err);
188-
} else {
189-
uploadStream.emit('success');
190-
}
191-
});
192-
});
193-
file.end();
174+
} else {
175+
const temporalName = path.join(this.path, `${name}.tmp-${String(Math.random()).replace(/^0\./, '')}`);
176+
const file = fs.createWriteStream(temporalName);
177+
const removeTempFile = () => fs.unlink(temporalName, function () {
178+
});
179+
let opened = false;
180+
uploadStream.pipe(file);
181+
182+
uploadStream.done = function () {
183+
const onend = function () {
184+
file.on('close', function () {
185+
renameTmp(temporalName, pathName, function (err) {
186+
if (err) {
187+
uploadStream.emit('error', err);
188+
} else {
189+
uploadStream.emit('success');
190+
}
191+
});
192+
});
193+
file.end();
194+
};
195+
if (_ended) {
196+
onend();
197+
} else {
198+
uploadStream.on('end', onend);
199+
}
194200
};
195-
if (_ended) {
196-
onend();
197-
} else {
198-
uploadStream.on('end', onend);
199-
}
200-
};
201201

202-
uploadStream.abort = function() {
203-
if (opened) {
204-
opened = false;
205-
file.on('close', function() {
206-
removeTempFile();
207-
});
208-
} else {
209-
// if the file does not recieve any byte never is opened and has to be removed anyway.
210-
removeTempFile();
211-
}
212-
file.end();
213-
};
202+
uploadStream.abort = function () {
203+
if (opened) {
204+
opened = false;
205+
file.on('close', function () {
206+
removeTempFile();
207+
});
208+
} else {
209+
// if the file does not recieve any byte never is opened and has to be removed anyway.
210+
removeTempFile();
211+
}
212+
file.end();
213+
};
214214

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

221-
file.on('error', function(err) {
222-
uploadStream.emit('error', err);
223-
});
221+
file.on('error', function (err) {
222+
uploadStream.emit('error', err);
223+
});
224+
}
224225
});
225226

226227
return uploadStream;

0 commit comments

Comments
 (0)