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 ( './es' )
5
6
const logger = console
6
7
@@ -357,6 +358,10 @@ const addItemLinks = function (results, endpoint) {
357
358
rel : 'root' ,
358
359
href : `${ endpoint } /`
359
360
} )
361
+ links . push ( {
362
+ rel : 'thumbnail' ,
363
+ href : `${ endpoint } /collections/${ collection } /items/${ id } /thumbnail`
364
+ } )
360
365
result . type = 'Feature'
361
366
return result
362
367
} )
@@ -689,6 +694,41 @@ const deleteItem = async function (collectionId, itemId, backend) {
689
694
return new Error ( `Error deleting item ${ collectionId } /${ itemId } ` )
690
695
}
691
696
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
+
692
732
module . exports = {
693
733
getConformance,
694
734
getCatalog,
@@ -705,5 +745,6 @@ module.exports = {
705
745
partialUpdateItem,
706
746
ValidationError,
707
747
extractLimit,
708
- extractDatetime
748
+ extractDatetime,
749
+ getItemThumbnail,
709
750
}
0 commit comments