This repository was archived by the owner on Jun 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
305 lines (281 loc) · 9.2 KB
/
index.html
File metadata and controls
305 lines (281 loc) · 9.2 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>File Manager - codehex16</title>
<link
href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap"
rel="stylesheet"
/>
<style>
body {
font-family: "Roboto", sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
}
.container {
max-width: 900px;
margin: 20px auto;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 24px;
font-weight: 500;
margin-bottom: 20px;
color: #212121;
}
.file-list {
width: 100%;
border-collapse: collapse;
margin: 0;
}
.file-list th,
.file-list td {
padding: 12px 16px;
text-align: left;
border-bottom: 1px solid #eee;
}
.file-list th {
background-color: #f9f9f9;
font-weight: 500;
font-size: 14px;
}
.file-list td {
font-size: 14px;
vertical-align: middle;
}
.file-icon {
font-size: 24px;
margin-right: 8px;
vertical-align: middle;
}
.file-name {
text-decoration: none;
color: #6200ea;
font-weight: 500;
}
.file-name:hover {
text-decoration: underline;
}
.icon-btn {
background: none;
border: none;
cursor: pointer;
font-size: 24px;
color: #333;
position: relative;
}
.icon-btn:hover {
color: #6200ea;
}
.icon-btn .tooltip {
display: none;
position: absolute;
top: -25px;
left: 50%;
transform: translateX(-50%);
background: #333;
color: #fff;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
}
.icon-btn:hover .tooltip {
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1>File Manager</h1>
<table class="file-list" id="file-list">
<thead>
<tr>
<th>File/Folder</th>
<th>File Size</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
const apiUrl =
"https://api.github.com/repos/codehex16/resources/contents";
const baseUrl = "https://codehex16.github.io/resources";
const rawBaseUrl =
"https://raw.githubusercontent.com/CodeHex16/resources/main";
const fileIcons = {
folder: "folder",
pdf: "picture_as_pdf",
doc: "description",
docx: "description",
ppt: "slideshow",
pptx: "slideshow",
xls: "table_chart",
xlsx: "table_chart",
png: "image",
jpg: "image",
jpeg: "image",
gif: "image",
mp3: "audiotrack",
mp4: "movie",
zip: "folder_zip",
txt: "article",
default: "insert_drive_file",
};
function getQueryParameter(name) {
const params = new URLSearchParams(window.location.search);
return params.get(name) || "/";
}
function getParentDirectory() {
const parts = getQueryParameter("dir").split("/").filter((part) => part !== "");
parts.pop();
return `/${parts.join("/")}`;
}
async function fetchDirectory(path = "/") {
try {
// Replace %2F in path with '/' to handle directories properly
path = path.replace(/%2F/g, "/");
const response = await fetch(`${apiUrl}${path}`);
if (response.status === 403) {
const retryAfter = response.headers.get("Retry-After");
const message = retryAfter
? `API rate limit exceeded. Please try again after ${retryAfter} seconds.`
: "API rate limit exceeded. Please try again after 1 hour.";
console.error(message);
alert(message); // Optionally, show an alert to the user
return;
}
const files = await response.json();
const tableBody = document.querySelector("#file-list tbody");
tableBody.innerHTML = ""; // Clear existing rows
if (path !== "/" && path !== "%2F" && path !== "") {
const row = document.createElement("tr");
const iconCell = document.createElement("td");
const icon = document.createElement("span");
icon.classList.add("material-icons", "file-icon");
icon.textContent = "arrow_back";
const link = document.createElement("a");
link.href = `?dir=${encodeURIComponent(getParentDirectory())}`;
link.textContent = "..";
link.classList.add("file-name");
iconCell.appendChild(icon);
iconCell.appendChild(link);
row.appendChild(iconCell);
row.innerHTML += "<td></td><td></td>"; // Empty cells for size and actions
tableBody.appendChild(row);
}
for (const file of files) {
if (
file.name.startsWith(".") ||
file.name === "README.md" ||
file.name === "index.html"
) {
continue; // Ignore hidden files, README, index.html, and directories
}
const row = document.createElement("tr");
// Icon and file name
const iconCell = document.createElement("td");
const icon = document.createElement("span");
const ext = file.name.split(".").pop().toLowerCase();
icon.classList.add("material-icons", "file-icon");
icon.textContent =
file.type === "dir"
? "folder"
: fileIcons[ext] || fileIcons["default"];
const link = document.createElement("a");
link.href = `${baseUrl}${path}/${file.name}`;
link.textContent = file.name;
link.classList.add("file-name");
link.target = "_blank"; // Open in new tab for viewing
if (file.type === "dir") {
link.href = `?dir=${encodeURIComponent(path + file.name)}`;
link.onclick = (event) => {
event.preventDefault();
fetchDirectory(path + file.name);
// update the URL without reloading the page
history.pushState(null, "", `?dir=${encodeURIComponent(path + file.name)}`);
};
}
iconCell.appendChild(icon);
iconCell.appendChild(link);
// File size
const sizeCell = document.createElement("td");
// Actions (download, copy URL buttons)
const actionCell = document.createElement("td");
if (file.type != "dir") {
sizeCell.textContent = formatFileSize(file.size);
// Download button
const downloadButton = createIconButton(
"download",
"Download",
() => {
window.location.href = `${rawBaseUrl}${path}/${file.name}`;
}
);
actionCell.appendChild(downloadButton);
// Copy URL button
const copyButton = createIconButton("link", "Copy URL", () => {
copyToClipboard(`${baseUrl}${path}/${file.name}`);
});
actionCell.appendChild(copyButton);
}
row.appendChild(iconCell);
row.appendChild(sizeCell);
row.appendChild(actionCell);
tableBody.appendChild(row);
}
} catch (error) {
console.error("Error fetching directory:", error);
}
}
function formatFileSize(size) {
if (size >= 1024 * 1024) {
return (size / (1024 * 1024)).toFixed(2) + " MB";
} else if (size >= 1024) {
return (size / 1024).toFixed(2) + " KB";
} else {
return size + " bytes";
}
}
function createIconButton(iconName, tooltipText, onClick) {
const button = document.createElement("button");
button.classList.add("icon-btn");
button.onclick = onClick;
const icon = document.createElement("span");
icon.classList.add("material-icons");
icon.textContent = iconName;
const tooltip = document.createElement("span");
tooltip.classList.add("tooltip");
tooltip.textContent = tooltipText;
button.appendChild(icon);
button.appendChild(tooltip);
return button;
}
function copyToClipboard(url) {
navigator.clipboard.writeText(url).then(
() => {
alert("URL copied to clipboard!");
},
(err) => {
console.error("Error copying to clipboard: ", err);
}
);
}
const currentDir = getQueryParameter("dir");
fetchDirectory(currentDir);
</script>
</body>
</html>