forked from thuglabs/arweave-image-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader.js
191 lines (160 loc) · 5.06 KB
/
uploader.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import fs from "fs";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
import Arweave from "arweave";
import csv from "csv-parser";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const results = [];
const initOptions = {
host: "arweave.net", // Hostname or IP address for a Arweave host
port: 443, // Port
protocol: "https", // Network protocol http or https
timeout: 20000, // Network request timeouts in milliseconds
logging: false, // Enable network request logging
};
const getNftName = (name) => `ART #${name}`;
const getMetadata = (name, imageUrl, attributes) => ({
name: getNftName(name),
symbol: "",
description:
"You hold in your possession an OG thugbird. It was created with love for the Solana community by 0x_thug",
seller_fee_basis_points: 500,
external_url: "https://www.thugbirdz.com/",
attributes,
collection: {
name: "Test Collection",
family: "thugbirdz",
},
properties: {
files: [
{
uri: imageUrl,
type: "image/png",
},
],
category: "image",
maxSupply: 0,
creators: [
{
address: "CBBUMHRmbVUck99mTCip5sHP16kzGj3QTYB8K3XxwmQx",
share: 100,
},
],
},
image: imageUrl,
});
// run localy
// npx @textury/arlocal
const initOptionsLocal = {
host: "localhost", // Hostname or IP address for a Arweave host
port: 1984, // Port
protocol: "http", // Network protocol http or https
timeout: 20000, // Network request timeouts in milliseconds
// logging: false, // Enable network request logging
};
const arweave = Arweave.init(initOptions);
let key = null;
const runUpload = async (data, contentType, isUploadByChunk = false) => {
const tx = await arweave.createTransaction({ data: data }, key);
tx.addTag(...contentType);
await arweave.transactions.sign(tx, key);
if (isUploadByChunk) {
const uploader = await arweave.transactions.getUploader(tx);
while (!uploader.isComplete) {
await uploader.uploadChunk();
console.log(
`${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`
);
}
}
// Do we need to post with uploader?
await arweave.transactions.post(tx);
// console.log("url", `http://localhost:1984/${tx.id}`);
// console.log("url", `https://arweave.net/${tx.id}`);
return tx;
};
const folder = "./public/images/";
let metadataCollection = {};
const getAttributes = (props) => {
// map attributes to the proper key/value objects
const attrs = Object.keys(props).map((key) => {
return {
trait_type: key,
value: props[key],
};
});
return attrs;
};
const iterateOverItems = async () => {
try {
for (const row of results) {
// get separately name and props
const { Name: name, ...props } = row;
console.log("name", name);
const nameByNumber = Number.parseInt(name);
const filePath = folder + nameByNumber + ".png";
console.log("filePath", filePath);
let newItem = {};
try {
const data = fs.readFileSync(filePath);
// if (!data) console.warn(`Can't find file: ${filePath}`);
const contentType = ["Content-Type", "image/png"];
const { id } = await runUpload(data, contentType, true);
const imageUrl = id ? `https://arweave.net/${id}` : undefined;
console.log("imageUrl", imageUrl);
const attributes = getAttributes(props);
const metadata = getMetadata(name, imageUrl, attributes);
// console.log(metadata);
const metaContentType = ["Content-Type", "application/json"];
const metadataString = JSON.stringify(metadata);
const { id: metadataId } = await runUpload(
metadataString,
metaContentType
);
const metadataUrl = id
? `https://arweave.net/${metadataId}`
: undefined;
console.log("metadataUrl", metadataUrl);
newItem = {
[nameByNumber]: {
name: getNftName(name),
uri: metadataUrl,
},
};
} catch (error) {
newItem = {
[nameByNumber]: undefined,
};
}
// update collection with new item
metadataCollection = { ...metadataCollection, ...newItem };
}
// All images iterated
console.log(metadataCollection);
// Save data to json in /public/
const data = JSON.stringify(metadataCollection);
fs.writeFileSync("./public/arweave-images.json", data);
} catch (e) {
// Catch anything bad that happens
console.error("We've thrown! Whoops!", e);
}
};
const readCsv = async () => {
key = await arweave.wallets.generate();
fs.createReadStream(path.resolve(__dirname, "public", "data.csv"))
.pipe(csv())
.on("data", (data) => results.push(data))
.on("end", () => {
// console.log(results);
// {
// Name: '0000',
// 'Background Color': 'palegreen',
// 'Head Color': 'lightblue',
// 'Neck Color': 'lightslategray',
// ...
// },
iterateOverItems();
});
};
readCsv();