Random image API
This commit is contained in:
parent
97d5ad79f5
commit
0f7a7b44b1
3 changed files with 26 additions and 3 deletions
27
index.js
27
index.js
|
@ -23,6 +23,29 @@ app.listen(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) => {
|
||||
var file = req.params[0]
|
||||
var absPath = path.join(directory, file)
|
||||
|
@ -32,11 +55,11 @@ app.get("/*", (req, res) => {
|
|||
try {
|
||||
var dirContents = fs.readdirSync(absPath)
|
||||
} 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
|
||||
}
|
||||
|
||||
res.write(baseStart)
|
||||
res.write(baseStart.replace("{TITLE}", '/' + file))
|
||||
|
||||
if (file != '') {
|
||||
res.write('<a href="../">Parent Directory</a><br>')
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
<title>{TITLE}</title>
|
||||
</head>
|
||||
<body>
|
||||
{CONTENT}
|
||||
|
|
0
test.png
Normal file
0
test.png
Normal file
Loading…
Reference in a new issue