@@ -18,8 +18,6 @@ import { filter, map, mergeMap, switchMap, take } from 'rxjs/operators';
1818
1919import { hasNoValue , hasValue , isNotEmpty } from '../../shared/empty.util' ;
2020import { DSONameService } from '../breadcrumbs/dso-name.service' ;
21- import { BitstreamDataService } from '../data/bitstream-data.service' ;
22- import { BitstreamFormatDataService } from '../data/bitstream-format-data.service' ;
2321
2422import { RemoteData } from '../data/remote-data' ;
2523import { BitstreamFormat } from '../shared/bitstream-format.model' ;
@@ -46,6 +44,8 @@ import { ITEM } from '../shared/item.resource-type';
4644import { DOCUMENT , isPlatformBrowser , isPlatformServer } from '@angular/common' ;
4745import { Root } from '../data/root.model' ;
4846import { environment } from '../../../environments/environment' ;
47+ import { Bundle } from '../shared/bundle.model' ;
48+ import { followLink } from '../../shared/utils/follow-link-config.model' ;
4949
5050/**
5151 * The base selector function to select the metaTag section in the store
@@ -95,8 +95,6 @@ export class MetadataService {
9595 private title : Title ,
9696 private dsoNameService : DSONameService ,
9797 private bundleDataService : BundleDataService ,
98- private bitstreamDataService : BitstreamDataService ,
99- private bitstreamFormatDataService : BitstreamFormatDataService ,
10098 private rootService : RootDataService ,
10199 private store : Store < CoreState > ,
102100 private hardRedirectService : HardRedirectService ,
@@ -449,7 +447,7 @@ export class MetadataService {
449447 * Add <meta name="og:image" ... > to the <head>
450448 */
451449 private setOpenGraphImageTag ( ) : void {
452- this . setPrimaryBitstreamInBundleTag ( 'og:image' ) ;
450+ this . setPrimaryImageInBundleTag ( 'og:image' ) ;
453451 }
454452
455453 /**
@@ -465,7 +463,7 @@ export class MetadataService {
465463 * Add <meta name="twitter:image" ... > to the <head>
466464 */
467465 private setTwitterImageTag ( ) : void {
468- this . setPrimaryBitstreamInBundleTag ( 'twitter:image' ) ;
466+ this . setPrimaryImageInBundleTag ( 'twitter:image' ) ;
469467 }
470468
471469 /**
@@ -496,6 +494,74 @@ export class MetadataService {
496494 }
497495
498496 private setPrimaryBitstreamInBundleTag ( tag : string ) : void {
497+ if ( this . currentObject . value instanceof Item ) {
498+ const item = this . currentObject . value as Item ;
499+
500+ // Retrieve the ORIGINAL bundle for the item
501+ this . bundleDataService . findByItemAndName (
502+ item ,
503+ 'ORIGINAL' ,
504+ true ,
505+ true ,
506+ followLink ( 'primaryBitstream' ) ,
507+ followLink ( 'bitstreams' , {
508+ findListOptions : {
509+ // limit the number of bitstreams used to find the citation pdf url to the number
510+ // shown by default on an item page
511+ elementsPerPage : this . appConfig . item . bitstream . pageSize ,
512+ } ,
513+ } , followLink ( 'format' ) ) ,
514+ ) . pipe (
515+ getFirstSucceededRemoteDataPayload ( ) ,
516+ switchMap ( ( bundle : Bundle ) =>
517+ // First try the primary bitstream
518+ bundle . primaryBitstream . pipe (
519+ getFirstCompletedRemoteData ( ) ,
520+ map ( ( rd : RemoteData < Bitstream > ) => {
521+ if ( hasValue ( rd . payload ) ) {
522+ return rd . payload ;
523+ } else {
524+ return null ;
525+ }
526+ } ) ,
527+ getDownloadableBitstream ( this . authorizationService ) ,
528+ // return the bundle as well so we can use it again if there's no primary bitstream
529+ map ( ( bitstream : Bitstream ) => [ bundle , bitstream ] ) ,
530+ ) ,
531+ ) ,
532+ switchMap ( ( [ bundle , primaryBitstream ] : [ Bundle , Bitstream ] ) => {
533+ if ( hasValue ( primaryBitstream ) ) {
534+ // If there was a downloadable primary bitstream, emit its link
535+ return [ getBitstreamDownloadRoute ( primaryBitstream ) ] ;
536+ } else {
537+ // Otherwise consider the regular bitstreams in the bundle
538+ return bundle . bitstreams . pipe (
539+ getFirstCompletedRemoteData ( ) ,
540+ switchMap ( ( bitstreamRd : RemoteData < PaginatedList < Bitstream > > ) => {
541+ if ( hasValue ( bitstreamRd . payload ) && bitstreamRd . payload . totalElements === 1 ) {
542+ // If there's only one bitstream in the bundle, emit its link if its downloadable
543+ return this . getBitLinkIfDownloadable ( bitstreamRd . payload . page [ 0 ] , bitstreamRd ) ;
544+ } else {
545+ // Otherwise check all bitstreams to see if one matches the format whitelist
546+ return this . getFirstAllowedFormatBitstreamLink ( bitstreamRd ) ;
547+ }
548+ } ) ,
549+ ) ;
550+ }
551+ } ) ,
552+ take ( 1 ) ,
553+ ) . subscribe ( ( link : string ) => {
554+ // Use the found link to set the <meta> tag
555+ this . addMetaTag (
556+ tag ,
557+ new URLCombiner ( this . hardRedirectService . getCurrentOrigin ( ) , link ) . toString ( ) ,
558+ true ,
559+ ) ;
560+ } ) ;
561+ }
562+ }
563+
564+ private setPrimaryImageInBundleTag ( tag : string ) : void {
499565 if ( this . currentObject . value instanceof Item ) {
500566 const item = this . currentObject . value as Item ;
501567 this . getBitstreamFromThumbnail ( item ) . pipe (
@@ -506,14 +572,14 @@ export class MetadataService {
506572 return null ;
507573 }
508574 } ) ,
509- take ( 1 )
575+ take ( 1 ) ,
510576 ) . subscribe ( ( link ) => {
511577 if ( hasValue ( link ) ) {
512578 // Use the found link to set the <meta> tag
513579 this . addMetaTag (
514580 tag ,
515581 new URLCombiner ( this . getUrlOrigin ( ) , link ) . toString ( ) ,
516- true
582+ true ,
517583 ) ;
518584 } else {
519585 this . addFallbackImageToTag ( tag ) ;
@@ -654,9 +720,9 @@ export class MetadataService {
654720 return this . currentObject . value . allMetadataValues ( keys ) ;
655721 }
656722
657- private addMetaTag ( name : string , content : string , isProperty = false ) : void {
723+ protected addMetaTag ( name : string , content : string , isProperty = false ) : void {
658724 if ( content ) {
659- const tag = isProperty ? { property : name , content} as MetaDefinition
725+ const tag = isProperty ? { name , property : name , content } as MetaDefinition
660726 : { name, content } as MetaDefinition ;
661727 this . meta . updateTag ( tag ) ;
662728 this . storeTag ( name ) ;
0 commit comments