-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
289 lines (245 loc) 路 9.49 KB
/
Copy pathscript.js
File metadata and controls
289 lines (245 loc) 路 9.49 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const API_BASE = "https://api.github.com/repos/aakashpuree/class-11-notes/contents/";
let currentPath = "";
let pathHistory = [];
// DOM Elements
const fileGrid = document.getElementById("file-grid");
const filePathDisplay = document.getElementById("file-path");
const backBtn = document.getElementById("back-btn");
const statusBar = document.getElementById("status-bar");
// Modals
const previewModal = document.getElementById("preview-modal");
const closePreviewBtn = document.getElementById("close-preview");
const previewTitle = document.getElementById("preview-title");
const previewBody = document.getElementById("preview-body");
const btnDownloadPreview = document.getElementById("btn-download-preview");
const confirmModal = document.getElementById("confirm-modal");
const closeConfirmBtn = document.getElementById("close-confirm");
const confirmText = document.getElementById("confirm-text");
const confirmCancelBtn = document.getElementById("confirm-cancel");
const confirmYesBtn = document.getElementById("confirm-yes");
let confirmAction = null; // Store function to execute upon confirmation
// Initialize
document.addEventListener("DOMContentLoaded", () => {
loadPath("");
});
// Load Path Content
async function loadPath(path) {
showStatus("Loading...");
try {
const response = await fetch(API_BASE + path);
if (!response.ok) throw new Error("Failed to load contents");
const data = await response.json();
currentPath = path;
updateBreadcrumbs();
renderItems(data);
hideStatus();
} catch (error) {
showStatus("Error loading items. Check API limits or network.");
console.error(error);
}
}
// Update Path UI
function updateBreadcrumbs() {
filePathDisplay.innerText = currentPath === "" ? "/root/" : `/root/${currentPath}`;
if (currentPath === "") {
backBtn.disabled = true;
} else {
backBtn.disabled = false;
}
}
// Render Files and Folders
function renderItems(items) {
fileGrid.innerHTML = "";
// Sort: Folders first, then files
items.sort((a, b) => {
if (a.type === b.type) return a.name.localeCompare(b.name);
return a.type === 'dir' ? -1 : 1;
});
if (items.length === 0) {
fileGrid.innerHTML = "<p>This folder is empty.</p>";
return;
}
items.forEach(item => {
const card = document.createElement("div");
card.className = "card";
const isDir = item.type === "dir";
const iconClass = isDir ? "fa-solid fa-folder folder" : getFileIcon(item.name);
card.innerHTML = `
<i class="${iconClass} card-icon"></i>
<div class="card-title">${item.name}</div>
<div class="card-actions"></div>
`;
const actionsContainer = card.querySelector(".card-actions");
if (isDir) {
const openBtn = document.createElement("button");
openBtn.className = "btn btn-secondary";
openBtn.innerHTML = '<i class="fa-solid fa-folder-open"></i> Open';
openBtn.onclick = () => {
pathHistory.push(currentPath);
loadPath(item.path);
};
const dlBtn = document.createElement("button");
dlBtn.className = "btn btn-primary";
dlBtn.innerHTML = '<i class="fa-solid fa-download"></i> DL';
dlBtn.onclick = () => {
requestConfirm(`Are you sure you want to download the entire folder "${item.name}"? This will zip all files.`, () => {
downloadFolder(item);
});
};
actionsContainer.appendChild(openBtn);
actionsContainer.appendChild(dlBtn);
} else {
const previewBtn = document.createElement("button");
previewBtn.className = "btn btn-secondary";
previewBtn.innerHTML = '<i class="fa-solid fa-eye"></i> View';
previewBtn.onclick = () => previewFile(item);
const dlBtn = document.createElement("button");
dlBtn.className = "btn btn-primary";
dlBtn.innerHTML = '<i class="fa-solid fa-download"></i> DL';
dlBtn.onclick = () => {
requestConfirm(`Are you sure you want to download "${item.name}"?`, () => {
downloadFile(item);
});
};
actionsContainer.appendChild(previewBtn);
actionsContainer.appendChild(dlBtn);
}
fileGrid.appendChild(card);
});
}
function getFileIcon(filename) {
const ext = filename.split('.').pop().toLowerCase();
if (['pdf'].includes(ext)) return 'fa-solid fa-file-pdf card-icon';
if (['doc', 'docx'].includes(ext)) return 'fa-solid fa-file-word card-icon';
if (['jpg', 'jpeg', 'png', 'gif'].includes(ext)) return 'fa-solid fa-file-image card-icon';
if (['html', 'css', 'js', 'json'].includes(ext)) return 'fa-solid fa-file-code card-icon';
if (['txt', 'md'].includes(ext)) return 'fa-solid fa-file-lines card-icon';
return 'fa-solid fa-file card-icon';
}
// Navigation Back
backBtn.addEventListener("click", () => {
if (pathHistory.length > 0) {
const prevPath = pathHistory.pop();
loadPath(prevPath);
}
});
// Confirmation Modal
function requestConfirm(text, onConfirm) {
confirmText.innerText = text;
confirmAction = onConfirm;
confirmModal.classList.add("active");
}
function closeConfirm() {
confirmModal.classList.remove("active");
confirmAction = null;
}
closeConfirmBtn.onclick = closeConfirm;
confirmCancelBtn.onclick = closeConfirm;
confirmYesBtn.onclick = () => {
if (confirmAction) confirmAction();
closeConfirm();
};
// Preview Logic
async function previewFile(item) {
const ext = item.name.split('.').pop().toLowerCase();
previewTitle.innerText = item.name;
previewBody.innerHTML = "Loading preview...";
btnDownloadPreview.onclick = () => {
closePreview();
requestConfirm(`Are you sure you want to download "${item.name}"?`, () => downloadFile(item));
};
previewModal.classList.add("active");
try {
if (['jpg', 'jpeg', 'png', 'gif', 'webp'].includes(ext)) {
previewBody.innerHTML = `<img src="${item.download_url}" alt="${item.name}" style="max-width:100%; height:auto;">`;
return;
}
if (['pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx'].includes(ext)) {
const viewerUrl = `https://docs.google.com/gview?url=${encodeURIComponent(item.download_url)}&embedded=true`;
previewBody.innerHTML = `<iframe src="${viewerUrl}" style="width:100%; height:60vh; border:none;"></iframe>`;
return;
}
const response = await fetch(item.download_url);
const text = await response.text();
if (['md', 'markdown'].includes(ext)) {
previewBody.innerHTML = marked.parse(text);
} else if (['html', 'htm'].includes(ext)) {
const blob = new Blob([text], { type: 'text/html' });
const url = URL.createObjectURL(blob);
previewBody.innerHTML = `<iframe src="${url}" style="width:100%; height:60vh; border:none; background:#fff;"></iframe>`;
} else {
const pre = document.createElement("pre");
pre.textContent = text;
previewBody.innerHTML = '';
previewBody.appendChild(pre);
}
} catch (e) {
previewBody.innerHTML = "Error loading preview.";
}
}
function closePreview() {
previewModal.classList.remove("active");
}
closePreviewBtn.onclick = closePreview;
// Download File
function downloadFile(item) {
showStatus(`Downloading ${item.name}...`);
fetch(item.download_url)
.then(res => res.blob())
.then(blob => {
saveAs(blob, item.name);
hideStatus();
})
.catch(err => {
showStatus('Error downloading file.');
console.error(err);
});
}
// Download Folder via Zip
async function downloadFolder(item) {
showStatus(`Preparing zip for ${item.name}...`);
try {
const zip = new JSZip();
const rootFolder = zip.folder(item.name);
await fetchFolderRecursive(item.path, rootFolder);
showStatus('Generating Zip...');
const content = await zip.generateAsync({type: "blob"});
saveAs(content, `${item.name}.zip`);
hideStatus();
} catch (error) {
showStatus('Error zipping folder. Rate limit exceeded?');
console.error(error);
}
}
async function fetchFolderRecursive(path, zipFolder) {
const response = await fetch(API_BASE + path);
if (!response.ok) throw new Error("API error");
const items = await response.json();
for (const item of items) {
if (item.type === 'file') {
const res = await fetch(item.download_url);
const blob = await res.blob();
zipFolder.file(item.name, blob);
} else if (item.type === 'dir') {
const subFolder = zipFolder.folder(item.name);
await fetchFolderRecursive(item.path, subFolder);
}
}
}
// Helpers
function showStatus(msg) {
statusBar.innerText = msg;
statusBar.classList.remove("hidden");
}
function hideStatus() {
statusBar.classList.add("hidden");
}
// Close modals when clicking outside content
window.onclick = function(event) {
if (event.target === previewModal) {
closePreview();
}
if (event.target === confirmModal) {
closeConfirm();
}
}