From 5e7c718e398b84feab56f353160c300617c69b6f Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Wed, 26 Dec 2018 18:54:38 +0100 Subject: [PATCH] Route for serving pictures --- server/index.js | 2 ++ server/route/pictures.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 server/route/pictures.js 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; + }); +};