From 32870148cf3cf0ccc92a457d07fd313d879e244d Mon Sep 17 00:00:00 2001 From: Dmitry Sumin Date: Mon, 6 Mar 2017 19:50:23 +0300 Subject: [PATCH] Attachment image size fallback for posts-opengraph API --- app/controllers/api/v2/PostsController.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v2/PostsController.js b/app/controllers/api/v2/PostsController.js index 7dd4b65b..7511b0b5 100644 --- a/app/controllers/api/v2/PostsController.js +++ b/app/controllers/api/v2/PostsController.js @@ -123,9 +123,22 @@ export default class PostsController { if (attachments.length > 0) { for (const item of attachments.map(serializeAttachment)) { if (item.mediaType === 'image') { - image = item.imageSizes[`t2`].url; - image_h = item.imageSizes[`t2`].h; - image_w = item.imageSizes[`t2`].w; + let image_size; + + // Image fallback: thumbnail 2 (t2) => thumbnail (t) => original (o) => none + if (`t2` in item.imageSizes) { + image_size = `t2`; + } else if (`t` in item.imageSizes) { + image_size = `t`; + } else if (`o` in item.imageSizes) { + image_size = `o`; + } else { + break; + } + + image = item.imageSizes[image_size].url; + image_h = item.imageSizes[image_size].h; + image_w = item.imageSizes[image_size].w; break; } }