Skip to content

Commit

Permalink
Upload of files, API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorTaelin committed Mar 18, 2017
1 parent 2e8c207 commit 1e5ab42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ swarm.upload(new Buffer(file)).then(hash => {
})
```

#### Downlod raw data
#### Download raw data

To download raw data, just call `swarm.download(hash)`. It returns a promise with the data buffer.

Expand Down Expand Up @@ -81,27 +81,29 @@ swarm.download(dirHash).then(dir => {
});
```


#### Download a file/directory to disk (on Node.js)

```javascript
swarm.download(dappHash, "/target/dir")
.then(dirPath => console.log(`Downloaded DApp to ${dirPath}.`))
swarm.download("DAPP_HASH", "/target/dir")
.then(path => console.log(`Downloaded DApp to ${path}.`))
.catch(console.log);
```

#### Upload a file/directory from disk (on Node.js)
#### Upload raw data, a file or a directory from disk (on Node.js)

```javascript
swarm.upload("/path/to/file/or/dir", "/optional_default_file.xyz")
swarm.upload({
path: "/path/to/thing", // path to data / file / directory
kind: "directory", // could also be "file" or "data"
defaultFile: "/index.html"}) // optional, and only for kind === "directory"
.then(console.log)
.catch(console.log);
```

#### Upload a file/directory from disk (on Browser)
#### Upload raw data, a file or a directory from disk (on Browser)

```javascript
swarm.upload(isDapp) // uploads file if isDapp === false, otherwise uploads directory
swarm.upload({pick: "file"}) // could also be "directory" or "data"
```

## Uploading an Ethereum DApp
Expand Down
4 changes: 3 additions & 1 deletion examples/dapp_upload_from_disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
const swarm = require("./../src/swarm.js").at("http://swarm-gateways.net");
const path = require("path");

swarm.upload(path.join(__dirname,"example_dapp_uploader"), "/index.html")
swarm.upload({
path: path.join(__dirname,"example_dapp_uploader"),
kind: "directory"}) // could be "file", "data" or "directory"
.then(console.log)
.catch(console.log);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swarm-js",
"version": "0.1.6",
"version": "0.1.7",
"description": "Swarm tools for JavaScript.",
"main": "src/swarm.js",
"scripts": {
Expand Down
14 changes: 9 additions & 5 deletions src/pick.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
picker = pickDirectory => () => new Promise((resolve, reject) => {
picker = type => () => new Promise((resolve, reject) => {
const fileLoader = e => {
const directory = {};
const totalFiles = e.target.files.length;
Expand All @@ -7,14 +7,17 @@ picker = pickDirectory => () => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => {
const data = new Buffer(e.target.result);
if (pickDirectory) {
if (type === "directory") {
const path = file.webkitRelativePath;
directory[path.slice(path.indexOf("/")+1)] = {
type: "text/plain",
data: data
};
if (++loadedFiles === totalFiles)
resolve(directory);
} else if (type === "file") {
const path = file.webkitRelativePath;
resolve({"type": mimetype.lookup(path), "data": data});
} else {
resolve(data);
}
Expand All @@ -24,7 +27,7 @@ picker = pickDirectory => () => new Promise((resolve, reject) => {
};

let fileInput;
if (pickDirectory) {
if (type === "directory") {
fileInput = document.createElement("input");
fileInput.addEventListener("change", fileLoader);
fileInput.type = "file";
Expand All @@ -45,6 +48,7 @@ picker = pickDirectory => () => new Promise((resolve, reject) => {
});

module.exports = {
file: picker(false),
directory: picker(true)
data: picker("data"),
file: picker("file"),
directory: picker("directory"),
}
56 changes: 36 additions & 20 deletions src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ const uploadToManifest = swarmUrl => hash => route => file => {
return attempt(3);
};

// String -> {type: String, data: Buffer} -> Promise String
const uploadFile = swarmUrl => file =>
uploadDirectory(swarmUrl)({"": file});

// String -> String -> Promise String
const uploadFileFromDisk = swarmUrl => filePath =>
fsp.readFile(filePath)
.then(data => uploadFile(swarmUrl)({type: mimetype.lookup(filePath), data: data}));

// String -> Map String File -> Promise String
// Uploads a directory to Swarm. The directory is
// represented as a map of routes and files.
Expand Down Expand Up @@ -172,34 +181,37 @@ const uploadDirectoryFromDisk = swarmUrl => defaultPath => dirPath =>
.then(directory => merge (defaultPath ? {"": directory[defaultPath]} : {}) (directory))
.then(uploadDirectory(swarmUrl));

// String -> Buffer | Bool | Map String Buffer | String -> Nullable String -> Promise String
// String -> UploadInfo -> Promise String
// Simplified multi-type upload which calls the correct
// one based on the type of the argument given.
const upload = swarmUrl => pathOrContents => defaultFile => {
const upload = swarmUrl => arg => {
// Upload raw data from browser
if (arg.pick === "data") {
return pick.data().then(uploadData(swarmUrl));

// Upload a file from browser
if (!pathOrContents) {
return pick.file().then(uploadData(swarmUrl));
} else if (arg.pick === "file") {
return pick.file().then(uploadFile(swarmUrl));

// Upload a directory from browser
} else if (pathOrContents === true) {
return pick.directory().then(uploadDirectory(swarmUrl));
} else if (arg.pick === "directory") {
return pick.directory().then(uploadDirectory(swarmUrl));

// Upload directory/file from disk
} else if (arg.path) {
switch (arg.kind) {
case "data": return uploadDataFromDisk(swarmUrl)(arg.path);
case "file": return uploadFileFromDisk(swarmUrl)(arg.path);
case "directory": return uploadDirectoryFromDisk(swarmUrl)(arg.defaultFile)(arg.path);
};

// Upload raw data (buffer)
} else if (pathOrContents.length && typeof pathOrContents !== "string") {
return uploadData(swarmUrl)(pathOrContents);
} else if (arg.length) {
return uploadData(swarmUrl)(arg);

// Upload directory with JSON
} else if (pathOrContents instanceof Object) {
return uploadDirectory(swarmUrl)(pathOrContents);

// Upload directory/file from disk
} else if (typeof pathOrContents === "string") {
const path = pathOrContents;
return fsp.lstat(path).then(stat => {
return stat.isDirectory()
? uploadDirectoryFromDisk(swarmUrl)(defaultFile)(path)
: uploadDataFromDisk(swarmUrl)(path);
});
} else if (arg instanceof Object) {
return uploadDirectory(swarmUrl)(arg);
}

return Q.reject(new Error("Bad arguments"));
Expand Down Expand Up @@ -368,8 +380,10 @@ const at = swarmUrl => ({
downloadEntries: uncurry(downloadEntries(swarmUrl)),
downloadRoutes: uncurry(downloadRoutes(swarmUrl)),
isAvailable: () => isAvailable(swarmUrl),
upload: (pathOrContents,defaultFile) => upload(swarmUrl)(pathOrContents)(defaultFile),
upload: (arg) => upload(swarmUrl)(arg),
uploadData: uncurry(uploadData(swarmUrl)),
uploadFile: uncurry(uploadFile(swarmUrl)),
uploadFileFromDisk: uncurry(uploadFile(swarmUrl)),
uploadDataFromDisk: uncurry(uploadDataFromDisk(swarmUrl)),
uploadDirectory: uncurry(uploadDirectory(swarmUrl)),
uploadDirectoryFromDisk: uncurry(uploadDirectoryFromDisk(swarmUrl)),
Expand All @@ -394,6 +408,8 @@ module.exports = {
upload,
uploadData,
uploadDataFromDisk,
uploadFile,
uploadFileFromDisk,
uploadDirectory,
uploadDirectoryFromDisk,
uploadToManifest,
Expand Down

0 comments on commit 1e5ab42

Please sign in to comment.