mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
apply suggested changes from code review
This commit is contained in:
parent
2b547f5a53
commit
5d6756778b
3 changed files with 13 additions and 13 deletions
52
src/utils/jellyfin-apiclient/backdropImage.ts
Normal file
52
src/utils/jellyfin-apiclient/backdropImage.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import type { BaseItemDto } from '@jellyfin/sdk/lib/generated-client/models/base-item-dto';
|
||||
import type { ApiClient } from 'jellyfin-apiclient';
|
||||
import { ImageType } from '@jellyfin/sdk/lib/generated-client/models/image-type';
|
||||
import { randomInt } from '../number';
|
||||
|
||||
export interface ScaleImageOptions {
|
||||
maxWidth?: number;
|
||||
width?: number;
|
||||
maxHeight?: number;
|
||||
height?: number;
|
||||
fillWidth?: number;
|
||||
fillHeight?: number;
|
||||
quality?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the url of the first or a random backdrop image of an item.
|
||||
* If the item has no backdrop image, the url of the first or a random backdrop image of the parent item is returned.
|
||||
* Falls back to the primary image (cover) of the item, if neither the item nor it's parent have at least one backdrop image.
|
||||
* Returns undefined if no usable image was found.
|
||||
* @param apiClient The ApiClient to generate the url.
|
||||
* @param item The item for which the backdrop image is requested.
|
||||
* @param options Optional; allows to scale the backdrop image.
|
||||
* @param random If set to true and the item has more than one backdrop, a random image is returned.
|
||||
* @returns The url of the first or a random backdrop image of the provided item.
|
||||
*/
|
||||
export const getItemBackdropImageUrl = (apiClient: ApiClient, item: BaseItemDto, options: ScaleImageOptions = {}, random = false): string | undefined => {
|
||||
if (item.Id && item.BackdropImageTags?.length) {
|
||||
const backdropImgIndex = random ? randomInt(0, item.BackdropImageTags.length - 1) : 0;
|
||||
return apiClient.getScaledImageUrl(item.Id, {
|
||||
type: ImageType.Backdrop,
|
||||
index: backdropImgIndex,
|
||||
tag: item.BackdropImageTags[backdropImgIndex],
|
||||
...options
|
||||
});
|
||||
} else if (item.ParentBackdropItemId && item.ParentBackdropImageTags?.length) {
|
||||
const backdropImgIndex = random ? randomInt(0, item.ParentBackdropImageTags.length - 1) : 0;
|
||||
return apiClient.getScaledImageUrl(item.ParentBackdropItemId, {
|
||||
type: ImageType.Backdrop,
|
||||
index: backdropImgIndex,
|
||||
tag: item.ParentBackdropImageTags[backdropImgIndex],
|
||||
...options
|
||||
});
|
||||
} else if (item.Id && item.ImageTags?.Primary) {
|
||||
return apiClient.getScaledImageUrl(item.Id, {
|
||||
type: ImageType.Primary,
|
||||
tag: item.ImageTags.Primary,
|
||||
...options
|
||||
});
|
||||
}
|
||||
return undefined;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue