Skip to content
This repository
Browse code

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

  • Loading branch information...
commit e0622ed84fdcfcd53e06a8ed0bfcdebb7c0f7684 1 parent 1dd58ec
Brian White authored

Showing 2 changed files with 15 additions and 5 deletions. Show diff stats Hide diff stats

  1. +3 2 README.md
  2. +12 3 ftp.js
5 README.md
Source Rendered
@@ -62,7 +62,7 @@ Examples
62 62
63 63 var c = new FTPClient();
64 64 c.on('ready', function() {
65   - c.put(fs.createReadStream('foo.txt'), 'foo.remote-copy.txt', function(err) {
  65 + c.put('foo.txt', 'foo.remote-copy.txt', function(err) {
66 66 if (err) throw err;
67 67 c.end();
68 68 });
@@ -71,6 +71,7 @@ Examples
71 71 c.connect();
72 72 ```
73 73
  74 +
74 75 API
75 76 ===
76 77
@@ -139,7 +140,7 @@ Methods
139 140
140 141 * **get**(< _string_ >path, < _function_ >callback) - _(void)_ - Retrieves a file, `path`, from the server. `callback` has 2 parameters: < _Error_ >err, < _ReadableStream_ >fileStream.
141 142
142   -* **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.
  143 +* **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.
143 144
144 145 * **append**(< _mixed_ >input, < _string_ >path, < _function_ >callback) - _(void)_ - Same as **put()**, except if `path` already exists, it will be appended to instead of overwritten.
145 146
15 ftp.js
... ... @@ -1,4 +1,5 @@
1   -var Socket = require('net').Socket,
  1 +var fs = require('fs'),
  2 + Socket = require('net').Socket,
2 3 EventEmitter = require('events').EventEmitter,
3 4 inherits = require('util').inherits,
4 5 XRegExp = require('./xregexp');
@@ -564,7 +565,7 @@ FTP.prototype._pasvConnect = function(ip, port, cb) {
564 565 FTP.prototype._store = function(cmd, input, cb) {
565 566 var isBuffer = Buffer.isBuffer(input);
566 567
567   - if (!isBuffer)
  568 + if (!isBuffer && input.pause !== undefined)
568 569 input.pause();
569 570
570 571 var self = this;
@@ -590,7 +591,15 @@ FTP.prototype._store = function(cmd, input, cb) {
590 591 if (code === 150) {
591 592 if (isBuffer)
592 593 sock.end(input);
593   - else {
  594 + else if (typeof input === 'string') {
  595 + // check if input is a file path or just string data to store
  596 + fs.stat(input, function(err, stats) {
  597 + if (err)
  598 + sock.end(input);
  599 + else
  600 + fs.createReadStream(input).pipe(sock);
  601 + });
  602 + } else {
594 603 input.pipe(sock);
595 604 input.resume();
596 605 }

0 comments on commit e0622ed

Please sign in to comment.
Something went wrong with that request. Please try again.