-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (51 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const fs = require("fs");
const mime = require("mime");
const path = require("path");
const { NFTStorage, File } = require("nft.storage");
const config = require("./config.json");
async function fileFromPath(filePath) {
const content = await fs.promises.readFile(filePath);
const type = mime.getType(filePath);
return new File([content], path.basename(filePath), { type });
}
function getAllImages() {
const supportImageTypes = ["jpg", "jpeg", "png"];
let images = [];
fs.readdirSync(config.imagesDirectory).forEach((file) => {
if (supportImageTypes.includes(file.split(".").pop())) {
images.push(path.join(config.imagesDirectory, file));
}
});
return images;
}
function getAllMetadata() {
let metadata = [];
fs.readdirSync(config.metadataDirectory).forEach((file) => {
metadata.push(path.join(config.metadataDirectory, file));
});
return metadata;
}
async function main() {
const allImages = getAllImages();
const allMetadata = getAllMetadata();
if (allImages.length !== allMetadata.length) {
return console.log("Error: Number of images and metadata files do not match");
}
let allRequests = [];
for (let i = 0; i < allImages.length; i++) {
const imageBlob = await fileFromPath(allImages[i]);
allRequests.push(imageBlob);
const metadataBlob = await fileFromPath(allMetadata[i]);
allRequests.push(metadataBlob);
}
const client = new NFTStorage({ token: config.nftStorageKey });
const cid = await client.storeDirectory(allRequests);
console.log(cid);
fs.writeFile("cid.txt", cid, function (err) {
if (err) return console.log(err);
console.log("cid saved to cid.txt");
});
}
main()
.then(() => console.log("Done"))
.catch(console.error);