2019-11-11 15:58:06 +00:00
|
|
|
/** @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');
|
2019-11-14 09:55:43 +00:00
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const querystring = require('querystring');
|
2019-11-14 14:27:51 +00:00
|
|
|
const promiseAny = require('promise-any');
|
2019-11-11 15:58:06 +00:00
|
|
|
|
|
|
|
const { error } = require('../utils');
|
|
|
|
|
|
|
|
router.get('/', (req, res) => {
|
|
|
|
Favorite.find({}, (err, favs) => {
|
|
|
|
if (err) {
|
|
|
|
return error(err, res);
|
|
|
|
}
|
|
|
|
res.render('index.dust', { favs });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-14 09:55:43 +00:00
|
|
|
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(`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>
|
|
|
|
<script>
|
|
|
|
console.log(window.location.href);
|
|
|
|
if (window.location.href.indexOf("#access") != -1) {
|
|
|
|
window.location.href = window.location.href.replace("#access",
|
|
|
|
"?access");
|
|
|
|
}
|
|
|
|
</script></body></html>`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async function fetchImgur(req, res, method, url, body) {
|
|
|
|
const endpoint = 'https://api.imgur.com/3';
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
const response = await fetch(endpoint + url, {
|
2019-11-14 09:55:43 +00:00
|
|
|
method: method,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'Authorization' : 'Bearer ' + req.app.locals.imgur.access_token
|
|
|
|
},
|
2019-11-14 14:27:51 +00:00
|
|
|
body: body ? querystring.stringify(body) : undefined
|
2019-11-14 09:55:43 +00:00
|
|
|
});
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
const json = await response.json();
|
2019-11-14 09:55:43 +00:00
|
|
|
|
2019-11-15 07:55:37 +00:00
|
|
|
if (json.status == 409 && url.match(/follow\/tag/)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
if (Math.floor(json.status / 100) != 2) {
|
|
|
|
res.status(json.status).json(json);
|
2019-11-14 09:55:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
return json;
|
2019-11-14 09:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router.post('/imgur/ordeal', async (req, res) => {
|
|
|
|
try {
|
2019-11-15 09:05:03 +00:00
|
|
|
if ((!req.body.replace && (!req.body.name || !req.body.album)) ||
|
|
|
|
(req.body.replace && (!req.body.name || !req.body.oldName))) {
|
|
|
|
res.status(400).json({ error: "Malformed ordeal request" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
const ordealResponse = { ordealSuccess: true };
|
|
|
|
|
2019-11-14 09:55:43 +00:00
|
|
|
const uploadData = {
|
|
|
|
image: req.body.dataURL.substring('data:image/png;base64,'.length),
|
|
|
|
type: 'base64',
|
2019-11-14 15:08:52 +00:00
|
|
|
title: req.body.name,
|
2019-11-14 09:55:43 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
if (!req.body.replace) {
|
|
|
|
if (req.body.album) {
|
|
|
|
albumData.title = req.body.album;
|
|
|
|
}
|
2019-11-14 09:55:43 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
const albumJson = await fetchImgur(req, res, 'POST', '/album', albumData);
|
2019-11-14 14:27:51 +00:00
|
|
|
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;
|
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
const submissions = await fetchImgur(req, res, 'GET',
|
|
|
|
'/account/me/submissions');
|
|
|
|
|
|
|
|
if (!submissions) {
|
2019-11-14 14:27:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
const submission = submissions.data
|
|
|
|
.filter(e => !e.images && e.in_gallery &&
|
|
|
|
e.title == req.body.oldName)[0];
|
2019-11-14 14:27:51 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
if (!submission) {
|
|
|
|
res.status(404).json({ error: "No image in submissions found" });
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 15:08:52 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
console.log('submission', submission.id, submission.title);
|
2019-11-14 14:27:51 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
const result = {};
|
|
|
|
result.image = submission;
|
|
|
|
ordealResponse.views = submission.views;
|
|
|
|
ordealResponse.votes = {};
|
|
|
|
ordealResponse.votes.ups = submission.ups;
|
|
|
|
ordealResponse.votes.downs = submission.downs;
|
|
|
|
|
|
|
|
const albumIds = await fetchImgur(req, res, 'GET',
|
|
|
|
'/account/me/albums/ids');
|
|
|
|
|
|
|
|
if (!albumIds) {
|
|
|
|
return;
|
2019-11-14 14:27:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
console.log('albumIds', albumIds.data.join(','));
|
|
|
|
|
|
|
|
let reject = false;
|
|
|
|
const promises = [];
|
|
|
|
for (const albumId of albumIds.data) {
|
|
|
|
promises.push(new Promise(async (res, rej) => {
|
|
|
|
if (reject) {
|
|
|
|
return reject;
|
|
|
|
}
|
|
|
|
|
|
|
|
const images = await fetchImgur(req, res, 'GET', '/album/' +
|
|
|
|
albumId + '/images');
|
|
|
|
|
|
|
|
if (!images || reject) {
|
|
|
|
rej();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('album', albumId, 'images', images.data.map(e => e.id));
|
2019-11-14 14:27:51 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
const image = images.data.filter(e => e.id == result.image.id)[0];
|
2019-11-15 07:55:37 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
|
|
|
|
if (!image || reject) {
|
|
|
|
rej();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reject = true;
|
|
|
|
console.log('the album', albumId, 'image', image.id);
|
|
|
|
res(albumId);
|
|
|
|
}));
|
2019-11-15 07:55:37 +00:00
|
|
|
}
|
2019-11-14 15:08:52 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
try {
|
|
|
|
result.albumId = await promiseAny(promises);
|
|
|
|
} catch(_) {
|
|
|
|
res.status(400).json({ error: "Image not found in any album" });
|
2019-11-14 15:08:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
if (!await fetchImgur(req, res, 'DELETE', '/image/' + result.image.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 09:55:43 +00:00
|
|
|
|
2019-11-15 08:48:32 +00:00
|
|
|
if (!await fetchImgur(req, res, 'PUT', '/album/' +
|
|
|
|
result.albumId + '/add', { 'ids[]': uploadJson.data.id })) {
|
2019-11-14 09:55:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 15:26:10 +00:00
|
|
|
if (!await fetchImgur(req, res, 'POST', '/gallery/image/' +
|
2019-11-15 07:55:37 +00:00
|
|
|
uploadJson.data.id, { title: req.body.name, tags: req.body.tags })) {
|
2019-11-14 09:55:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body.tags) {
|
|
|
|
const tagsData = {
|
|
|
|
'tags': req.body.tags
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!await fetchImgur(req, res, 'POST', '/gallery/tags/' +
|
2019-11-14 15:26:10 +00:00
|
|
|
uploadJson.data.id, tagsData)) {
|
2019-11-14 09:55:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const tag of req.body.tags.split(',')) {
|
|
|
|
if (!await fetchImgur(req, res, 'POST', '/account/me/follow/tag/' + tag,
|
|
|
|
{})) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:27:51 +00:00
|
|
|
res.json(ordealResponse);
|
2019-11-14 09:55:43 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
res.status(500).json(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-11 15:58:06 +00:00
|
|
|
/** router for /root */
|
|
|
|
module.exports = router;
|