-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathestuary.js
112 lines (97 loc) · 2.79 KB
/
estuary.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
require("dotenv").config();
const fs = require("fs");
const fsPromises = require("fs/promises");
const FormData = require("form-data");
const contentType = require("content-type");
const axios = require("axios");
const PostTimeout = 30000;
function saveJson(json, filename) {
try {
const data = JSON.stringify(json);
fs.writeFileSync(filename, data);
} catch (e) {
console.error("Failed to save JSON:", filename, e);
}
}
function loadJson(filename, ignore) {
try {
let rawData = fs.readFileSync(filename);
return JSON.parse(rawData);
} catch (e) {
if (!ignore) {
console.error("Failed to load JSON:", filename, e);
}
}
return null;
}
(async () => {
const estuary = loadJson("estuary.json", true) || {
uploads: {},
success: {},
};
const fn = `uploads-${Date.now()}.txt`;
try {
await fsPromises.rename("uploads.txt", fn);
const uploads = fs.readFileSync(fn, "utf8").trim().split("\n");
uploads.forEach((upload) => {
if (estuary.success[upload]) {
return;
}
estuary.uploads[upload] = true;
});
} catch {}
saveJson(estuary, "estuary.json");
try {
await fsPromises.rm(fn);
} catch {}
// console.log(estuary);
for (const upload of Object.keys(estuary.uploads)) {
try {
console.log("Fetching:", upload);
const res = await axios({
method: "get",
url: "https://ipfs.near.social/ipfs/" + upload,
timeout: PostTimeout,
headers: {
Referer: "https://near.social/",
"Referrer-Policy": "strict-origin-when-cross-origin",
},
responseType: "arraybuffer",
});
const body = res.data;
const contentType = res.headers["content-type"];
console.log("Fetched:", body.length, contentType, upload);
const formData = new FormData();
try {
formData.append("data", res.data, { filename: upload, contentType });
console.log(`Uploading ${body.length} bytes:`, upload);
} catch (e) {
estuary.success[upload] = false;
delete estuary.uploads[upload];
saveJson(estuary, "estuary.json");
throw e;
}
const upRes = await axios({
method: "post",
url: "https://api.estuary.tech/content/add",
headers: {
Accept: "application/json",
Authorization: `Bearer ${process.env.ESTUARY_KEY}`,
...formData.getHeaders(),
},
data: formData,
timeout: PostTimeout,
});
console.log("Uploaded:", upRes.data.cid);
estuary.success[upload] = true;
delete estuary.uploads[upload];
saveJson(estuary, "estuary.json");
} catch (e) {
console.error(e.toString());
console.log(e);
break;
}
}
console.log("All done");
saveJson(estuary, "estuary.json");
})();