avatar api endpoint

This commit is contained in:
Evert Prants 2017-08-25 21:54:03 +03:00
parent 69af1d2ae1
commit 27668e8f2e
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 30 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import express from 'express' import express from 'express'
import RateLimit from 'express-rate-limit' import RateLimit from 'express-rate-limit'
import path from 'path'
import multiparty from 'multiparty' import multiparty from 'multiparty'
import config from '../../scripts/load-config' import config from '../../scripts/load-config'
import wrap from '../../scripts/asyncRoute' import wrap from '../../scripts/asyncRoute'
@ -8,6 +9,8 @@ import News from '../api/news'
import Image from '../api/image' import Image from '../api/image'
import APIExtern from '../api/external' import APIExtern from '../api/external'
const userContent = path.join(__dirname, '../..', 'usercontent')
let router = express.Router() let router = express.Router()
let apiLimiter = new RateLimit({ let apiLimiter = new RateLimit({
@ -326,6 +329,32 @@ router.post('/avatar/remove', wrap(async (req, res, next) => {
res.status(200).jsonp({done: true}) 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 // 404
router.use((req, res) => { router.use((req, res) => {
res.status(404).jsonp({error: 'Not found'}) res.status(404).jsonp({error: 'Not found'})

BIN
static/image/avatar.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -10,6 +10,6 @@ block body
p Please wait.. p Please wait..
script. script.
window.close() window.close()
setTimeout(() => { setTimeout(function () {
window.location.href = '#{url}' window.location.href = '#{url}'
}, 1000) }, 1000)