|
| 1 | +import { inject, Injectable } from '@angular/core'; |
| 2 | + |
| 3 | +import { from, Observable } from 'rxjs'; |
| 4 | +import { filter, map, mergeMap, reduce, switchMap, take } from 'rxjs/operators'; |
| 5 | + |
| 6 | +import { followLink } from '../../shared/utils/follow-link-config.model'; |
| 7 | +import { getFirstCompletedRemoteData } from '../shared/operators'; |
| 8 | +import { RemoteData } from '../data/remote-data'; |
| 9 | +import { PaginatedList } from '../data/paginated-list.model'; |
| 10 | +import { Bitstream } from '../shared/bitstream.model'; |
| 11 | +import { BitstreamFormat } from '../shared/bitstream-format.model'; |
| 12 | +import { hasValue } from '../../shared/empty.util'; |
| 13 | +import { BitstreamDataService } from '../data/bitstream-data.service'; |
| 14 | +import { Item } from '../shared/item.model'; |
| 15 | +import { DSpaceObject } from '../shared/dspace-object.model'; |
| 16 | + |
| 17 | +interface ItemAndImage { |
| 18 | + itemUUID: string; |
| 19 | + imageHref: string; |
| 20 | +} |
| 21 | + |
| 22 | +@Injectable({ providedIn: 'root' }) |
| 23 | +export class BitstreamImagesService { |
| 24 | + |
| 25 | + private readonly bitstreamDataService = inject(BitstreamDataService); |
| 26 | + |
| 27 | + /** |
| 28 | + * Retrieve all items and their image bitstreams |
| 29 | + * @param items |
| 30 | + * @param bundleName |
| 31 | + */ |
| 32 | + getItemToImageMap(items: Item[], bundleName = 'ORIGINAL'): Observable<Map<string, string>> { |
| 33 | + return from(items).pipe( |
| 34 | + mergeMap((item) => this.findImageBitstreams(item, bundleName).pipe( |
| 35 | + take(1), |
| 36 | + map((bitstream: Bitstream) => <ItemAndImage>{ |
| 37 | + itemUUID: item.uuid, imageHref: bitstream._links.content.href |
| 38 | + }), |
| 39 | + )), |
| 40 | + reduce((acc: Map<string, string>, value: ItemAndImage) => { |
| 41 | + acc.set(value.itemUUID, value.imageHref); |
| 42 | + return acc; |
| 43 | + }, new Map<string, string>()), |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Find all image bitstreams for an item |
| 49 | + * @param item the item for which the images should be retrieved |
| 50 | + * @param bundleName the bundle name (ORIGINAL by default) |
| 51 | + */ |
| 52 | + findImageBitstreams(item: Item | DSpaceObject, bundleName = 'ORIGINAL') { |
| 53 | + const isImageMimetypeRegex = /^image\//; |
| 54 | + |
| 55 | + // retrieve all bundle's bitstreams for the item |
| 56 | + const bitstreamPayload$: Observable<Bitstream> = this.bitstreamDataService.showableByItem( |
| 57 | + item.uuid, bundleName, [], {}, true, true, followLink('format'), |
| 58 | + ).pipe( |
| 59 | + getFirstCompletedRemoteData(), |
| 60 | + switchMap((rd: RemoteData<PaginatedList<Bitstream>>) => rd.hasSucceeded ? rd.payload.page : new Array<Bitstream>()), |
| 61 | + ); |
| 62 | + |
| 63 | + // filter bitstreams according to mime type |
| 64 | + return bitstreamPayload$.pipe( |
| 65 | + switchMap((bitstream: Bitstream) => bitstream.format.pipe( |
| 66 | + getFirstCompletedRemoteData(), |
| 67 | + filter((bitstreamFormatRD: RemoteData<BitstreamFormat>) => |
| 68 | + bitstreamFormatRD.hasSucceeded && hasValue(bitstreamFormatRD.payload) && hasValue(bitstream) && |
| 69 | + isImageMimetypeRegex.test(bitstreamFormatRD.payload.mimetype) |
| 70 | + ), |
| 71 | + map(() => bitstream) |
| 72 | + )), |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments