/** @module root/router */ 'use strict'; // vim: set ts=2 sw=2 et tw=80: const express = require('express'); const router = express.Router(); const mongoose = require('mongoose'); const Favorite = mongoose.model('Favorite'); const fetch = require('node-fetch'); const querystring = require('querystring'); const promiseAny = require('promise-any'); const { error } = require('../utils'); router.get('/', (req, res) => { Favorite.find({}, (err, favs) => { if (err) { return error(err, res); } res.render('index.dust', { favs }); }); }); router.get('/imgur', (req, res) => { if (req.query.access_token) { req.app.locals.imgur = req.query; res.redirect('/'); } else { res.status(200, {'Content-Type': 'text/html'}); res.end(` `); } }); async function fetchImgur(req, res, method, url, body) { const endpoint = 'https://api.imgur.com/3'; const response = await fetch(endpoint + url, { method: method, headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization' : 'Bearer ' + req.app.locals.imgur.access_token }, body: body ? querystring.stringify(body) : undefined }); const json = await response.json(); if (json.status == 409 && url.match(/follow\/tag/)) { return {}; } if (Math.floor(json.status / 100) != 2) { res.status(json.status).json(json); return false; } return json; } async function tryAllImages(req, res) { const images = await fetchImgur(req, res, 'GET', '/account/me/images'); if (!images) { return false; } console.log('all images list', images.data); const image = images.data .filter(e => e.title.match(req.body.oldName) && e.in_gallery)[0]; console.log('all images', image); if (image) { return { album: null, image: image }; } else { res.status(404).json({ error: "No image in gallery found" }); return false; } } router.post('/imgur/ordeal', async (req, res) => { try { const ordealResponse = { ordealSuccess: true }; const uploadData = { image: req.body.dataURL.substring('data:image/png;base64,'.length), type: 'base64', title: req.body.name, name: req.body.name + '.png', }; const uploadJson = await fetchImgur(req, res, 'POST', '/upload', uploadData); if (!uploadJson) { return; } const imageId = uploadJson.data.id; const albumData = { 'ids[]': imageId, }; let albumJson; if (!req.body.replace) { if (req.body.album) { albumData.title = req.body.album; } albumJson = await fetchImgur(req, res, 'POST', '/album', albumData); if (!albumJson) { return; } console.log(req.body.favorites); if (req.body.favorites) { if (!await fetchImgur(req, res, 'POST', '/album/' + albumJson.data.id + '/favorite', {})) { return; } } } else { req.body.album = undefined; const albums = await fetchImgur(req, res, 'GET', '/account/me/albums'); if (!albums) { return; } console.log('albums', albums.data.length); let result; if (albums.data.length == 0) { result = await tryAllImages(req, res); if (!result) { return; } } else { let reject = false; const promises = []; for (const album of albums.data) { promises.push(new Promise(async (res, rej) => { if (reject) { return reject; } const images = await fetchImgur(req, res, 'GET', '/album/' + album.id + '/images'); if (!images || reject) { rej(); return; } const image = images.data .filter(e => e.title.match(req.body.oldName))[0]; console.log('image', image); if (!image || reject) { rej(); return; } reject = true; res({ album: album, image: image }); })); } try { result = await promiseAny(promises); } catch(_) { result = await tryAllImages(req, res); if (!result) { return } } } console.log('result', result); albumJson = { data: result.album }; ordealResponse.views = result.image.views; if (result.image.in_gallery) { ordealResponse.votes = await fetchImgur(req, res, 'GET', '/gallery/' + result.image.id + '/votes'); } else if (result.album.in_gallery) { ordealResponse.votes = await fetchImgur(req, res, 'GET', '/gallery/' + result.album.id + '/votes'); } else { ordealResponse.votes = { ups: 'not avaliable', downs: 'not avaliable' }; } if (!ordealResponse.votes) { return; } else { ordealResponse.votes = ordealResponse.votes.data; } console.log('votes', ordealResponse.votes); if (!await fetchImgur(req, res, 'DELETE', '/image/' + result.image.id)) { return; } if (result.album && !await fetchImgur(req, res, 'PUT', '/album/' + result.album.id + '/add', { 'ids[]': uploadJson.data.id })) { return; } } if (!await fetchImgur(req, res, 'POST', '/gallery/image/' + uploadJson.data.id, { title: req.body.name, tags: req.body.tags })) { return; } if (req.body.tags) { const tagsData = { 'tags': req.body.tags }; if (!await fetchImgur(req, res, 'POST', '/gallery/tags/' + uploadJson.data.id, tagsData)) { return; } for (const tag of req.body.tags.split(',')) { if (!await fetchImgur(req, res, 'POST', '/account/me/follow/tag/' + tag, {})) { return; } } } res.json(ordealResponse); } catch(e) { console.error(e); res.status(500).json(e); } }); /** router for /root */ module.exports = router;