Update server.js
Browse files
server.js
CHANGED
|
@@ -8,7 +8,7 @@ const app = express();
|
|
| 8 |
const port = 7860;
|
| 9 |
|
| 10 |
app.use("/file", express.static(os.tmpdir()));
|
| 11 |
-
app.use("/", express.static(path.join(process.cwd(), 'index.html')));
|
| 12 |
|
| 13 |
const storage = multer.diskStorage({
|
| 14 |
destination: (req, file, cb) => cb(null, os.tmpdir()),
|
|
@@ -24,6 +24,205 @@ const upload = multer( {
|
|
| 24 |
}
|
| 25 |
});
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
app.post("/upload", upload.single("file"), (req, res) => {
|
| 28 |
if (!req.file) return res.status(400).json({
|
| 29 |
error: "No file uploaded"
|
|
|
|
| 8 |
const port = 7860;
|
| 9 |
|
| 10 |
app.use("/file", express.static(os.tmpdir()));
|
| 11 |
+
//app.use("/", express.static(path.join(process.cwd(), 'index.html')));
|
| 12 |
|
| 13 |
const storage = multer.diskStorage({
|
| 14 |
destination: (req, file, cb) => cb(null, os.tmpdir()),
|
|
|
|
| 24 |
}
|
| 25 |
});
|
| 26 |
|
| 27 |
+
app.all('/', async (req, res) => {
|
| 28 |
+
res.send(`<!DOCTYPE html>
|
| 29 |
+
<html lang="en">
|
| 30 |
+
<head>
|
| 31 |
+
<meta charset="UTF-8">
|
| 32 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 33 |
+
<title>File Uploader | Fast & Simple.</title>
|
| 34 |
+
<style>
|
| 35 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&display=swap');
|
| 36 |
+
|
| 37 |
+
* {
|
| 38 |
+
margin: 0;
|
| 39 |
+
padding: 0;
|
| 40 |
+
box-sizing: border-box;
|
| 41 |
+
font-family: 'Inter', sans-serif;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
body {
|
| 45 |
+
background: #000;
|
| 46 |
+
color: #fff;
|
| 47 |
+
display: flex;
|
| 48 |
+
flex-direction: column;
|
| 49 |
+
justify-content: center;
|
| 50 |
+
align-items: center;
|
| 51 |
+
height: 100vh;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
.container {
|
| 55 |
+
background: #111;
|
| 56 |
+
padding: 2rem;
|
| 57 |
+
border-radius: 12px;
|
| 58 |
+
box-shadow: 0 4px 10px rgba(255, 255, 255, 0.1);
|
| 59 |
+
text-align: center;
|
| 60 |
+
width: 320px;
|
| 61 |
+
opacity: 0;
|
| 62 |
+
transform: translateY(20px);
|
| 63 |
+
animation: fadeIn 0.5s ease-out forwards;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
@keyframes fadeIn {
|
| 67 |
+
to {
|
| 68 |
+
opacity: 1;
|
| 69 |
+
transform: translateY(0);
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
p {
|
| 74 |
+
color: #aaa;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.file-upload-container {
|
| 78 |
+
display: flex;
|
| 79 |
+
flex-direction: column;
|
| 80 |
+
align-items: center;
|
| 81 |
+
margin-top: 1rem;
|
| 82 |
+
background: #222;
|
| 83 |
+
padding: 1rem;
|
| 84 |
+
border-radius: 8px;
|
| 85 |
+
box-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
|
| 86 |
+
transition: box-shadow 0.3s ease-in-out;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
.file-upload-container:hover {
|
| 90 |
+
box-shadow: 0 0 20px rgba(255, 255, 255, 0.4);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.file-input-label {
|
| 94 |
+
display: flex;
|
| 95 |
+
justify-content: center;
|
| 96 |
+
align-items: center;
|
| 97 |
+
width: 100%;
|
| 98 |
+
padding: 12px;
|
| 99 |
+
background: #333;
|
| 100 |
+
border-radius: 6px;
|
| 101 |
+
cursor: pointer;
|
| 102 |
+
transition: background 0.3s, transform 0.2s;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
.file-input-label:hover {
|
| 106 |
+
background: #444;
|
| 107 |
+
transform: scale(1.05);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
.file-input {
|
| 111 |
+
display: none;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
.file-info {
|
| 115 |
+
margin-top: 10px;
|
| 116 |
+
color: #aaa;
|
| 117 |
+
font-size: 0.9rem;
|
| 118 |
+
text-align: left;
|
| 119 |
+
width: 100%;
|
| 120 |
+
opacity: 0;
|
| 121 |
+
transform: translateY(10px);
|
| 122 |
+
animation: fadeIn 0.5s ease-out forwards;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
.upload-status {
|
| 126 |
+
margin-top: 10px;
|
| 127 |
+
color: #aaa;
|
| 128 |
+
font-size: 0.9rem;
|
| 129 |
+
opacity: 0;
|
| 130 |
+
animation: fadeIn 0.5s ease-out forwards;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
.loading {
|
| 134 |
+
width: 24px;
|
| 135 |
+
height: 24px;
|
| 136 |
+
border: 3px solid #aaa;
|
| 137 |
+
border-top: 3px solid transparent;
|
| 138 |
+
border-radius: 50%;
|
| 139 |
+
animation: spin 1s linear infinite;
|
| 140 |
+
display: none;
|
| 141 |
+
margin: 10px auto;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
@keyframes spin {
|
| 145 |
+
to {
|
| 146 |
+
transform: rotate(360deg);
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
</style>
|
| 150 |
+
</head>
|
| 151 |
+
<body>
|
| 152 |
+
<div class="container">
|
| 153 |
+
<h1>File Uploader.</h1>
|
| 154 |
+
<p>Fast & simple.</p>
|
| 155 |
+
<div class="file-upload-container">
|
| 156 |
+
<label for="fileInput" class="file-input-label">
|
| 157 |
+
<span>Choose File.</span>
|
| 158 |
+
</label>
|
| 159 |
+
<input type="file" id="fileInput" class="file-input" onchange="handleFile()">
|
| 160 |
+
<div class="file-info" id="fileInfo">No file selected.</div>
|
| 161 |
+
<div class="loading" id="loading"></div>
|
| 162 |
+
<div class="upload-status" id="uploadStatus"></div>
|
| 163 |
+
</div>
|
| 164 |
+
</div>
|
| 165 |
+
|
| 166 |
+
<script>
|
| 167 |
+
function formatFileSize(size) {
|
| 168 |
+
const units = ["B", "KB", "MB", "GB", "TB", "PB"];
|
| 169 |
+
let index = 0;
|
| 170 |
+
while (size >= 1024 && index < units.length - 1) {
|
| 171 |
+
size /= 1024;
|
| 172 |
+
index++;
|
| 173 |
+
}
|
| 174 |
+
return size.toFixed(2) + " " + units[index] + ".";
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
function handleFile() {
|
| 178 |
+
let fileInput = document.getElementById('fileInput');
|
| 179 |
+
let fileInfo = document.getElementById('fileInfo');
|
| 180 |
+
let uploadStatus = document.getElementById('uploadStatus');
|
| 181 |
+
let loading = document.getElementById('loading');
|
| 182 |
+
let file = fileInput.files[0];
|
| 183 |
+
|
| 184 |
+
if (file) {
|
| 185 |
+
let fileSize = formatFileSize(file.size);
|
| 186 |
+
let lastModified = new Date(file.lastModified).toLocaleString() + ".";
|
| 187 |
+
|
| 188 |
+
fileInfo.innerHTML = \`
|
| 189 |
+
<strong>Name:</strong> \${file.name}.<br>
|
| 190 |
+
<strong>Size:</strong> \${fileSize}<br>
|
| 191 |
+
<strong>Last Modified:</strong> \${lastModified}
|
| 192 |
+
\`;
|
| 193 |
+
|
| 194 |
+
uploadFile(file, uploadStatus, loading);
|
| 195 |
+
} else {
|
| 196 |
+
fileInfo.innerHTML = "No file selected.";
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
function uploadFile(file, uploadStatus, loading) {
|
| 201 |
+
let formData = new FormData();
|
| 202 |
+
formData.append("file", file);
|
| 203 |
+
|
| 204 |
+
uploadStatus.innerHTML = "";
|
| 205 |
+
loading.style.display = "block";
|
| 206 |
+
|
| 207 |
+
fetch("/upload", {
|
| 208 |
+
method: "POST",
|
| 209 |
+
body: formData
|
| 210 |
+
})
|
| 211 |
+
.then(response => response.json())
|
| 212 |
+
.then(data => {
|
| 213 |
+
loading.style.display = "none";
|
| 214 |
+
uploadStatus.innerHTML = \`Uploaded: <a href="\${data.url}" target="_blank">\${data.url}</a>.\`;
|
| 215 |
+
})
|
| 216 |
+
.catch(error => {
|
| 217 |
+
loading.style.display = "none";
|
| 218 |
+
uploadStatus.innerHTML = "Upload failed.";
|
| 219 |
+
});
|
| 220 |
+
}
|
| 221 |
+
</script>
|
| 222 |
+
</body>
|
| 223 |
+
</html>`)
|
| 224 |
+
})
|
| 225 |
+
|
| 226 |
app.post("/upload", upload.single("file"), (req, res) => {
|
| 227 |
if (!req.file) return res.status(400).json({
|
| 228 |
error: "No file uploaded"
|