fix avatar api; fix edit link api

This commit is contained in:
mntmn 2018-04-14 22:27:38 +02:00
parent 30d2995565
commit 54755621a1
3 changed files with 27 additions and 69 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
node_modules node_modules
javascripts/maps javascripts/maps
javascripts/spacedeck.js javascripts/spacedeck.js
*.swp
*~

View File

@ -3,6 +3,7 @@
var config = require('config'); var config = require('config');
const db = require('../../models/db'); const db = require('../../models/db');
const uuidv4 = require('uuid/v4'); const uuidv4 = require('uuid/v4');
const os = require('os');
var mailer = require('../../helpers/mailer'); var mailer = require('../../helpers/mailer');
var uploader = require('../../helpers/uploader'); var uploader = require('../../helpers/uploader');
@ -215,8 +216,8 @@ router.post('/:user_id/avatar', (req, res, next) => {
const user = req.user; const user = req.user;
const filename = "u"+req.user._id+"_"+(new Date().getTime())+".jpeg" const filename = "u"+req.user._id+"_"+(new Date().getTime())+".jpeg"
const localFilePath = "/tmp/"+filename; const localFilePath = os.tmpdir()+"/"+filename;
const localResizedFilePath = "/tmp/resized_"+filename; const localResizedFilePath = os.tmpdir()+"/resized_"+filename;
const writeStream = fs.createWriteStream(localFilePath); const writeStream = fs.createWriteStream(localFilePath);
const stream = req.pipe(writeStream); const stream = req.pipe(writeStream);

View File

@ -1,7 +1,6 @@
"use strict"; "use strict";
const config = require('config'); const config = require('config');
require('../models/db');
const redis = require('../helpers/redis'); const redis = require('../helpers/redis');
const express = require('express'); const express = require('express');
@ -10,6 +9,11 @@ const router = express.Router();
const mailer = require('../helpers/mailer'); const mailer = require('../helpers/mailer');
const _ = require('underscore'); const _ = require('underscore');
const db = require('../models/db');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const uuidv4 = require('uuid/v4');
router.get('/', (req, res) => { router.get('/', (req, res) => {
res.render('index', { title: 'Spaces' }); res.render('index', { title: 'Spaces' });
}); });
@ -120,79 +124,30 @@ router.get('/t/:id', (req, res) => {
}); });
router.get('/s/:token', (req, res) => { router.get('/s/:token', (req, res) => {
redis.rateLimit(req.real_ip, "token", function(ok) { var token = req.params.token;
if (ok) { if (token.split("-").length > 0) {
var token = req.params.token; token = token.split("-")[0];
if (token.split("-").length > 0) { }
token = token.split("-")[0];
}
Space.findOne({"edit_hash": token}).exec(function (err, space) { db.Space.findOne({where: {"edit_hash": token}}).then(function (space) {
if (err) { if (space) {
res.status(404).render('not_found', { title: 'Page Not Found.' }); if (req.accepts('text/html')){
} else { res.redirect("/spaces/"+space._id + "?spaceAuth=" + token);
if (space) { } else {
if(req.accepts('text/html')){ res.status(200).json(space);
res.redirect("/spaces/"+space._id + "?spaceAuth=" + token); }
}else{
res.status(200).json(space);
}
} else {
if(req.accepts('text/html')){
res.status(404).render('not_found', { title: 'Page Not Found.' });
} else {
res.status(404).json({});
}
}
}
});
} else { } else {
res.status(429).json({"error": "Too Many Requests"}); if (req.accepts('text/html')) {
res.status(404).render('not_found', { title: 'Page Not Found.' });
} else {
res.status(404).json({});
}
} }
}); });
}); });
router.get('/spaces/:id', (req, res) => { router.get('/spaces/:id', (req, res) => {
if (req.headers['user-agent']) { res.render('spacedeck', { title: 'Space' });
if (req.headers['user-agent'].match(/facebook/)) {
Space.findOne({"_id": req.params.id }).exec(function (err, space) {
if (err) {
res.status(400).json(err);
} else {
if (space) {
if (space.access_mode == "public") {
Artifact.find({"space_id": req.params.id }).populate("creator").exec(function(err, artifacts) {
space.artifacts = artifacts;
res.render('facebook', { space: space });
});
} else {
res.redirect("/?error=space_not_accessible");
}
} else {
res.render('not_found', { title: 'Spaces' });
}
}
});
} else {
// not facebook, render javascript
res.render('spacedeck', { title: 'Space' });
}
} else res.render('spacedeck', { title: 'Space' });
});
router.get('/qrcode/:id', function(req, res) {
Space.findOne({"_id": req.params.id}).exec(function(err, space) {
if (space) {
const url = config.get("endpoint") + "/s/"+space.edit_hash;
const code = qr.image(url, { type: 'svg' });
res.type('svg');
code.pipe(res);
} else {
res.status(404).json({
"error": "not_found"
});
}
});
}); });
module.exports = router; module.exports = router;