From 27668e8f2e33b63c1dcad447d664d45794438323 Mon Sep 17 00:00:00 2001 From: Evert Date: Fri, 25 Aug 2017 21:54:03 +0300 Subject: [PATCH] avatar api endpoint --- server/routes/api.js | 29 +++++++++++++++++++++++++++++ static/image/avatar.png | Bin 0 -> 2995 bytes views/redirect.pug | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 static/image/avatar.png 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 0000000000000000000000000000000000000000..25ce30ecfa6975685b9f51fd12339c2110135d8c GIT binary patch literal 2995 zcmb_edpOg58`o1$W_THkCCMp-Q09;{Sx&=<92axUs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