a / index.js
asdass1's picture
Update index.js
51ae27a verified
import crc32 from './crc32.js'
import http from 'axios'
import express from 'express'
const app = express()
const port = 7860
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
const splitBuffer = (buffer, n) => {
const partSize = Math.ceil(buffer.length / n)
const parts = []
for (let i = 0; i < n; i++) {
const start = i * partSize
const end = start + partSize
parts.push(buffer.slice(start, end))
}
return parts
}
app.get('*', async (req, res) => {
try {
const file_url = req.path.replace(/^\//, '')
const file = await http({
method: 'GET',
url: file_url,
responseType: 'arraybuffer'
})
const files_list = splitBuffer(file.data, 5)
const upload_info = await http({
method: 'GET',
url: 'https://data.emmmm.eu.org/upload/cn'
})
const upload_part_info = await http({
method: 'POST',
url: upload_info.data.url,
params: {
uploadmode: 'part',
phase: 'init'
},
headers: {
'authorization': upload_info.data.authorization
}
})
const uploadid = upload_part_info.data.data.uploadid
console.log(uploadid)
const upload_list = {}
await Promise.all(files_list.map(async (item, index) => {
const crc32_text = crc32(item)
let count = 1
while (count && count <= 5) {
const upload_result = await http({
method: 'POST',
url: upload_info.data.url,
params: {
uploadid: uploadid,
part_number: index + 1,
phase: 'transfer'
},
headers: {
'content-crc32': crc32_text,
'authorization': upload_info.data.authorization
},
data: item
})
if (upload_result.data.data.crc32 == crc32_text) {
count = 0
upload_list[index + 1] = crc32_text
} else {
count += 1
if (count > 5) {
throw Error
}
}
}
}))
const finish = Object.entries(upload_list).map(i => i.join(':')).join(',')
const upload_result = await http({
method: 'POST',
url: upload_info.data.url,
params: {
uploadid: uploadid,
uploadmode: 'part',
phase: 'finish'
},
headers: {
'authorization': upload_info.data.authorization
},
data: finish
})
if (upload_result.data.code == 2000) {
res.send(JSON.stringify({
vid: upload_info.data.vid,
url: `https://data.emmmm.eu.org/parse/zj/${upload_info.data.uri}`
}))
} else {
throw Error
}
} catch (e) {
res.send(e.toString())
}
})
app.listen(port, async () => {
console.log(`http://localhost:${port}`)
})