Skip to content

Commit

Permalink
Merge pull request #6 from ajaxorg/ftppathfix
Browse files Browse the repository at this point in the history
Fixed problems with FTP paths
  • Loading branch information
mikedeboer committed Dec 1, 2011
2 parents 5006231 + 46f4ff9 commit 10442fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 50 deletions.
61 changes: 12 additions & 49 deletions lib/DAV/tree/ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ var jsDAV = require("../../jsdav");
var jsDAV_Tree = require("../tree").jsDAV_Tree;
var jsDAV_Ftp_Directory = require("../ftp/directory").jsDAV_Ftp_Directory;
var jsDAV_Ftp_File = require("../ftp/file").jsDAV_Ftp_File;

var Ftp = require("../../../support/jsftp/jsftp");
var Util = require("../util");
var Exc = require("../exceptions");
var Path = require('path');

var PWD_RE = /.*"(.*)".*/;

/**
* jsDAV_Tree_Ftp
*
Expand All @@ -45,16 +41,16 @@ exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;
var ftp = this.ftp = new Ftp(this.options);
this.ftp.$cache = {};
this.ftpCmdListeners.forEach(function(listener) {
ftp.addCmdListener(listener)
ftp.addCmdListener(listener);
});
};

this.addFtpCmdListener = function(listener) {
if (this.ftp)
this.ftp.addCmdListener(listener)
this.ftp.addCmdListener(listener);
else
this.ftpCmdListeners.push(listener);
}
};

/**
* Returns a new node for the given path
Expand All @@ -63,15 +59,17 @@ exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;
* @return void
*/
this.getNodeForPath = function(path, next) {
if (!path || path.match(/^\s+$/) || path.match(/^\/+$/))
if (!path || path.match(/^\s+$/) || path.match(/^[\/]+$/))
path = this.basePath;

if (!this.ftp)
this.setup();

var ftp = this.ftp;
if (ftp.$cache[path])

if (ftp.$cache[path]) {
return next(null, ftp.$cache[path]);
}

// Root node requires special treatment because it will not be listed
if (path === this.basePath) {
Expand All @@ -82,7 +80,9 @@ exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;

var self = this;
var baseName = Path.basename(path).replace("/", "");
ftp.ls(Path.dirname(path), function(err, res) {
path = this.getRealPath(path);
var parentDir = Path.resolve(path + "/..");
ftp.ls(parentDir, function(err, res) {
if (err) {
if (res.code === 530) // Not logged in
ftp.auth(self.options.user, self.options.pass, function(err, res) {
Expand All @@ -98,7 +98,6 @@ exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;
var file;
for (var i = 0; i < res.length; i++) {
var stat = res[i];

if (stat.name === baseName) {
file = stat;
break;
Expand Down Expand Up @@ -149,8 +148,8 @@ exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;
*/
this.copy = function(source, destination, next) {
next(new Exc.jsDAV_Exception_NotImplemented(
"Could not copy from " + source + " to " + destination + ". " +
+ "COPY/DUPLICATE not implemented.")
"Could not copy from " + source + " to " + destination
+ ". COPY/DUPLICATE not implemented.")
);
};

Expand Down Expand Up @@ -206,42 +205,6 @@ exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;
}
};

/**
* Caches a path's parent path and its children, then goes back to the caller function with the
* same previous arguments.
*
* @param string path
* @return void
*/
this.$getParentNodeRecall = function(path) {
var caller = arguments.callee.caller;
var callerArgs = caller.arguments;
var callback;
var next = callback = callerArgs[callerArgs.length-1];
var parentPath = Util.splitPath(path)[0];
var self = this;

this.getNodeForPath(parentPath.substring(this.basePath.length), function(err, node) {
if (err)
return next(err);

node.getChildren(function(err, nodes) {
if (err)
return next(err);

if (nodes.length && typeof caller === 'function') {
nodes.forEach(function(child) {
if (child.path === path)
callback = caller.bind.apply(caller, [self].concat([].slice.call(callerArgs)));
});
callback();
} else {
next();
}
});
});
};

this.unmount = function() {
console.log("Closed connection to the server. Unmounting FTP tree.");
if (this.ftp) {
Expand Down
2 changes: 1 addition & 1 deletion support/jsftp
Submodule jsftp updated from f93b4e to e3f10c

0 comments on commit 10442fe

Please sign in to comment.