Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow local file path string for put()/append()

  • Loading branch information...
commit e0622ed84fdcfcd53e06a8ed0bfcdebb7c0f7684 1 parent 1dd58ec
Brian White authored
Showing with 15 additions and 5 deletions.
  1. +3 −2 README.md
  2. +12 −3 ftp.js
5 README.md
View
@@ -62,7 +62,7 @@ Examples
var c = new FTPClient();
c.on('ready', function() {
- c.put(fs.createReadStream('foo.txt'), 'foo.remote-copy.txt', function(err) {
+ c.put('foo.txt', 'foo.remote-copy.txt', function(err) {
if (err) throw err;
c.end();
});
@@ -71,6 +71,7 @@ Examples
c.connect();
```
+
API
===
@@ -139,7 +140,7 @@ Methods
* **get**(< _string_ >path, < _function_ >callback) - _(void)_ - Retrieves a file, `path`, from the server. `callback` has 2 parameters: < _Error_ >err, < _ReadableStream_ >fileStream.
-* **put**(< _mixed_ >input, < _string_ >path, < _function_ >callback) - _(void)_ - Sends data to the server to be stored as `path`. `input` can be a ReadableStream or a single Buffer. `callback` has 1 parameter: < _Error_ >err.
+* **put**(< _mixed_ >input, < _string_ >path, < _function_ >callback) - _(void)_ - Sends data to the server to be stored as `path`. `input` can be a ReadableStream, a Buffer, or a path to a file. `callback` has 1 parameter: < _Error_ >err.
* **append**(< _mixed_ >input, < _string_ >path, < _function_ >callback) - _(void)_ - Same as **put()**, except if `path` already exists, it will be appended to instead of overwritten.
15 ftp.js
View
@@ -1,4 +1,5 @@
-var Socket = require('net').Socket,
+var fs = require('fs'),
+ Socket = require('net').Socket,
EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
XRegExp = require('./xregexp');
@@ -564,7 +565,7 @@ FTP.prototype._pasvConnect = function(ip, port, cb) {
FTP.prototype._store = function(cmd, input, cb) {
var isBuffer = Buffer.isBuffer(input);
- if (!isBuffer)
+ if (!isBuffer && input.pause !== undefined)
input.pause();
var self = this;
@@ -590,7 +591,15 @@ FTP.prototype._store = function(cmd, input, cb) {
if (code === 150) {
if (isBuffer)
sock.end(input);
- else {
+ else if (typeof input === 'string') {
+ // check if input is a file path or just string data to store
+ fs.stat(input, function(err, stats) {
+ if (err)
+ sock.end(input);
+ else
+ fs.createReadStream(input).pipe(sock);
+ });
+ } else {
input.pipe(sock);
input.resume();
}
Please sign in to comment.
Something went wrong with that request. Please try again.