File size: 4,526 Bytes
858ab9f 46f8a04 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | let token = "";
// ------------------ ADMIN LOGIN ------------------
async function loginAdmin() {
const username = document.getElementById("adminUser").value;
const password = document.getElementById("adminPass").value;
if (!username || !password) {
alert("Please enter username and password");
return;
}
const formData = new URLSearchParams();
formData.append("username", username);
formData.append("password", password);
formData.append("grant_type", "password");
try {
const response = await fetch("/login", {
method: "POST",
headers: {"Content-Type":"application/x-www-form-urlencoded"},
body: formData
});
const data = await response.json();
if (response.ok) {
token = data.access_token;
localStorage.setItem("token", token);
alert("Login Successful");
document.getElementById("authSection").style.display = "none";
document.getElementById("adminPanel").style.display = "block";
loadDocs();
} else {
alert(data.detail || "Login Failed");
}
} catch (err) {
console.error(err);
alert("Login request failed");
}
}
// ------------------ UPLOAD DOCUMENT ------------------
async function uploadDocument() {
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
if (!file) {
alert("Please select a file");
return;
}
alert("File is being uploaded and indexed. Please wait ...");
const formData = new FormData();
formData.append("file", file);
try {
const res = await fetch("/admin/upload-document", {
headers: {"Authorization": "Bearer " + token},
method: "POST",
body: formData
});
const data = await res.json();
if (data.message) {
alert(data.message);
} else if (data.error) {
alert(data.error);
}
loadDocs();
} catch (err) {
console.error(err);
alert("Upload failed");
}
}
// ------------------ OTHER EXISTING FUNCTIONS ------------------
async function loadDocs() {
try {
const response = await fetch("/admin/list-documents", {
headers: {"Authorization": "Bearer " + token}
});
if (!response.ok) throw new Error("Failed to fetch documents");
const data = await response.json();
const table = document.getElementById("docTable");
if (!table) return;
let rows = `<tr><th>Document</th><th>Chunks</th><th>Action</th></tr>`;
if (data.documents.length === 0) {
rows += `<tr><td colspan="3">No documents found</td></tr>`;
} else {
data.documents.forEach(doc => {
rows += `<tr>
<td><a href="/uploads/${encodeURIComponent(doc.document)}" target="_blank">${doc.document}</a></td>
<td>${doc.chunks}</td>
<td><button onclick='deleteDocument(${JSON.stringify(doc.document)}, event)'>Delete</button></td>
</tr>`;
});
}
table.innerHTML = rows;
} catch (err) {
console.error(err);
}
}
async function deleteDocument(name, event) {
if (!confirm("Delete " + name + " ?")) return;
try {
const btn = event.target;
btn.disabled = true;
btn.innerText = "Deleting...";
const params = new URLSearchParams({ filename: name });
const res = await fetch(`/admin/delete-document?${params.toString()}`, {
headers: {"Authorization": "Bearer " + token},
method: "DELETE"
});
const data = await res.json();
alert(data.message || "Deleted successfully");
await loadDocs();
} catch (err) {
console.error(err);
alert("Error deleting file");
}
}
async function deleteFolder() {
const folder = prompt("Enter folder name to delete");
if (!folder) return;
await fetch(`/admin/delete-folder?folder=${encodeURIComponent(folder)}`, {
method: "DELETE",
headers: {"Authorization": "Bearer " + token}
});
loadDocs();
}
async function resetIndex() {
if (!confirm("Reset entire index?")) return;
await fetch("/admin/reset-index?confirm=true", {
headers: {"Authorization": "Bearer " + token},
method: "DELETE"
});
loadDocs();
} |