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