Route for serving pictures
This commit is contained in:
parent
d98042d301
commit
5e7c718e39
2 changed files with 23 additions and 0 deletions
|
@ -5,12 +5,14 @@ const Cors = require('koa-cors');
|
||||||
const Mount = require('koa-mount');
|
const Mount = require('koa-mount');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
const routePictures = require('./route/pictures');
|
||||||
const route35c3 = require('./route/35c3');
|
const route35c3 = require('./route/35c3');
|
||||||
|
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
route35c3(router);
|
route35c3(router);
|
||||||
|
routePictures(router);
|
||||||
|
|
||||||
app
|
app
|
||||||
.use(Cors())
|
.use(Cors())
|
||||||
|
|
21
server/route/pictures.js
Normal file
21
server/route/pictures.js
Normal file
|
@ -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;
|
||||||
|
});
|
||||||
|
};
|
Loading…
Reference in a new issue