HW7: task 6 is not buggy anymore

This commit is contained in:
Claudio Maggioni 2019-11-15 09:48:32 +01:00
parent 3b781b97b8
commit 57b7545f7d

View file

@ -65,27 +65,6 @@ async function fetchImgur(req, res, method, url, body) {
return json; 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) => { router.post('/imgur/ordeal', async (req, res) => {
try { try {
const ordealResponse = { ordealSuccess: true }; const ordealResponse = { ordealSuccess: true };
@ -108,13 +87,12 @@ router.post('/imgur/ordeal', async (req, res) => {
'ids[]': imageId, 'ids[]': imageId,
}; };
let albumJson;
if (!req.body.replace) { if (!req.body.replace) {
if (req.body.album) { if (req.body.album) {
albumData.title = req.body.album; albumData.title = req.body.album;
} }
albumJson = await fetchImgur(req, res, 'POST', '/album', albumData); const albumJson = await fetchImgur(req, res, 'POST', '/album', albumData);
if (!albumJson) { if (!albumJson) {
return; return;
} }
@ -129,42 +107,60 @@ router.post('/imgur/ordeal', async (req, res) => {
} else { } else {
req.body.album = undefined; req.body.album = undefined;
const albums = await fetchImgur(req, res, 'GET', '/account/me/albums'); const submissions = await fetchImgur(req, res, 'GET',
if (!albums) { '/account/me/submissions');
if (!submissions) {
return; return;
} }
console.log('albums', albums.data.length); const submission = submissions.data
.filter(e => !e.images && e.in_gallery &&
e.title == req.body.oldName)[0];
let result; if (!submission) {
if (albums.data.length == 0) { res.status(404).json({ error: "No image in submissions found" });
result = await tryAllImages(req, res);
if (!result) {
return; return;
} }
} else {
console.log('submission', submission.id, submission.title);
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;
}
console.log('albumIds', albumIds.data.join(','));
let reject = false; let reject = false;
const promises = []; const promises = [];
for (const album of albums.data) { for (const albumId of albumIds.data) {
promises.push(new Promise(async (res, rej) => { promises.push(new Promise(async (res, rej) => {
if (reject) { if (reject) {
return reject; return reject;
} }
const images = await fetchImgur(req, res, 'GET', '/album/' + const images = await fetchImgur(req, res, 'GET', '/album/' +
album.id + '/images'); albumId + '/images');
if (!images || reject) { if (!images || reject) {
rej(); rej();
return; return;
} }
const image = console.log('album', albumId, 'images', images.data.map(e => e.id));
images.data
.filter(e => e.title.match(req.body.oldName))[0]; const image = images.data.filter(e => e.id == result.image.id)[0];
console.log('image', image);
if (!image || reject) { if (!image || reject) {
rej(); rej();
@ -172,49 +168,24 @@ router.post('/imgur/ordeal', async (req, res) => {
} }
reject = true; reject = true;
res({ album: album, image: image }); console.log('the album', albumId, 'image', image.id);
res(albumId);
})); }));
} }
try { try {
result = await promiseAny(promises); result.albumId = await promiseAny(promises);
} catch(_) { } catch(_) {
result = await tryAllImages(req, res); res.status(400).json({ error: "Image not found in any album" });
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; return;
} else {
ordealResponse.votes = ordealResponse.votes.data;
} }
console.log('votes', ordealResponse.votes);
if (!await fetchImgur(req, res, 'DELETE', '/image/' + result.image.id)) { if (!await fetchImgur(req, res, 'DELETE', '/image/' + result.image.id)) {
return; return;
} }
if (result.album && !await fetchImgur(req, res, 'PUT', '/album/' + if (!await fetchImgur(req, res, 'PUT', '/album/' +
result.album.id + '/add', { 'ids[]': uploadJson.data.id })) { result.albumId + '/add', { 'ids[]': uploadJson.data.id })) {
return; return;
} }
} }