Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 10/8 #8

Merged
merged 7 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ COPY --chown=uploader:uploader . .

FROM base AS release
COPY --from=dependencies /app/prod_node_modules ./node_modules
COPY --chown=uploader:uploader . .
COPY --chown=uploader:uploader package*.json server.js bundlr.js metrics.js ./
EXPOSE 3000
CMD ["npm","start"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# HUB Upload API

This is the Upload API for HUB. It allows users to upload files to the server.
This is the Upload API for HUB. It allows users to upload files to the server using the Bundlr service.

## Setup

To setup the server, follow these steps:

1. Install the dependencies by running `npm install`
2. Set the environment variables `WEB3_UP_KEY`, `WEB3_UP_PROOF`, and `WEB3_UP_GATEWAY`. Note that `WEB3_UP_GATEWAY` should be an IPFS compatible gateway for retrieving assets over HTTP.
2. Set the environment variables `IRYS_GATEWAY`, `IRYS_URL`, `SOLANA_RPC_URL`, and `SOLANA_KEYPAIR`. Note that `IRYS_GATEWAY` should be an Arweave compatible gateway for retrieving assets over HTTP.
3. Start the server by running `npm start`

## Routes

The available routes are:

- POST /uploads: Upload a file. This route consumes multipart/form-data and returns the URI and CID of the uploaded file.
- POST /uploads: Upload a file or JSON. This route accepts either a file as multipart/form-data or a JSON object and returns the URI and CID of the uploaded file.

## Swagger Documentation

Expand Down
55 changes: 55 additions & 0 deletions bundlr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Irys from "@irys/sdk";

/**
* Class representing an Uploader.
*/
class Uploader {
/**
* Create an uploader.
* @param {Object} client - The client object.
*/
constructor(client, gateway) {
this.client = client;
this.gateway = gateway;
}

/**
* Upload a file.
* @param {Buffer} buffer - The file data as a buffer.
* @return {Object} The upload response containing the URI and CID.
* @throws {Error} If an error occurs during upload.
*/
async upload(buffer, contentType) {
const tags = [{ name: "Content-Type", value: contentType }];

const response = await this.client.upload(buffer, { tags });

const cid = response.id;
const uri = `${this.gateway}/${cid}`;

return { uri, cid };
}

async fund(bytes) {
const price = await this.client.getPrice(bytes);

const response = await this.client.fund(price);

fastify.log.info(response);

return price;
}
}

export function irys(gateway, url, providerUrl, key) {
const irys = new Irys({
url, // URL of the node you want to connect to
token: "solana", // Token used for payment
key, // ETH or SOL private key
config: {
providerUrl,
},
});

return new Uploader(irys, gateway);
}
Loading
Loading