-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
417141e
commit 64f11b9
Showing
10 changed files
with
551 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,129 @@ | |||
/* | |||
* @package jsDAV | |||
* @subpackage DAV | |||
* @copyright Copyright (C) 2010 Mike de Boer. All rights reserved. | |||
* @author Mike de Boer <mike AT ajax DOT org> | |||
* @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License | |||
*/ | |||
|
|||
var jsDAV = require("./../../jsdav"), | |||
jsDAV_SFTP_Node = require("./node").jsDAV_SFTP_Node, | |||
jsDAV_SFTP_File = require("./file").jsDAV_SFTP_File, | |||
jsDAV_Directory = require("./../directory").jsDAV_Directory, | |||
jsDAV_iCollection = require("./../iCollection").jsDAV_iCollection, | |||
jsDAV_iQuota = require("./../iQuota").jsDAV_iQuota, | |||
|
|||
Fs = require("fs"), | |||
Async = require("./../../../support/async.js"), | |||
Exc = require("./../exceptions"); | |||
|
|||
function jsDAV_SFTP_Directory(path, sftp) { | |||
this.path = (path || "").replace(/[\/]+$/, ""); | |||
this.sftp = sftp; | |||
} | |||
|
|||
exports.jsDAV_SFTP_Directory = jsDAV_SFTP_Directory; | |||
|
|||
(function() { | |||
this.implement(jsDAV_Directory, jsDAV_iCollection, jsDAV_iQuota); | |||
|
|||
/** | |||
* Creates a new file in the directory | |||
* | |||
* data is a readable stream resource | |||
* | |||
* @param string name Name of the file | |||
* @param resource data Initial payload | |||
* @return void | |||
*/ | |||
this.createFile = function(name, data, enc, cbfscreatefile) { | |||
var newPath = (this.path + "/" + name).replace(/[\/]+$/, ""); | |||
if (data.length === 0) { //sftp lib does not support writing empty files... | |||
data = new Buffer("empty file"); | |||
enc = "binary"; | |||
} | |||
this.sftp.writeFile(newPath, data, enc || "utf8", cbfscreatefile); | |||
}; | |||
|
|||
/** | |||
* Creates a new subdirectory | |||
* | |||
* @param string name | |||
* @return void | |||
*/ | |||
this.createDirectory = function(name, cbfscreatedir) { | |||
var newPath = this.path + "/" + name.replace(/[\/]+$/, ""); | |||
this.sftp.mkdir(newPath, 0755, cbfscreatedir); | |||
}; | |||
|
|||
/** | |||
* Returns a specific child node, referenced by its name | |||
* | |||
* @param string name | |||
* @throws Sabre_DAV_Exception_FileNotFound | |||
* @return Sabre_DAV_INode | |||
*/ | |||
this.getChild = function(name, cbfsgetchild) { | |||
var path = (this.path + "/" + name).replace(/[\/]+$/, ""), | |||
_self = this; | |||
|
|||
this.sftp.stat(path, function(err, stat) { | |||
if (err || typeof stat == "undefined") { | |||
return cbfsgetchild(new Exc.jsDAV_Exception_FileNotFound("File with name " | |||
+ path + " could not be located")); | |||
} | |||
cbfsgetchild(null, stat.isDirectory() | |||
? new jsDAV_SFTP_Directory(path, _self.sftp) | |||
: new jsDAV_SFTP_File(path, _self.sftp)) | |||
}); | |||
}; | |||
|
|||
/** | |||
* Returns an array with all the child nodes | |||
* | |||
* @return Sabre_DAV_INode[] | |||
*/ | |||
this.getChildren = function(cbfsgetchildren) { | |||
var nodes = [], | |||
_self = this; | |||
this.sftp.readdir(this.path, function(err, listing) { | |||
if (err) | |||
return cbfsgetchildren(null, nodes); | |||
Async.list(listing) | |||
.each(function(node, cbnext) { | |||
var path = (_self.path + "/" + node).replace(/[\/]+$/, ""); | |||
_self.sftp.stat(path, function(err, stat) { | |||
if (err) | |||
return cbnext(); | |||
nodes.push(stat.isDirectory() | |||
? new jsDAV_SFTP_Directory(path, _self.sftp) | |||
: new jsDAV_SFTP_File(path, _self.sftp) | |||
); | |||
cbnext(); | |||
}); | |||
}) | |||
.end(function() { | |||
cbfsgetchildren(null, nodes); | |||
}); | |||
}); | |||
}; | |||
|
|||
/** | |||
* Deletes all files in this directory, and then itself | |||
* | |||
* @return void | |||
*/ | |||
this["delete"] = function(cbfsdel) { | |||
this.sftp.rmdir(this.path, cbfsdel); | |||
}; | |||
|
|||
/** | |||
* Returns available diskspace information | |||
* | |||
* @return array | |||
*/ | |||
this.getQuotaInfo = function(cbfsquota) { | |||
// @todo: impl. sftp.statvfs(); | |||
return cbfsquota(null, [0, 0]); | |||
}; | |||
}).call(jsDAV_SFTP_Directory.prototype = new jsDAV_SFTP_Node()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,107 @@ | |||
/* | |||
* @package jsDAV | |||
* @subpackage DAV | |||
* @copyright Copyright (C) 2010 Mike de Boer. All rights reserved. | |||
* @author Mike de Boer <mike AT ajax DOT org> | |||
* @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License | |||
*/ | |||
|
|||
var jsDAV = require("./../../jsdav"), | |||
jsDAV_SFTP_Node = require("./node").jsDAV_SFTP_Node, | |||
jsDAV_Directory = require("./../directory").jsDAV_Directory, | |||
jsDAV_iFile = require("./../iFile").jsDAV_iFile, | |||
|
|||
Fs = require("fs"), | |||
Exc = require("./../exceptions"), | |||
Util = require("./../util"); | |||
|
|||
function jsDAV_SFTP_File(path, sftp) { | |||
this.path = (path || "").replace(/[\/]+$/, ""); | |||
this.sftp = sftp; | |||
} | |||
|
|||
exports.jsDAV_SFTP_File = jsDAV_SFTP_File; | |||
|
|||
(function() { | |||
this.implement(jsDAV_iFile); | |||
|
|||
/** | |||
* Updates the data | |||
* | |||
* @param {mixed} data | |||
* @return void | |||
*/ | |||
this.put = function(data, type, cbfsput) { | |||
this.sftp.writeFile(this.path, data, type || "utf8", cbfsput); | |||
}; | |||
|
|||
/** | |||
* Returns the data | |||
* | |||
* @return Buffer | |||
*/ | |||
this.get = function(cbfsfileget) { | |||
if (this.$buffer) | |||
return cbfsfileget(null, this.$buffer); | |||
var _self = this; | |||
this.sftp.readFile(this.path, null, function(err, buff) { | |||
if (err) | |||
return cbfsfileget(err); | |||
// Zero length buffers act funny, use a string | |||
if (buff.length === 0) | |||
buff = ""; | |||
//_self.$buffer = buff; | |||
cbfsfileget(null, buff); | |||
}); | |||
}; | |||
|
|||
/** | |||
* Delete the current file | |||
* | |||
* @return void | |||
*/ | |||
this["delete"] = function(cbfsfiledel) { | |||
this.sftp.unlink(this.path, cbfsfiledel); | |||
}; | |||
|
|||
/** | |||
* Returns the size of the node, in bytes | |||
* | |||
* @return int | |||
*/ | |||
this.getSize = function(cbfsgetsize) { | |||
if (this.$stat) | |||
return cbfsgetsize(null, this.$stat.size); | |||
var _self = this; | |||
this.sftp.stat(this.path, function(err, stat) { | |||
if (err || !stat) { | |||
return cbfsgetsize(new Exc.jsDAV_Exception_FileNotFound("File at location " | |||
+ _self.path + " not found")); | |||
} | |||
//_self.$stat = stat; | |||
cbfsgetsize(null, stat.size); | |||
}); | |||
}; | |||
|
|||
/** | |||
* Returns the ETag for a file | |||
* An ETag is a unique identifier representing the current version of the file. | |||
* If the file changes, the ETag MUST change. | |||
* Return null if the ETag can not effectively be determined | |||
* | |||
* @return mixed | |||
*/ | |||
this.getETag = function(cbfsgetetag) { | |||
cbfsgetetag(null, null); | |||
}; | |||
|
|||
/** | |||
* Returns the mime-type for a file | |||
* If null is returned, we'll assume application/octet-stream | |||
* | |||
* @return mixed | |||
*/ | |||
this.getContentType = function(cbfsmime) { | |||
return cbfsmime(null, Util.mime.type(this.path)); | |||
}; | |||
}).call(jsDAV_SFTP_File.prototype = new jsDAV_SFTP_Node()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,81 @@ | |||
/* | |||
* @package jsDAV | |||
* @subpackage DAV | |||
* @copyright Copyright (C) 2010 Mike de Boer. All rights reserved. | |||
* @author Mike de Boer <mike AT ajax DOT org> | |||
* @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License | |||
*/ | |||
|
|||
var jsDAV = require("./../../jsdav"), | |||
jsDAV_iNode = require("./../iNode").jsDAV_iNode, | |||
|
|||
Fs = require("fs"), | |||
Path = require("path"), | |||
Util = require("./../util"), | |||
Exc = require("./../exceptions"); | |||
|
|||
function jsDAV_SFTP_Node(path, sftp) { | |||
this.path = (path || "").replace(/[\/]+$/, ""); | |||
this.sftp = sftp; | |||
} | |||
|
|||
exports.jsDAV_SFTP_Node = jsDAV_SFTP_Node; | |||
|
|||
(function() { | |||
/** | |||
* Returns the name of the node | |||
* | |||
* @return {string} | |||
*/ | |||
this.getName = function() { | |||
return Util.splitPath(this.path)[1]; | |||
}; | |||
|
|||
/** | |||
* Renames the node | |||
* | |||
* @param {string} name The new name | |||
* @return void | |||
*/ | |||
this.setName = function(name, cbfssetname) { | |||
var parentPath = Util.splitPath(this.path)[0], | |||
newName = Util.splitPath(name)[1]; | |||
|
|||
var newPath = parentPath + "/" + newName; | |||
var _self = this; | |||
this.sftp.rename(this.path, newPath, function(err) { | |||
if (err) | |||
return cbfssetname(err); | |||
_self.path = newPath; | |||
cbfssetname(); | |||
}); | |||
}; | |||
|
|||
/** | |||
* Returns the last modification time, as a unix timestamp | |||
* | |||
* @return {Number} | |||
*/ | |||
this.getLastModified = function(cbfsgetlm) { | |||
if (this.$stat) | |||
return cbfsgetlm(null, this.$stat.mtime); | |||
var _self = this; | |||
this.sftp.stat(this.path, function(err, stat) { | |||
if (err || typeof stat == "undefined") | |||
return cbfsgetlm(err); | |||
//_self.$stat = stat; | |||
cbfsgetlm(null, stat.mtime); | |||
}); | |||
}; | |||
|
|||
/** | |||
* Returns whether a node exists or not | |||
* | |||
* @return {Boolean} | |||
*/ | |||
this.exists = function(cbfsexist) { | |||
this.sftp.stat(this.path, function(err, stat) { | |||
cbfsexist(Boolean(!err && stat)) | |||
}); | |||
}; | |||
}).call(jsDAV_SFTP_Node.prototype = new jsDAV_iNode()); |
Oops, something went wrong.