2023-08-12 06:23:18 +03:00
|
|
|
const form = document.getElementById("userform")
|
|
|
|
const submit = document.getElementById("submit")
|
|
|
|
const results = document.getElementById("results")
|
|
|
|
|
|
|
|
const chunksize = 1000000;
|
|
|
|
|
|
|
|
const splitter = (file, hash) => {
|
|
|
|
let numberofChunks = Math.ceil(file.byteLength/chunksize);
|
|
|
|
let left = 0
|
|
|
|
let chunks = []
|
|
|
|
|
|
|
|
for (let i = 0; i < numberofChunks; i++) {
|
|
|
|
const chunkForm = new FormData();
|
|
|
|
let contentRange = "";
|
|
|
|
let chunk;
|
|
|
|
if(left+chunksize <= file.byteLength){
|
|
|
|
contentRange = `${left}-${left+chunksize}`
|
|
|
|
chunk = file.slice(left,left+chunksize)
|
|
|
|
left += chunksize
|
|
|
|
} else {
|
|
|
|
contentRange = `${left}-${file.byteLength}`
|
|
|
|
chunk = file.slice(left,file.byteLength)
|
|
|
|
}
|
|
|
|
chunkForm.append('file', new Blob([chunk], {type:"video/webm"}))
|
|
|
|
|
|
|
|
chunks.push(
|
2023-08-12 06:37:40 +03:00
|
|
|
fetch("/uploadchunk", {
|
2023-08-12 06:23:18 +03:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Number": i,
|
|
|
|
"Content-Range": `bytes ${contentRange}/${file.byteLength}`,
|
|
|
|
"File-Hash": hash
|
|
|
|
},
|
|
|
|
body: chunkForm
|
2023-08-12 06:37:40 +03:00
|
|
|
}).then(res => Promise.resolve(res.status))
|
|
|
|
)
|
2023-08-12 06:23:18 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
return chunks
|
|
|
|
}
|
|
|
|
|
|
|
|
async function digest(data){
|
|
|
|
|
|
|
|
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
|
|
return hash
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const processFile = file => {
|
|
|
|
const fr = new FileReader()
|
|
|
|
fr.readAsArrayBuffer(file)
|
|
|
|
|
|
|
|
fr.addEventListener("loadend", e => {
|
|
|
|
digest(e.target.result).then(digestBuffer => {
|
|
|
|
const hashAsString = Array.from(new Uint8Array(digestBuffer)).map((b) => b.toString(16).padStart(2, "0")).join(""); // hex the buffer for readability
|
|
|
|
|
2023-08-12 06:37:40 +03:00
|
|
|
let details = JSON.stringify({"usercode": "anonymous", "hash": hashAsString, "size": file.size, "type": file.type})
|
|
|
|
|
|
|
|
fetch(`/announce`, {
|
|
|
|
method: "POST",
|
|
|
|
headers:{
|
|
|
|
"Content-Type": "json",
|
|
|
|
},
|
|
|
|
body: details
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if(res.status == 200){
|
|
|
|
Promise.all(splitter(e.target.result,hashAsString))
|
|
|
|
.then((values) => {
|
|
|
|
if(values.every(x => x === 200)){
|
|
|
|
fetch(`/finish`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "json",
|
|
|
|
},
|
|
|
|
body: details
|
|
|
|
}).then(res => res.json())
|
|
|
|
.then(res => {
|
|
|
|
console.log(res)
|
|
|
|
results.innerHTML += `<a href="/${res.file}">https://i.hjmt.xyz/${res.file}</a><br>`
|
|
|
|
})
|
|
|
|
}else{
|
|
|
|
alert("upload failed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}else if(res.status == 409){
|
|
|
|
alert("file exists!")
|
|
|
|
}else if(res.status == 403){
|
|
|
|
alert("get a usertoken for bigger files")
|
|
|
|
}
|
2023-08-12 06:23:18 +03:00
|
|
|
})
|
2023-08-12 06:37:40 +03:00
|
|
|
})
|
|
|
|
|
2023-08-12 06:23:18 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
submit.addEventListener("click", e => {
|
2023-08-12 06:37:40 +03:00
|
|
|
e.preventDefault();
|
2023-08-12 06:23:18 +03:00
|
|
|
for (const file of form.media.files){
|
|
|
|
processFile(file)
|
|
|
|
}
|
2023-08-12 06:37:40 +03:00
|
|
|
})
|
|
|
|
|