funny init

This commit is contained in:
Red Duck 2023-03-14 21:57:14 +02:00
commit b7d9b4f104
3 changed files with 100 additions and 0 deletions

6
.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
yarn.lock
node_modules
package-lock.json
yarn-error.log
uploads
*.env

82
index.js Normal file
View file

@ -0,0 +1,82 @@
import express from 'express';
import fs from 'fs';
import multer from "multer";
import path from 'path';
import crypto from 'crypto';
import blueutilities from 'blueutilities';
const __dirname = path.resolve();
const app = express();
app.use(multer().single("file"))
// body-parser
// app.use(express.raw({limit: '100mb',type: 'application/octet-stream'}));
// app.use(express.urlencoded({ extended: false }));
// app.use(express.urlencoded({extended: false, limit: '5mb'}));
if(!fs.existsSync('uploads')) {
fs.mkdirSync('uploads');
}
app.get("/", (req, res) => {
res.send("helo")
})
app.get('/:name', (req, res) => {
if(req.headers['user-agent'].toLowerCase().includes("discord")) {
res.header("facts", "tencent is crab").redirect("/i/" + req.params.name); // discord trolling
// (redirects discord to the full image url)
}
if(blueutilities.random.numBetween(0, 100) == 68) {
res.send("Among");
}
res.send(`
<script>
const red = {
crab() {
console.log("E")
},
isCrab() {
return Math.floor(Math.random() * 10) != 5
} // crabs a random amount of time
}
while(red.isCrab()) { // y e s
red.crab();
}
</script>
`);
});
app.post('/upload', (req, res) => {
// save file with a randomized name and send back the name to the client
// so that the client can use the name to retrieve the file
let fileName;
while(!fileName || fs.existsSync(path.join(__dirname, 'uploads', fileName))) {
fileName = crypto.randomBytes(16).toString("hex") + ".png";
}
const filePath = path.join(__dirname, 'uploads', fileName);
const fileData = req.file.buffer; // try now
fs.writeFile(filePath, fileData, (err) => {
if (err) {
console.log(err);
res.status(500).send('error');
return;
}
res.send({
success: true,
fileName: fileName,
fullURL: `${req.protocol}://${req.headers.host}/${fileName}`
});
});
});
app.use('i', express.static(path.join(__dirname, "uploads")))
app.use((req, res, next) => {
res.status(418).send("418 crab not found");
});
app.listen(3000, () => {
console.log('stupid uploader is run');
});

12
package.json Normal file
View file

@ -0,0 +1,12 @@
{
"dependencies": {
"blueutilities": "^1.2.2",
"express": "^4.18.2",
"multer": "^1.4.5-lts.1"
},
"name": "image_uploader",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type": "module"
}