File size: 6,698 Bytes
3a83a07 bba2e6a 3a83a07 bba2e6a a1ce60e bba2e6a a1ce60e bba2e6a a1ce60e bba2e6a 3a83a07 |
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 |
const express = require("express");
const multer = require("multer");
const path = require("path");
const os = require("os");
const crypto = require("crypto");
const app = express();
const port = 7860;
app.use("/file", express.static(os.tmpdir()));
//app.use("/", express.static(path.join(process.cwd(), 'index.html')));
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, os.tmpdir()),
filename: (req, file, cb) => {
const randomName = crypto.randomBytes(4).toString("base64url");
cb(null, `${randomName}${path.extname(file.originalname)}`);
},
});
const upload = multer( {
storage, limits: {
fileSize: 10 * 1024 * 1024
}
});
app.all('/', async (req, res) => {
res.send(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Uploader | Fast & Simple.</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Inter', sans-serif;
}
body {
background: #000;
color: #fff;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: #111;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 4px 10px rgba(255, 255, 255, 0.1);
text-align: center;
width: 320px;
opacity: 0;
transform: translateY(20px);
animation: fadeIn 0.5s ease-out forwards;
}
@keyframes fadeIn {
to {
opacity: 1;
transform: translateY(0);
}
}
p {
color: #aaa;
}
a {
color: #aaa;
}
.file-upload-container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 1rem;
background: #222;
padding: 1rem;
border-radius: 8px;
box-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
transition: box-shadow 0.3s ease-in-out;
}
.file-upload-container:hover {
box-shadow: 0 0 20px rgba(255, 255, 255, 0.4);
}
.file-input-label {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
padding: 12px;
background: #333;
border-radius: 6px;
cursor: pointer;
transition: background 0.3s, transform 0.2s;
}
.file-input-label:hover {
background: #444;
transform: scale(1.05);
}
.file-input {
display: none;
}
.file-info {
margin-top: 10px;
color: #aaa;
font-size: 0.9rem;
text-align: left;
width: 100%;
opacity: 0;
transform: translateY(10px);
animation: fadeIn 0.5s ease-out forwards;
}
.upload-status {
margin-top: 10px;
color: #aaa;
font-size: 0.9rem;
opacity: 0;
animation: fadeIn 0.5s ease-out forwards;
}
.loading {
width: 24px;
height: 24px;
border: 3px solid #aaa;
border-top: 3px solid transparent;
border-radius: 50%;
animation: spin 1s linear infinite;
display: none;
margin: 10px auto;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="container">
<h1>File Uploader.</h1>
<p>Fast & simple.</p>
<div class="file-upload-container">
<label for="fileInput" class="file-input-label">
<span>Choose File.</span>
</label>
<input type="file" id="fileInput" class="file-input" onchange="handleFile()">
<div class="file-info" id="fileInfo">No file selected.</div>
<div class="loading" id="loading"></div>
<div class="upload-status" id="uploadStatus"></div>
</div>
</div>
<script>
function formatFileSize(size) {
const units = ["B", "KB", "MB", "GB", "TB", "PB"];
let index = 0;
while (size >= 1024 && index < units.length - 1) {
size /= 1024;
index++;
}
return size.toFixed(2) + " " + units[index] + ".";
}
function handleFile() {
let fileInput = document.getElementById('fileInput');
let fileInfo = document.getElementById('fileInfo');
let uploadStatus = document.getElementById('uploadStatus');
let loading = document.getElementById('loading');
let file = fileInput.files[0];
if (file) {
let fileSize = formatFileSize(file.size);
fileInfo.innerHTML = \`
<strong>Name:</strong> \${file.name}.<br>
<strong>Size:</strong> \${fileSize}<br>
\`;
uploadFile(file, uploadStatus, loading);
} else {
fileInfo.innerHTML = "No file selected.";
}
}
function uploadFile(file, uploadStatus, loading) {
let formData = new FormData();
formData.append("file", file);
uploadStatus.innerHTML = "";
loading.style.display = "block";
fetch("/upload", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
loading.style.display = "none";
uploadStatus.innerHTML = \`<a href="\${data.url}" target="_blank">\${data.url}</a>.\`;
})
.catch(error => {
loading.style.display = "none";
uploadStatus.innerHTML = "Upload failed.";
});
}
</script>
</body>
</html>`)
})
app.post("/upload", upload.single("file"), (req, res) => {
if (!req.file) return res.status(400).json({
error: "No file uploaded"
});
res.json({
url: `https://${req.hostname}/file/${req.file.filename}`
});
});
app.listen(port, () => console.log(`Server running on port ${port}`)); |