HW7: task 6 is not buggy anymore
This commit is contained in:
parent
3b781b97b8
commit
57b7545f7d
1 changed files with 60 additions and 89 deletions
|
@ -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,92 +107,85 @@ 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 (!submission) {
|
||||
res.status(404).json({ error: "No image in submissions found" });
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
console.log('submission', submission.id, submission.title);
|
||||
|
||||
const images = await fetchImgur(req, res, 'GET', '/album/' +
|
||||
album.id + '/images');
|
||||
const result = {};
|
||||
result.image = submission;
|
||||
ordealResponse.views = submission.views;
|
||||
ordealResponse.votes = {};
|
||||
ordealResponse.votes.ups = submission.ups;
|
||||
ordealResponse.votes.downs = submission.downs;
|
||||
|
||||
if (!images || reject) {
|
||||
rej();
|
||||
return;
|
||||
}
|
||||
const albumIds = await fetchImgur(req, res, 'GET',
|
||||
'/account/me/albums/ids');
|
||||
|
||||
const image =
|
||||
images.data
|
||||
.filter(e => e.title.match(req.body.oldName))[0];
|
||||
if (!albumIds) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('image', image);
|
||||
console.log('albumIds', albumIds.data.join(','));
|
||||
|
||||
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
|
||||
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));
|
||||
|
||||
const image = images.data.filter(e => e.id == result.image.id)[0];
|
||||
|
||||
|
||||
if (!image || reject) {
|
||||
rej();
|
||||
return;
|
||||
}
|
||||
|
||||
reject = true;
|
||||
console.log('the album', albumId, 'image', image.id);
|
||||
res(albumId);
|
||||
}));
|
||||
}
|
||||
|
||||
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) {
|
||||
try {
|
||||
result.albumId = await promiseAny(promises);
|
||||
} catch(_) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue