diff --git a/server/routes/api.js b/server/routes/api.js index 74418f5..f433a80 100644 --- a/server/routes/api.js +++ b/server/routes/api.js @@ -1,5 +1,6 @@ import express from 'express' import RateLimit from 'express-rate-limit' +import path from 'path' import multiparty from 'multiparty' import config from '../../scripts/load-config' import wrap from '../../scripts/asyncRoute' @@ -8,6 +9,8 @@ import News from '../api/news' import Image from '../api/image' import APIExtern from '../api/external' +const userContent = path.join(__dirname, '../..', 'usercontent') + let router = express.Router() let apiLimiter = new RateLimit({ @@ -326,6 +329,32 @@ router.post('/avatar/remove', wrap(async (req, res, next) => { res.status(200).jsonp({done: true}) })) +router.get('/avatar', wrap(async (req, res, next) => { + if (!req.session.user) return next() + let user = req.session.user + + if (!user.avatar_file) return next() + + res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000) // 1 week + res.redirect('/usercontent/images/' + user.avatar_file) +})) + +router.get('/avatar/:id', wrap(async (req, res, next) => { + let id = parseInt(req.params.id) + if (isNaN(id)) return next() + + let user = await API.User.get(id) + + if (!user || !user.avatar_file) return next() + + res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000) // 1 week + res.redirect('/usercontent/images/' + user.avatar_file) +})) + +router.use('/avatar', (req, res) => { + res.redirect('/static/image/avatar.png') +}) + // 404 router.use((req, res) => { res.status(404).jsonp({error: 'Not found'}) diff --git a/static/image/avatar.png b/static/image/avatar.png new file mode 100644 index 0000000..25ce30e Binary files /dev/null and b/static/image/avatar.png differ diff --git a/views/redirect.pug b/views/redirect.pug index fa7db69..1972a2e 100644 --- a/views/redirect.pug +++ b/views/redirect.pug @@ -10,6 +10,6 @@ block body p Please wait.. script. window.close() - setTimeout(() => { + setTimeout(function () { window.location.href = '#{url}' }, 1000)