Skip to content

Commit fde13d0

Browse files
author
y86993
authored
Refactor for Filebase
Replace the use of the old IPFS Gateway with using Filebase.
1 parent c9a526c commit fde13d0

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/utils/ipfs-publish.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
import fetch from 'node-fetch'
1+
import { File, FilebaseClient } from '@filebase/client';
2+
3+
const filebase = new FilebaseClient({
4+
token: process.env.FILEBASE_TOKEN ?? "",
5+
bucket: process.env.FILEBASE_BUCKET ?? ""
6+
});
27

38
/**
4-
* Send file to IPFS network via the Kleros IPFS node
9+
* Send file to IPFS network via Filebase
510
* @param {string} fileName - The name that will be used to store the file. This is useful to preserve extension type.
611
* @param {ArrayBuffer} data - The raw data from the file to upload.
712
* @returns {object} ipfs response. Should include the hash and path of the stored item.
813
*/
914
async function ipfsPublish(
1015
fileName: string,
1116
data: ArrayBuffer | Buffer,
12-
): Promise<Record<string, string>[]> {
13-
data = await Buffer.from(data)
17+
): Promise<{ hash: string; path: string }[]> { // Return an array of objects with a hash property
18+
const buffer = Buffer.from(data);
1419

15-
return (
16-
await (
17-
await fetch(`${process.env.IPFS_GATEWAY}/add`, {
18-
method: 'POST',
19-
body: JSON.stringify({
20-
fileName,
21-
buffer: data,
22-
}),
23-
headers: {
24-
'content-type': 'application/json',
25-
},
26-
})
27-
).json()
28-
).data
20+
const cid = await filebase.storeDirectory([new File([buffer], fileName)]);
21+
22+
return [{ hash: cid, path: `${cid}/${fileName}` }]; // Return an array
2923
}
3024

3125
export default ipfsPublish

0 commit comments

Comments
 (0)