this is it, this is the one

This commit is contained in:
NotAShelf 2022-01-24 17:53:05 +03:00
parent cc9f6beec8
commit 07aa9b1242
No known key found for this signature in database
GPG key ID: 2E4615E792DA9BAA

95
index.js Normal file
View file

@ -0,0 +1,95 @@
const express = require('express');
const app = express();
const fs = require('fs');
require('dotenv').config();
const port = process.env.PORT;
app.listen(port, () => {
console.log('App listening at http://localhost:' + port);
});
app.get('/', (req, res) => {
res.sendFile(getRandomImagePath(), (err) => {
if (err) {
res.status(err.status).end();
}
});
});
app.get('/:id', (req, res) => {
const image = getImageById(req.params.id);
if (image) {
res.sendFile(image, (err) => {
if (err) {
res.status(err.status).end();
}
});
} else {
return res.json({
error: 'Image not found'
});
}
});
app.get('/api/listimgs', (req, res) => {
return res.json(getAllImageIds());
});
app.get('/api/randomimg', (req, res) => {
return res.json(getRandomImageApi());
});
app.get('/api/:id', (req, res) => {
const image = getImageById(req.params.id, false);
if (image) {
return res.json({
id: parseInt(req.params.id),
url: 'https://cat.frozendev.tk' + req.params.id,
});
} else {
return res.json({
error: 'Image not found'
});
}
});
const getRandomImagePath = () => {
const images = fs.readdirSync('./images');
return __dirname + '/images/' + images[Math.floor(Math.random() * images.length)];
};
const getImageById = (id, path = true) => {
const images = fs.readdirSync('./images');
for (const image of images) {
if (image.substring(0, image.length - 4).split('-')[1] === id) {
if (path) {
return __dirname + '/images/' + image;
} else {
return image;
}
}
}
return null;
};
const getAllImageIds = () => {
const ids = [];
fs.readdirSync('./images').forEach(image => {
ids.push(parseInt(image.substring(0, image.length - 4) .split('-')[1]));
});
return ids.sort((a, b) => a - b);
};
const getRandomImageApi = () => {
const path = getRandomImagePath();
const id = path.substring(0, path.length - 4).split('-')[1];
return {
id: parseInt(id),
url: 'https://cat.frozendev.tk' + id,
};
};