Skip to content

Commit

Permalink
Merge pull request #8 from holaplex/main
Browse files Browse the repository at this point in the history
Release 10/8
  • Loading branch information
kespinola authored Oct 8, 2023
2 parents 567db1e + 5524332 commit 212213a
Show file tree
Hide file tree
Showing 6 changed files with 2,507 additions and 112 deletions.
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

0 comments on commit 212213a

Please sign in to comment.