filehost/site/script.js
2023-08-12 06:37:40 +03:00

105 lines
2.5 KiB
JavaScript

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(
fetch("/uploadchunk", {
method: "POST",
headers: {
"Content-Number": i,
"Content-Range": `bytes ${contentRange}/${file.byteLength}`,
"File-Hash": hash
},
body: chunkForm
}).then(res => Promise.resolve(res.status))
)
}
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
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")
}
})
})
})
}
submit.addEventListener("click", e => {
e.preventDefault();
for (const file of form.media.files){
processFile(file)
}
})