2021-10-24 02:25:42 -07:00
|
|
|
// Exported to main.rs
|
|
|
|
function loadFromDb() {
|
|
|
|
const dbReq = window.indexedDB.open("omegaupload", 1);
|
|
|
|
dbReq.onsuccess = (evt) => {
|
|
|
|
const db = (evt.target as IDBRequest).result;
|
|
|
|
const obj_store = db
|
2021-10-24 11:40:19 -07:00
|
|
|
.transaction("decrypted data")
|
|
|
|
.objectStore("decrypted data");
|
|
|
|
let fetchReq = obj_store.get(window.location.pathname);
|
|
|
|
fetchReq.onsuccess = (evt) => {
|
2021-10-24 02:25:42 -07:00
|
|
|
const data = (evt.target as IDBRequest).result;
|
|
|
|
switch (data.type) {
|
|
|
|
case "string":
|
|
|
|
createStringPasteUi(data);
|
|
|
|
break;
|
|
|
|
case "blob":
|
|
|
|
createBlobPasteUi(data);
|
|
|
|
break;
|
|
|
|
case "image":
|
|
|
|
createImagePasteUi(data);
|
|
|
|
break;
|
2021-10-24 11:40:19 -07:00
|
|
|
case "audio":
|
|
|
|
createAudioPasteUi(data);
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
createVideoPasteUi(data);
|
|
|
|
break;
|
2021-10-26 18:19:14 -07:00
|
|
|
case "archive":
|
|
|
|
createArchivePasteUi(data);
|
|
|
|
break;
|
2021-10-24 02:25:42 -07:00
|
|
|
default:
|
2021-10-25 02:42:20 -07:00
|
|
|
renderMessage("Something went wrong. Try clearing local data.");
|
2021-10-24 02:25:42 -07:00
|
|
|
break;
|
|
|
|
}
|
2021-10-24 11:40:19 -07:00
|
|
|
|
|
|
|
// IDB was only used as a temporary medium;
|
|
|
|
window.onbeforeunload = (e) => {
|
|
|
|
// See https://link.eddie.sh/NrIIq on why .commit is necessary.
|
|
|
|
const transaction = db.transaction("decrypted data", "readwrite");
|
|
|
|
transaction
|
|
|
|
.objectStore("decrypted data")
|
|
|
|
.delete(window.location.pathname);
|
|
|
|
transaction.commit();
|
|
|
|
transaction.oncomplete = () => {
|
|
|
|
console.log("Item deleted from cache");
|
|
|
|
}
|
|
|
|
};
|
2021-10-24 02:25:42 -07:00
|
|
|
};
|
|
|
|
|
2021-10-24 11:40:19 -07:00
|
|
|
fetchReq.onerror = (evt) => {
|
2021-10-24 02:25:42 -07:00
|
|
|
console.log("err");
|
|
|
|
console.log(evt);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createStringPasteUi(data) {
|
|
|
|
let bodyEle = document.getElementsByTagName("body")[0];
|
|
|
|
bodyEle.textContent = '';
|
|
|
|
|
|
|
|
let mainEle = document.createElement("main");
|
|
|
|
let preEle = document.createElement("pre");
|
|
|
|
preEle.classList.add("paste");
|
|
|
|
|
2021-10-27 01:49:06 -07:00
|
|
|
let headerEle = document.createElement("p");
|
2021-10-24 02:25:42 -07:00
|
|
|
headerEle.classList.add("unselectable");
|
2021-10-27 01:49:06 -07:00
|
|
|
headerEle.classList.add("centered");
|
2021-10-24 02:25:42 -07:00
|
|
|
headerEle.textContent = data.expiration;
|
|
|
|
preEle.appendChild(headerEle);
|
|
|
|
|
|
|
|
preEle.appendChild(document.createElement("hr"));
|
|
|
|
|
|
|
|
let codeEle = document.createElement("code");
|
|
|
|
codeEle.textContent = data.data;
|
|
|
|
preEle.appendChild(codeEle);
|
|
|
|
|
|
|
|
mainEle.appendChild(preEle);
|
|
|
|
bodyEle.appendChild(mainEle);
|
|
|
|
|
|
|
|
hljs.highlightAll();
|
|
|
|
hljs.initLineNumbersOnLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
function createBlobPasteUi(data) {
|
|
|
|
let bodyEle = document.getElementsByTagName("body")[0];
|
|
|
|
bodyEle.textContent = '';
|
|
|
|
|
|
|
|
let mainEle = document.createElement("main");
|
|
|
|
mainEle.classList.add("hljs");
|
|
|
|
mainEle.classList.add("centered");
|
|
|
|
mainEle.classList.add("fullscreen");
|
|
|
|
|
|
|
|
let divEle = document.createElement("div");
|
|
|
|
divEle.classList.add("centered");
|
|
|
|
|
|
|
|
let expirationEle = document.createElement("p");
|
|
|
|
expirationEle.textContent = data.expiration;
|
|
|
|
divEle.appendChild(expirationEle);
|
|
|
|
|
|
|
|
let downloadEle = document.createElement("a");
|
|
|
|
downloadEle.href = URL.createObjectURL(data.data);
|
|
|
|
downloadEle.download = window.location.pathname;
|
|
|
|
downloadEle.classList.add("hljs-meta");
|
|
|
|
downloadEle.textContent = "Download binary file.";
|
|
|
|
divEle.appendChild(downloadEle);
|
|
|
|
|
|
|
|
|
|
|
|
mainEle.appendChild(divEle);
|
|
|
|
|
|
|
|
let displayAnywayEle = document.createElement("p");
|
|
|
|
displayAnywayEle.classList.add("display-anyways");
|
|
|
|
displayAnywayEle.classList.add("hljs-comment");
|
|
|
|
displayAnywayEle.textContent = "Display anyways?";
|
|
|
|
displayAnywayEle.onclick = () => {
|
|
|
|
data.data.text().then(text => {
|
|
|
|
data.data = text;
|
|
|
|
createStringPasteUi(data);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
mainEle.appendChild(displayAnywayEle);
|
|
|
|
bodyEle.appendChild(mainEle);
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:31:30 -07:00
|
|
|
function createImagePasteUi({ expiration, data, file_size }) {
|
|
|
|
createMultiMediaPasteUi("img", expiration, data, (downloadEle, imgEle) => {
|
|
|
|
imgEle.onload = () => {
|
|
|
|
downloadEle.textContent = "Download " + file_size + " \u2014 " + imgEle.naturalWidth + " by " + imgEle.naturalHeight;
|
|
|
|
}
|
|
|
|
});
|
2021-10-24 11:40:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function createAudioPasteUi({ expiration, data }) {
|
|
|
|
createMultiMediaPasteUi("audio", expiration, data, "Download");
|
|
|
|
}
|
|
|
|
|
|
|
|
function createVideoPasteUi({ expiration, data }) {
|
|
|
|
createMultiMediaPasteUi("video", expiration, data, "Download");
|
|
|
|
}
|
|
|
|
|
2021-10-26 18:19:14 -07:00
|
|
|
function createArchivePasteUi({ expiration, data, entries }) {
|
|
|
|
let bodyEle = document.getElementsByTagName("body")[0];
|
|
|
|
bodyEle.textContent = '';
|
|
|
|
|
|
|
|
let mainEle = document.createElement("main");
|
|
|
|
|
2021-10-27 01:49:06 -07:00
|
|
|
let sectionEle = document.createElement("section");
|
|
|
|
sectionEle.classList.add("paste");
|
2021-10-26 18:19:14 -07:00
|
|
|
|
|
|
|
let expirationEle = document.createElement("p");
|
|
|
|
expirationEle.textContent = expiration;
|
2021-10-27 01:49:06 -07:00
|
|
|
expirationEle.classList.add("centered");
|
|
|
|
sectionEle.appendChild(expirationEle);
|
|
|
|
|
|
|
|
let downloadEle = document.createElement("a");
|
|
|
|
downloadEle.href = URL.createObjectURL(data);
|
|
|
|
downloadEle.download = window.location.pathname;
|
|
|
|
downloadEle.textContent = "Download";
|
|
|
|
downloadEle.classList.add("hljs-meta");
|
|
|
|
downloadEle.classList.add("centered");
|
|
|
|
sectionEle.appendChild(downloadEle);
|
|
|
|
|
|
|
|
sectionEle.appendChild(document.createElement("hr"));
|
2021-10-26 18:19:14 -07:00
|
|
|
|
|
|
|
let mediaEle = document.createElement("table");
|
2021-10-27 01:49:06 -07:00
|
|
|
mediaEle.classList.add("archive-table");
|
2021-10-26 18:19:14 -07:00
|
|
|
const tr = mediaEle.insertRow();
|
2021-10-27 01:49:06 -07:00
|
|
|
tr.classList.add("hljs-title");
|
2021-10-26 18:19:14 -07:00
|
|
|
const tdName = tr.insertCell();
|
2021-10-27 01:49:06 -07:00
|
|
|
tdName.textContent = "Name";
|
2021-10-26 18:19:14 -07:00
|
|
|
const tdSize = tr.insertCell();
|
2021-10-27 01:49:06 -07:00
|
|
|
tdSize.classList.add("align-right");
|
|
|
|
tdSize.textContent = "File Size";
|
|
|
|
|
|
|
|
// Because it's a stable sort, we can first sort by name (to get all folder
|
|
|
|
// items grouped together) and then sort by if there's a / or not.
|
|
|
|
entries.sort((a, b) => {
|
|
|
|
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
|
|
|
|
});
|
|
|
|
|
2021-10-27 02:23:28 -07:00
|
|
|
// This doesn't get sub directories and their folders, but hey it's close
|
|
|
|
// enough
|
2021-10-27 01:49:06 -07:00
|
|
|
entries.sort((a, b) => {
|
|
|
|
return b.name.includes("/") - a.name.includes("/");
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const { name, file_size } of entries) {
|
2021-10-26 18:19:14 -07:00
|
|
|
const tr = mediaEle.insertRow();
|
|
|
|
const tdName = tr.insertCell();
|
2021-10-27 01:49:06 -07:00
|
|
|
tdName.textContent = name;
|
2021-10-26 18:19:14 -07:00
|
|
|
const tdSize = tr.insertCell();
|
2021-10-27 01:49:06 -07:00
|
|
|
tdSize.textContent = file_size;
|
|
|
|
tdSize.classList.add("align-right");
|
|
|
|
tdSize.classList.add("hljs-number");
|
2021-10-26 18:19:14 -07:00
|
|
|
}
|
|
|
|
|
2021-10-27 01:49:06 -07:00
|
|
|
sectionEle.appendChild(mediaEle);
|
|
|
|
mainEle.appendChild(sectionEle);
|
2021-10-26 18:19:14 -07:00
|
|
|
bodyEle.appendChild(mainEle);
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:31:30 -07:00
|
|
|
function createMultiMediaPasteUi(tag, expiration, data, on_create?) {
|
2021-10-24 11:40:19 -07:00
|
|
|
let bodyEle = document.getElementsByTagName("body")[0];
|
|
|
|
bodyEle.textContent = '';
|
|
|
|
|
|
|
|
let mainEle = document.createElement("main");
|
|
|
|
mainEle.classList.add("hljs");
|
|
|
|
mainEle.classList.add("centered");
|
|
|
|
mainEle.classList.add("fullscreen");
|
|
|
|
|
|
|
|
const downloadLink = URL.createObjectURL(data);
|
|
|
|
|
|
|
|
let expirationEle = document.createElement("p");
|
|
|
|
expirationEle.textContent = expiration;
|
|
|
|
mainEle.appendChild(expirationEle);
|
|
|
|
|
2021-10-25 17:31:30 -07:00
|
|
|
let mediaEle = document.createElement(tag);
|
|
|
|
mediaEle.src = downloadLink;
|
|
|
|
mediaEle.controls = true;
|
|
|
|
mainEle.appendChild(mediaEle);
|
2021-10-24 11:40:19 -07:00
|
|
|
|
2021-10-27 01:49:06 -07:00
|
|
|
|
2021-10-24 11:40:19 -07:00
|
|
|
let downloadEle = document.createElement("a");
|
|
|
|
downloadEle.href = downloadLink;
|
|
|
|
downloadEle.download = window.location.pathname;
|
|
|
|
downloadEle.classList.add("hljs-meta");
|
|
|
|
mainEle.appendChild(downloadEle);
|
|
|
|
|
|
|
|
bodyEle.appendChild(mainEle);
|
2021-10-25 17:31:30 -07:00
|
|
|
|
|
|
|
if (on_create instanceof Function) {
|
|
|
|
on_create(downloadEle, mediaEle);
|
|
|
|
} else {
|
|
|
|
downloadEle.textContent = on_create;
|
|
|
|
}
|
2021-10-24 11:40:19 -07:00
|
|
|
}
|
|
|
|
|
2021-10-25 02:42:20 -07:00
|
|
|
function renderMessage(message) {
|
2021-10-24 02:25:42 -07:00
|
|
|
let body = document.getElementsByTagName("body")[0];
|
|
|
|
body.textContent = '';
|
|
|
|
let mainEle = document.createElement("main");
|
|
|
|
mainEle.classList.add("hljs");
|
|
|
|
mainEle.classList.add("centered");
|
|
|
|
mainEle.classList.add("fullscreen");
|
|
|
|
mainEle.textContent = message;
|
2021-10-25 02:42:20 -07:00
|
|
|
body.appendChild(mainEle);
|
2021-10-24 02:25:42 -07:00
|
|
|
}
|
|
|
|
|
2021-10-26 18:19:14 -07:00
|
|
|
window.addEventListener("hashchange", () => location.reload());
|