2024-06-14 15:53:35 +00:00
|
|
|
const client = new WebTorrent();
|
2024-06-14 16:32:51 +00:00
|
|
|
const trackerUrl = "<%= trackerUrl %>";
|
2024-06-14 15:53:35 +00:00
|
|
|
|
2024-06-14 16:32:51 +00:00
|
|
|
async function getRTCIceServers() {
|
|
|
|
const response = await fetch("/turn-credentials");
|
|
|
|
const data = await response.json();
|
|
|
|
return data.iceServers;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uploadFile() {
|
2024-06-14 15:53:35 +00:00
|
|
|
const fileInput = document.getElementById("fileInput");
|
|
|
|
const file = fileInput.files[0];
|
|
|
|
const uploadProgressBar = document.getElementById("uploadProgressBar");
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
alert("Please select a file to upload.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-14 16:32:51 +00:00
|
|
|
const rtcConfig = {
|
|
|
|
iceServers: await getRTCIceServers(),
|
|
|
|
};
|
|
|
|
|
2024-06-14 15:53:35 +00:00
|
|
|
const opts = {
|
|
|
|
announce: [trackerUrl],
|
2024-06-14 16:32:51 +00:00
|
|
|
rtcConfig: rtcConfig,
|
2024-06-14 15:53:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
client.seed(file, opts, (torrent) => {
|
|
|
|
fetch(`/generate-mnemonic/${torrent.infoHash}`)
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
const uploadResult = document.getElementById("uploadResult");
|
2024-06-14 16:32:51 +00:00
|
|
|
uploadResult.innerHTML = `<p>Sharing your file. Share this mnemonic: <strong>${data.mnemonic}</strong></p>
|
|
|
|
<p>The file will be available for download as long as you keep this page open.</p>`;
|
2024-06-14 15:53:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
torrent.on("upload", () => {
|
|
|
|
const progress = Math.round((torrent.uploaded / torrent.length) * 100);
|
|
|
|
uploadProgressBar.style.width = `${progress}%`;
|
|
|
|
uploadProgressBar.textContent = `${progress}%`;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-14 16:32:51 +00:00
|
|
|
async function downloadFile() {
|
2024-06-14 15:53:35 +00:00
|
|
|
const mnemonicInput = document.getElementById("mnemonicInput").value;
|
|
|
|
const downloadProgressBar = document.getElementById("downloadProgressBar");
|
|
|
|
|
|
|
|
if (!mnemonicInput) {
|
|
|
|
alert("Please enter a mnemonic.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-14 16:32:51 +00:00
|
|
|
const rtcConfig = {
|
|
|
|
iceServers: await getRTCIceServers(),
|
|
|
|
};
|
|
|
|
|
2024-06-14 15:53:35 +00:00
|
|
|
fetch(`/get-infohash/${mnemonicInput}`)
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
const torrentId = data.infoHash;
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
announce: [trackerUrl],
|
2024-06-14 16:32:51 +00:00
|
|
|
rtcConfig: rtcConfig,
|
2024-06-14 15:53:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
client.add(torrentId, opts, (torrent) => {
|
|
|
|
torrent.files[0].getBlob((err, blob) => {
|
|
|
|
if (err) {
|
|
|
|
const downloadResult = document.getElementById("downloadResult");
|
|
|
|
downloadResult.innerHTML = `Error: ${err.message}`;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
const a = document.createElement("a");
|
|
|
|
a.href = url;
|
|
|
|
a.download = torrent.files[0].name;
|
|
|
|
a.click();
|
|
|
|
|
|
|
|
const downloadResult = document.getElementById("downloadResult");
|
|
|
|
downloadResult.innerHTML = `File downloaded: <strong>${torrent.files[0].name}</strong>`;
|
|
|
|
});
|
|
|
|
|
|
|
|
torrent.on("download", () => {
|
|
|
|
const progress = Math.round(
|
|
|
|
(torrent.downloaded / torrent.length) * 100
|
|
|
|
);
|
|
|
|
downloadProgressBar.style.width = `${progress}%`;
|
|
|
|
downloadProgressBar.textContent = `${progress}%`;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|