|
1 | 1 | const { pickBy, assign, get: getNested } = require('lodash')
|
2 | 2 | const extent = require('@mapbox/extent')
|
3 | 3 | const { DateTime } = require('luxon')
|
| 4 | +const AWS = require('aws-sdk') |
4 | 5 | const { isIndexNotFoundError } = require('./database')
|
5 | 6 | const logger = console
|
6 | 7 |
|
@@ -374,6 +375,10 @@ const addItemLinks = function (results, endpoint) {
|
374 | 375 | type: 'application/geo+json',
|
375 | 376 | href: `${endpoint}`
|
376 | 377 | })
|
| 378 | + links.push({ |
| 379 | + rel: 'thumbnail', |
| 380 | + href: `${endpoint}/collections/${collection}/items/${id}/thumbnail` |
| 381 | + }) |
377 | 382 | result.type = 'Feature'
|
378 | 383 | return result
|
379 | 384 | })
|
@@ -862,6 +867,39 @@ const deleteItem = async function (collectionId, itemId, backend) {
|
862 | 867 | return new Error(`Error deleting item ${collectionId}/${itemId}`)
|
863 | 868 | }
|
864 | 869 |
|
| 870 | +const getItemThumbnail = async function (collectionId, itemId, backend) { |
| 871 | + const itemQuery = { collections: [collectionId], id: itemId } |
| 872 | + const { results } = await backend.search(itemQuery) |
| 873 | + const [item] = results |
| 874 | + if (!item) { |
| 875 | + return new Error('Item not found') |
| 876 | + } |
| 877 | + |
| 878 | + const thumbnailAsset = Object.values(item.assets || []).find((x) => x.roles.includes('thumbnail')) |
| 879 | + if (!thumbnailAsset) { |
| 880 | + return new Error('Thumbnail not found') |
| 881 | + } |
| 882 | + |
| 883 | + let location |
| 884 | + if (thumbnailAsset.href && thumbnailAsset.href.startsWith('http')) { |
| 885 | + location = thumbnailAsset.href |
| 886 | + } else if (thumbnailAsset.href && thumbnailAsset.href.startsWith('s3')) { |
| 887 | + const withoutProtocol = thumbnailAsset.href.substring(5) // chop off s3:// |
| 888 | + const [bucket, ...keyArray] = withoutProtocol.split('/') |
| 889 | + const key = keyArray.join('/') |
| 890 | + location = new AWS.S3().getSignedUrl('getObject', { |
| 891 | + Bucket: bucket, |
| 892 | + Key: key, |
| 893 | + Expires: 60 * 5, // expiry in seconds |
| 894 | + RequestPayer: 'requester' |
| 895 | + }) |
| 896 | + } else { |
| 897 | + return new Error('Thumbnail not found') |
| 898 | + } |
| 899 | + |
| 900 | + return { location } |
| 901 | +} |
| 902 | + |
865 | 903 | const healthCheck = async function (backend) {
|
866 | 904 | const response = await backend.healthCheck()
|
867 | 905 | logger.debug(`Health check: ${response}`)
|
@@ -890,5 +928,6 @@ module.exports = {
|
890 | 928 | extractLimit,
|
891 | 929 | extractDatetime,
|
892 | 930 | aggregate,
|
893 |
| - healthCheck |
| 931 | + getItemThumbnail, |
| 932 | + healthCheck, |
894 | 933 | }
|
0 commit comments