opt-cc/api/routes/signatures.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-05-10 11:04:06 +02:00
"use strict";
// modules
const fs = require('fs');
const express = require('express');
const logger = require('debug')('cc:signatures');
// HTTP status codes by name
const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
const UserModel = require('../models/user');
const signatures = express.Router();
// routes **********************
signatures.route('/:id')
.get((req, res, next) => {
// decode UTF8-escape sequences (special characters)
const uri = decodeURIComponent(req.params.id);
UserModel.findOne({username: uri}, (err, user) => {
const emptyFile = 'resource/signature/0.png';
if (user === null) {
res.sendFile(emptyFile, {root: __dirname + '/../'});
} else {
const file = 'resource/signature/' + user._id + '.png';
fs.stat(__dirname + '/../' + file, (err, stat) => {
if (err === null) {
res.sendFile(file, {root: __dirname + '/../'});
} else if (err.code === 'ENOENT') {
res.sendFile(emptyFile, {root: __dirname + '/../'});
} else {
err = new Error("Internal server error");
err.status = codes.servererror;
return next(err);
}
});
}
})
})
.all(
routerHandling.httpMethodNotAllowed
);
// this middleware function can be used, if you like or remove it
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
signatures.use(routerHandling.emptyResponse);
module.exports = signatures;