Random image API

This commit is contained in:
bingus_violet 2024-01-19 19:25:26 +00:00
parent 97d5ad79f5
commit 0f7a7b44b1
3 changed files with 26 additions and 3 deletions

View file

@ -23,6 +23,29 @@ app.listen(PORT, () => {
console.log("Now listening on PORT: " + PORT) console.log("Now listening on PORT: " + PORT)
}) })
app.get("/api/v1/randomIMG*", (req, res) => {
var folder = req.params[0]
var dirContent = fs.readdirSync(path.join(directory, folder))
var files = []
var imageFormats = [".png", ".jpg", ".jpeg", ".webp"]
for (let index = 0; index < dirContent.length; index++) {
const file = dirContent[index];
for (let index = 0; index < imageFormats.length; index++) {
const extension = imageFormats[index];
if (file.includes(extension)) {
files.push(file)
break
}
}
}
if (files.length > 0) {
res.redirect(path.join('/', folder, files[Math.floor(Math.random() * files.length)]))
} else {
res.send("No Images in this directory")
}
})
app.get("/*", (req, res) => { app.get("/*", (req, res) => {
var file = req.params[0] var file = req.params[0]
var absPath = path.join(directory, file) var absPath = path.join(directory, file)
@ -32,11 +55,11 @@ app.get("/*", (req, res) => {
try { try {
var dirContents = fs.readdirSync(absPath) var dirContents = fs.readdirSync(absPath)
} catch (error) { } catch (error) {
res.send(baseStart + "<h2>404: not found!</h2>" + baseEnd) res.send(baseStart.replace("{TITLE}", "404: not found!") + "<h2>404: not found!</h2>" + baseEnd)
return return
} }
res.write(baseStart) res.write(baseStart.replace("{TITLE}", '/' + file))
if (file != '') { if (file != '') {
res.write('<a href="../">Parent Directory</a><br>') res.write('<a href="../">Parent Directory</a><br>')

View file

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>{TITLE}</title>
</head> </head>
<body> <body>
{CONTENT} {CONTENT}

0
test.png Normal file
View file