diff --git a/src/components/playback/playbackmanager.js b/src/components/playback/playbackmanager.js index 29fe4aaed..a8192885c 100644 --- a/src/components/playback/playbackmanager.js +++ b/src/components/playback/playbackmanager.js @@ -14,7 +14,7 @@ import alert from '../alert'; import { PluginType } from '../../types/plugin.ts'; import { includesAny } from '../../utils/container.ts'; import { getItems } from '../../utils/jellyfin-apiclient/getItems.ts'; -import { getItemBackdropImageUrl } from '../../utils/url'; +import { getItemBackdropImageUrl } from '../../utils/jellyfin-apiclient/BackdropImage'; const UNLIMITED_ITEMS = -1; diff --git a/src/controllers/itemDetails/index.js b/src/controllers/itemDetails/index.js index 37c559f88..1de640f6e 100644 --- a/src/controllers/itemDetails/index.js +++ b/src/controllers/itemDetails/index.js @@ -36,7 +36,7 @@ import Dashboard from '../../utils/dashboard'; import ServerConnections from '../../components/ServerConnections'; import confirm from '../../components/confirm/confirm'; import { download } from '../../scripts/fileDownloader'; -import { getItemBackdropImageUrl } from '../../utils/url'; +import { getItemBackdropImageUrl } from '../../utils/jellyfin-apiclient/BackdropImage'; function autoFocus(container) { import('../../components/autoFocuser').then(({ default: autoFocuser }) => { diff --git a/src/utils/jellyfin-apiclient/BackdropImage.ts b/src/utils/jellyfin-apiclient/BackdropImage.ts new file mode 100644 index 000000000..2d23fd506 --- /dev/null +++ b/src/utils/jellyfin-apiclient/BackdropImage.ts @@ -0,0 +1,55 @@ +import { BaseItemDto } from '@jellyfin/sdk/lib/generated-client'; +import { randomInt } from '../number'; +import { ApiClient } from 'jellyfin-apiclient'; + +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 random If set to true and the item has more than one backdrop, a random image is returned. + * @param options Optional; allows to scale the backdrop image. + * @returns The url of the first or a random backdrop image of the provided item. + */ +export const getItemBackdropImageUrl = (apiClient: ApiClient, item: BaseItemDto, random = false, options: ScaleImageOptions = {}): string | undefined => { + let imgUrl; + + if (item.BackdropImageTags?.length) { + const backdropImgIndex = random ? randomInt(0, item.BackdropImageTags.length - 1) : 0; + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + imgUrl = apiClient.getScaledImageUrl(item.Id!, { + type: '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; + imgUrl = apiClient.getScaledImageUrl(item.ParentBackdropItemId, { + type: 'Backdrop', + index: backdropImgIndex, + tag: item.ParentBackdropImageTags[backdropImgIndex], + ...options + }); + } else if (item.ImageTags?.Primary) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + imgUrl = apiClient.getScaledImageUrl(item.Id!, { + type: 'Primary', + tag: item.ImageTags.Primary, + ...options + }); + } + return imgUrl; +}; diff --git a/src/utils/url.ts b/src/utils/url.ts index 1768807bc..1c65b2343 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -1,7 +1,3 @@ -import { BaseItemDto } from '@jellyfin/sdk/lib/generated-client'; -import { randomInt } from './number'; -import { ApiClient } from 'jellyfin-apiclient'; - /** * Gets the url search string. * This function should be used instead of location.search alone, because the app router @@ -37,55 +33,3 @@ export const getParameterByName = (name: string, url?: string | null | undefined // eslint-disable-next-line compat/compat return new URLSearchParams(url).get(name) || ''; }; - -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 random If set to true and the item has more than one backdrop, a random image is returned. - * @param options Optional; allows to scale the backdrop image. - * @returns The url of the first or a random backdrop image of the provided item. - */ -export const getItemBackdropImageUrl = (apiClient: ApiClient, item: BaseItemDto, random = false, options: ScaleImageOptions = {}): string | undefined => { - let imgUrl; - - if (item.BackdropImageTags?.length) { - const backdropImgIndex = random ? randomInt(0, item.BackdropImageTags.length - 1) : 0; - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - imgUrl = apiClient.getScaledImageUrl(item.Id!, { - type: '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; - imgUrl = apiClient.getScaledImageUrl(item.ParentBackdropItemId, { - type: 'Backdrop', - index: backdropImgIndex, - tag: item.ParentBackdropImageTags[backdropImgIndex], - ...options - }); - } else if (item.ImageTags?.Primary) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - imgUrl = apiClient.getScaledImageUrl(item.Id!, { - type: 'Primary', - tag: item.ImageTags.Primary, - ...options - }); - } - return imgUrl; -};