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