11const { pickBy, assign, get : getNested } = require ( 'lodash' )
22const extent = require ( '@mapbox/extent' )
33const { DateTime } = require ( 'luxon' )
4+ const AWS = require ( 'aws-sdk' )
45const { isIndexNotFoundError } = require ( './es' )
56const logger = console
67
@@ -357,6 +358,10 @@ const addItemLinks = function (results, endpoint) {
357358 rel : 'root' ,
358359 href : `${ endpoint } /`
359360 } )
361+ links . push ( {
362+ rel : 'thumbnail' ,
363+ href : `${ endpoint } /collections/${ collection } /items/${ id } /thumbnail`
364+ } )
360365 result . type = 'Feature'
361366 return result
362367 } )
@@ -689,6 +694,41 @@ const deleteItem = async function (collectionId, itemId, backend) {
689694 return new Error ( `Error deleting item ${ collectionId } /${ itemId } ` )
690695}
691696
697+ const getItemThumbnail = async function ( collectionId , itemId , backend ) {
698+ const itemQuery = { collections : [ collectionId ] , id : itemId }
699+ const { results } = await backend . search ( itemQuery )
700+ const [ item ] = results
701+ if ( ! item ) {
702+ return new Error ( 'Item not found' )
703+ }
704+
705+ const thumbnailAsset = Object . values ( item . assets || [ ] ) . find (
706+ ( x ) => x . roles && x . roles . includes ( 'thumbnail' )
707+ )
708+ if ( ! thumbnailAsset ) {
709+ return new Error ( 'Thumbnail not found' )
710+ }
711+
712+ let location
713+ if ( thumbnailAsset . href && thumbnailAsset . href . startsWith ( 'http' ) ) {
714+ location = thumbnailAsset . href
715+ } else if ( thumbnailAsset . href && thumbnailAsset . href . startsWith ( 's3' ) ) {
716+ const withoutProtocol = thumbnailAsset . href . substring ( 5 ) // chop off s3://
717+ const [ bucket , ...keyArray ] = withoutProtocol . split ( '/' )
718+ const key = keyArray . join ( '/' )
719+ location = new AWS . S3 ( ) . getSignedUrl ( 'getObject' , {
720+ Bucket : bucket ,
721+ Key : key ,
722+ Expires : 60 * 5 , // expiry in seconds
723+ RequestPayer : 'requester'
724+ } )
725+ } else {
726+ return new Error ( 'Thumbnail not found' )
727+ }
728+
729+ return { location }
730+ }
731+
692732module . exports = {
693733 getConformance,
694734 getCatalog,
@@ -705,5 +745,6 @@ module.exports = {
705745 partialUpdateItem,
706746 ValidationError,
707747 extractLimit,
708- extractDatetime
748+ extractDatetime,
749+ getItemThumbnail,
709750}
0 commit comments