diff --git a/server/index.js b/server/index.js index cc06254..4110091 100644 --- a/server/index.js +++ b/server/index.js @@ -5,12 +5,14 @@ const Cors = require('koa-cors'); const Mount = require('koa-mount'); const path = require('path'); +const routePictures = require('./route/pictures'); const route35c3 = require('./route/35c3'); const app = new Koa(); const router = new Router(); route35c3(router); +routePictures(router); app .use(Cors()) diff --git a/server/route/pictures.js b/server/route/pictures.js new file mode 100644 index 0000000..126943d --- /dev/null +++ b/server/route/pictures.js @@ -0,0 +1,21 @@ +const path = require('path'); +const fs = require('fs'); +const { promisify } = require('util'); + +const fsStat = promisify(fs.stat); +const fsReadDir = promisify(fs.readdir); + +module.exports = (router) => { + router.get('/pictures/:folder', async (ctx) => { + const folder = path.resolve(__dirname, `../../pictures/${path.basename(ctx.params.folder)}`); + try { + await fsStat(folder); + } catch (e) { + ctx.status = 404; + return ctx; + } + const files = await fsReadDir(folder); + ctx.body = files.map(f => `/pictures/${path.basename(ctx.params.folder)}/${f}`); + return ctx; + }); +};