This repository has been archived on 2024-08-06. You can view files and clone it, but cannot push or open issues or pull requests.
readyornot/pages/api/[map]/maps.tsx

23 lines
889 B
TypeScript
Raw Permalink Normal View History

2022-12-03 20:02:18 +00:00
import fsPromises from 'fs/promises'
import path from 'path'
export default async function MapApi(req: any, res: any) {
const { map } = req.query
try {
// get list of all files(maps) in the readyornot folder - maybe there is a cleaner way to do this?
// we filter out any subdirectories as well as any files not ending in png or jpg
var fs = require('fs')
var maps = fs.readdirSync(path.join(process.cwd(), "/public/images/" + map + "/"), { withFileTypes: true })
.filter((file: any) => file.isFile())
.filter((file: any) => file.name.endsWith(".jpg") || file.name.endsWith(".png"))
.map((file: any) => '/images/' + map + '/' + file.name);
// get Empire and Game name first to create an EmpireData object
res.status(200).json(maps)
}
catch (error) {
console.log(error)
res.status(500).json({ error: 'Error reading data' })
}
}