Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

DO NOT DELETE - WEB-2963 - Adding callback style for createWriteStream #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 70 additions & 63 deletions lib/FtpConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,79 +1124,86 @@ FtpConnection.prototype._STOR_usingCreateWriteStream = function(filename, initia
var self = this;

var wStreamFlags = {flags: flag || 'w', mode: 0644};
var storeStream = self.fs.createWriteStream(pathModule.join(self.root, filename), wStreamFlags);
var notErr = true;
// Adding for event metadata for file upload (STOR)
var startTime = new Date();

if (initialBuffers) {
//todo: handle back-pressure
initialBuffers.forEach(function(b) {
storeStream.write(b);
});
self.fs.createWriteStream(pathModule.join(self.root, filename), wStreamFlags, function(err, storeStream) {
if (err) {
self._logIf(LOG.ERROR, 'Error deleting file: ' + filename + ', ' + err);
// write error to socket
self.respond('550 Permission denied');
return;
}
var notErr = true;
// Adding for event metadata for file upload (STOR)
var startTime = new Date();

if (initialBuffers) {
//todo: handle back-pressure
initialBuffers.forEach(function(b) {
storeStream.write(b);
});
}

self._whenDataReady(handleUpload);
self._whenDataReady(handleUpload);

storeStream.on('open', function() {
self._logIf(LOG.DEBUG, 'File opened/created: ' + filename);
self._logIf(LOG.DEBUG, 'Told client ok to send file data');
// Adding event emitter for upload start time
self.emit('file:stor', 'open', {
user: self.username,
file: filename,
time: startTime,
});
storeStream.on('open', function() {
self._logIf(LOG.DEBUG, 'File opened/created: ' + filename);
self._logIf(LOG.DEBUG, 'Told client ok to send file data');
// Adding event emitter for upload start time
self.emit('file:stor', 'open', {
user: self.username,
file: filename,
time: startTime,
});

self.respond('150 Ok to send data');
});
self.respond('150 Ok to send data');
});

storeStream.on('error', function() {
self.emit('file:stor', 'error', {
user: self.username,
file: filename,
/** @deprecated filesize is deprecated, use bytesRead/bytesWritten instead */
filesize: 0,
bytesWritten: storeStream.bytesWritten,
sTime: startTime,
eTime: new Date(),
duration: new Date() - startTime,
errorState: !notErr,
storeStream.on('error', function() {
self.emit('file:stor', 'error', {
user: self.username,
file: filename,
/** @deprecated filesize is deprecated, use bytesRead/bytesWritten instead */
filesize: 0,
bytesWritten: storeStream.bytesWritten,
sTime: startTime,
eTime: new Date(),
duration: new Date() - startTime,
errorState: !notErr,
});
storeStream.end();
notErr = false;
if (self.dataSocket) {
self._closeSocket(self.dataSocket, true);
}
self.respond('426 Connection closed; transfer aborted');
});
storeStream.end();
notErr = false;
if (self.dataSocket) {
self._closeSocket(self.dataSocket, true);
}
self.respond('426 Connection closed; transfer aborted');
});

storeStream.on('finish', function() {
// Adding event emitter for completed upload.
self.emit('file:stor', 'close', {
user: self.username,
file: filename,
/** @deprecated filesize is deprecated, use bytesRead/bytesWritten instead */
filesize: 0,
bytesWritten: storeStream.bytesWritten,
sTime: startTime,
eTime: new Date(),
duration: new Date() - startTime,
errorState: !notErr,
storeStream.on('finish', function() {
// Adding event emitter for completed upload.
self.emit('file:stor', 'close', {
user: self.username,
file: filename,
/** @deprecated filesize is deprecated, use bytesRead/bytesWritten instead */
filesize: 0,
bytesWritten: storeStream.bytesWritten,
sTime: startTime,
eTime: new Date(),
duration: new Date() - startTime,
errorState: !notErr,
});
notErr ? self.respond('226 Closing data connection') : true;
if (self.dataSocket) {
self._closeSocket(self.dataSocket);
}
});
notErr ? self.respond('226 Closing data connection') : true;
if (self.dataSocket) {
self._closeSocket(self.dataSocket);

function handleUpload(dataSocket) {
dataSocket.pipe(storeStream);
dataSocket.on('error', function(err) {
notErr = false;
self._logIf(LOG.ERROR, 'Data connection error: ' + util.inspect(err));
});
}
});

function handleUpload(dataSocket) {
dataSocket.pipe(storeStream);
dataSocket.on('error', function(err) {
notErr = false;
self._logIf(LOG.ERROR, 'Data connection error: ' + util.inspect(err));
});
}
};

FtpConnection.prototype._STOR_usingWriteFile = function(filename, flag) {
Expand Down