Navigation Menu

Skip to content

Commit

Permalink
first working commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrik committed May 13, 2013
0 parents commit 2405b57
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/.idea
29 changes: 29 additions & 0 deletions README.md
@@ -0,0 +1,29 @@
replicate-http
====

Copy remote files with webdav protocol by urls.

```
npm install replicate-http
```

### Usage

Note that destination must support webdav PUT method.

```javascript
var replicate = require("replicate-http");

replicate("http://pewpewpew.com/passwords.txt", "http://backup.pewpewpew.com/passwords.txt", function() {
if (error) {
throw error;
}

console.log("File successfully copied!");
});

```

### Authors

* [Ian Babrou](https://github.com/bobrik)
50 changes: 50 additions & 0 deletions index.js
@@ -0,0 +1,50 @@
(function(module) {
var http = require("http"),
url = require("url");

module.exports = function(from, to, callback) {
var toUrl = url.parse(to),
put;

http.get(from, function(res) {
res.on("error", callback);

if (res.statusCode != 200) {
res.on("end", function() {
callback(new Error("Got http code " + res.statusCode + " for " + from));
});

// suck stream in
res.resume();
} else {
put = http.request({
host : toUrl.hostname,
port : toUrl.port,
method : "PUT",
path : toUrl.path,
headers : {
"Content-Length": res.headers['content-length']
}
});

put.on("response", function(res) {
if (res.statusCode != 201 && res.statusCode != 204) {
res.on("end", function() {
callback(new Error("HTTP put failed with code " + res.statusCode + " for " + to));
});
} else {
res.on("end", callback);
}

// suck stream in
res.resume();
});

put.on("end", callback);
put.on("error", callback);

res.pipe(put);
}
}).on("error", callback);
};
})(module);
22 changes: 22 additions & 0 deletions package.json
@@ -0,0 +1,22 @@
{
"name": "replicate-http",
"version": "0.1.0",
"description": "Replicate remote files by webdav protocol.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@github.com:Topface/node-replicate-http.git"
},
"keywords": [
"webdav",
"dav",
"http",
"replicate",
"copy"
],
"author": "Ian Babrou <ibobrik@gmail.com> (http://bobrik.name/)",
"license": "BSD"
}

0 comments on commit 2405b57

Please sign in to comment.