Skip to content

Commit

Permalink
fix: check if data already exists on IPFS (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
guerrap authored Feb 11, 2024
1 parent cc0756a commit 92f0a2e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ script under `./scripts/generate-jwt.ts`. You can call it using:
pnpm generate-jwt $SCOPE
```

Where `$SCOPE` is the scope/role you want the generated JWT to have.
Where `$SCOPE` is the scope/role you want the generated JWT to have (use `ipfs`
to test the `/data/ipfs` API, and `s3` for `/data/s3/json`).

If you instead want to test the `/data/s3/templates` API to upload templates to
limbo storage, you need to use the `./scripts/upload-folder.ts` script. This is
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ services:
- 9000:9000
- 9001:9001
environment:
- MINIO_ROOT_USER=user
- MINIO_ROOT_PASSWORD=password
- MINIO_ROOT_USER=data-manager-test-access-key
- MINIO_ROOT_PASSWORD=data-manager-test-secret-key
- MINIO_DEFAULT_BUCKETS=data-manager-test-bucket
18 changes: 18 additions & 0 deletions src/routes/data/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import type { ServerRoute } from "@hapi/hapi";
import type { Client as W3UpClient } from "@web3-storage/w3up-client";
import { SCOPE_IPFS } from "../../constants";
import { CID } from "multiformats/cid";
import type { StoreGetFailure } from "@web3-storage/upload-client/types";
import type { Error } from "@ucanto/core/schema";
import { isStoreItemNotFound } from "../../utils";

interface GetDataRoutesParams {
s3: S3;
Expand Down Expand Up @@ -91,6 +94,21 @@ export const getIPFSDataRoute = async ({
return badGateway("Could not upload data to IPFS");
}

// Check if data is already on IPFS
try {
await w3UpClient.capability.store.get(parsedCid);
throw new Error(
`Data with CID ${cid} already existing on IPFS`,
);
} catch (error: any) {
if (error.cause && error.cause.name === "StoreItemNotFound")
request.logger.info("Data not existing on IPFS yet");
else {
request.logger.error(error, "Could not get data from IPFS");
return badGateway("Could not upload data to IPFS");
}
}

// Fetch the raw CAR stream from S3
let car: StreamingBlobPayloadOutputTypes;
try {
Expand Down

0 comments on commit 92f0a2e

Please sign in to comment.