-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
164 lines (130 loc) · 4.97 KB
/
client.js
File metadata and controls
164 lines (130 loc) · 4.97 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
const socket = io();
let localauth = localStorage.getItem("auth");
if (!localauth || localauth === 'false') {
window.location.replace("/auth-failed.htm");
}
let auth = localauth;
socket.on("auth", (data) => {
auth = data;
localStorage.setItem("auth", data); // store updated auth
});
async function uploadFile() {
const fileInput = document.getElementById('file');
const file = fileInput.files[0];
if (!file) {
alert('Please select a file.');
return;
}
const formData = new FormData();
formData.append('uploadedFile', file); // 'uploadedFile' is the field name on the server
try {
const response = await fetch('upload', { // Send to your Node.js endpoint
method: 'POST',
body: formData
});
if (response.ok) {
alert('File uploaded successfully!');
loadfiles();
} else {
alert('File upload failed.');
}
} catch (error) {
console.error('Error:', error);
alert(error)
alert('An error occurred during upload.');
}
}
addEventListener("DOMContentLoaded", function () {
// Logout button
const logoutBtn = document.getElementById('logout');
if (logoutBtn) {
logoutBtn.addEventListener('click', function () {
window.location.replace("/warning.htm");
});
}
const power = document.getElementById('power');
power.addEventListener('click', function () {
alert('shutting down fileserver (not machine)');
socket.emit("shutdown", true);
window.location.reload();
})
// Your code to execute after the DOM is fully loaded and parsed goes here
loadfiles();
});
function loadfiles() {
fetch('/files')
.then(response => response.json())
.then(files => {
const fileListElement = document.getElementById('Mylist');
// Clear existing list items
fileListElement.innerHTML = '';
files.forEach(file => {
const listItem = document.createElement('li');
const Button = document.createElement('button');
const Button1 = document.createElement('button')
// Create download link
const downloadLink = document.createElement('a');
downloadLink.href = `/uploads/${file}`;
downloadLink.textContent = file;
downloadLink.download = file; // This forces download instead of opening in browser
downloadLink.style.textDecoration = 'none';
downloadLink.style.color = 'inherit';
// Add some styling to make it look like a download button
downloadLink.addEventListener('mouseenter', function () {
this.style.textDecoration = 'underline';
});
downloadLink.addEventListener('mouseleave', function () {
this.style.textDecoration = 'none';
});
// Style and configure the download button
Button.textContent = 'Download';
Button1.textContent = 'Remove';
Button.className = 'list-item-btn';
Button1.className = 'list-item-btn';
Button.setAttribute('data-filename', file);
// Add download functionality
Button.addEventListener('click', function () {
// Create a temporary link and trigger download
const tempLink = document.createElement('a');
tempLink.href = `/uploads/${file}`;
tempLink.download = file;
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
});
Button1.addEventListener('click', function () {
socket.emit("delete", file);
loadfiles();
});
listItem.appendChild(downloadLink);
listItem.appendChild(Button);
listItem.appendChild(Button1);
fileListElement.appendChild(listItem);
});
})
.catch(error => console.error('Error fetching files:', error));
}
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: "smooth"
});
}
function scrollTopButton() {
scrollToTop();
}
function toggleBackToTopButton() {
const backToTopButton = document.getElementById('myBtn');
if (!backToTopButton) return;
if (window.pageYOffset > 100) {
backToTopButton.style.display = 'flex';
} else {
backToTopButton.style.display = 'none';
}
}
window.addEventListener('scroll', toggleBackToTopButton, { passive: true });
document.addEventListener('DOMContentLoaded', function () {
const backToTopButton = document.getElementById('myBtn');
if (backToTopButton) backToTopButton.style.display = 'none';
toggleBackToTopButton();
});