-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (141 loc) · 4.11 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
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
const http = require('http')
const fetch = require('node-fetch');
const url = require('url');
const AbortController = require('abort-controller');
const Hash = require('ipfs-only-hash')
var token = process.env.WEB3_TOKEN
var ipfs_api = process.env.IPFS_API
async function hash(url) {
var download = await fetch(url)
// var downloadClone = await download.clone();
// var length = (await downloadClone.text()).length
var cid = await Hash.of(download.body, {cidVersion: 1, rawLeaves: true})
var json = {}
// json.length = length
json.cid = cid
return json
}
async function upload(url, filename) {
var downloadStart = Date.now()
var download = await fetch(url)
var controller = new AbortController();
var timeout = setTimeout(() => {
controller.abort();
}, 1000*20);
if (download.headers.get('content-length')){
var length = download.headers.get('content-length')
} else {
var downloadClone = await download.clone();
var length = (await downloadClone.text()).length
}
var downloadEnd = Date.now()
var downloadTime = downloadEnd-downloadStart
var max_size = 1024*1000*30
if (length > max_size){
console.log(JSON.stringify({url: url, length: length, error: 'too big'}))
return {length: length, error: 'too big'}
}
try {
var uploadStart = Date.now()
var upload = await fetch(`https://api.web3.storage/upload`, {
method: 'POST',
headers: {
"Authorization": `Bearer ${token}`,
"x-name": filename
},
body: download.body,
signal: controller.signal
})
var json = await upload.json()
var uploadEnd = Date.now()
var uploadTime = uploadEnd-uploadStart
json.filename = filename
json.length = length
json.download = downloadTime
json.upload = uploadTime
if (json.cid){
console.log(JSON.stringify(json))
} else {
var error = json
error.url = url
error.filename = filename
console.error(JSON.stringify(error))
}
return json
} catch (error) {
console.error(error)
console.log(JSON.stringify({error: "timeout", filename: filename, length: length}));
return {error: "timeout", length: length}
} finally {
clearTimeout(timeout);
}
}
async function transport(cid, filename) {
if(!cid){ return {} }
console.log('Uploading', cid, filename)
var url = `${ipfs_api}/api/v0/dag/export?arg=${cid}&carversion=1`
var download = await fetch(url, { method: 'POST' })
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 1000*30);
try {
var upload = await fetch(`https://api.web3.storage/car`, {
method: 'POST',
headers: {
"Authorization": `Bearer ${token}`,
"x-name": filename
},
body: download.body,
signal: controller.signal
})
var json = await upload.json()
if (json.cid){
console.log(JSON.stringify(json))
} else {
var error = json
error.cid = cid
error.filename = filename
var download = await fetch(url, { method: 'POST' })
var txt = await download.text()
error.length = txt.length
console.error(JSON.stringify(error))
}
clearTimeout(timeout);
return json
} catch (error) {
console.log(JSON.stringify({error: "timeout", cid: cid, filename: filename}));
clearTimeout(timeout);
return {}
} finally {
clearTimeout(timeout);
}
}
const handler = async function (req, res) {
var parsedUrl = url.parse(req.url,true)
var query = parsedUrl.query
if(parsedUrl.pathname == '/upload'){
if(query.url){
var result = await upload(query.url, query.filename)
} else {
var result = {}
}
} else if (parsedUrl.pathname == '/hash') {
if(query.url){
var result = await hash(query.url)
} else {
var result = {}
}
} else {
var result = await transport(query.cid, query.filename)
}
res.setHeader('Content-Type', 'application/json');
if (result.cid){
res.writeHead(200);
} else {
res.writeHead(500);
}
res.end(JSON.stringify(result));
}
const server = http.createServer(handler);
server.listen(process.env.PORT || 8080);