mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-07-19 18:54:16 +01:00
chore: refactor
This commit is contained in:
47
utils/jellyfin/image/getBackdropUrl.ts
Normal file
47
utils/jellyfin/image/getBackdropUrl.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Api } from "@jellyfin/sdk";
|
||||
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import { getPrimaryImageUrl } from "./getPrimaryImageUrl";
|
||||
|
||||
/**
|
||||
* Retrieves the primary image URL for a given item.
|
||||
*
|
||||
* @param api - The Jellyfin API instance.
|
||||
* @param item - The media item to retrieve the backdrop image URL for.
|
||||
* @param quality - The desired image quality (default: 10).
|
||||
*/
|
||||
export const getBackdropUrl = ({
|
||||
api,
|
||||
item,
|
||||
quality,
|
||||
width,
|
||||
}: {
|
||||
api?: Api | null;
|
||||
item?: BaseItemDto | null;
|
||||
quality?: number;
|
||||
width?: number;
|
||||
}) => {
|
||||
if (!api || !item) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const backdropImageTags = item.BackdropImageTags?.[0];
|
||||
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (quality) {
|
||||
params.append("quality", quality.toString());
|
||||
}
|
||||
|
||||
if (width) {
|
||||
params.append("fillWidth", width.toString());
|
||||
}
|
||||
|
||||
if (backdropImageTags) {
|
||||
params.append("tag", backdropImageTags);
|
||||
return `${api.basePath}/Items/${
|
||||
item.Id
|
||||
}/Images/Backdrop/0?${params.toString()}`;
|
||||
} else {
|
||||
return getPrimaryImageUrl({ api, item, quality, width });
|
||||
}
|
||||
};
|
||||
33
utils/jellyfin/image/getLogoImageUrlById.ts
Normal file
33
utils/jellyfin/image/getLogoImageUrlById.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Api } from "@jellyfin/sdk";
|
||||
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
|
||||
|
||||
/**
|
||||
* Retrieves the primary image URL for a given item.
|
||||
*
|
||||
* @param api - The Jellyfin API instance.
|
||||
* @param item - The media item to retrieve the backdrop image URL for.
|
||||
* @param quality - The desired image quality (default: 10).
|
||||
*/
|
||||
export const getLogoImageUrlById = ({
|
||||
api,
|
||||
item,
|
||||
}: {
|
||||
api?: Api | null;
|
||||
item?: BaseItemDto | null;
|
||||
}) => {
|
||||
if (!api || !item) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const imageTags = item.ImageTags?.["Logo"];
|
||||
|
||||
if (!imageTags) return null;
|
||||
|
||||
const params = new URLSearchParams();
|
||||
|
||||
params.append("tag", imageTags);
|
||||
params.append("quality", "90");
|
||||
params.append("fillHeight", "130");
|
||||
|
||||
return `${api.basePath}/Items/${item.Id}/Images/Logo?${params.toString()}`;
|
||||
};
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Api } from "@jellyfin/sdk";
|
||||
import { getImageApi } from "@jellyfin/sdk/lib/utils/api";
|
||||
|
||||
export const getPrimaryImage = async (
|
||||
api: Api,
|
||||
itemId: string,
|
||||
): Promise<string> => {
|
||||
const image = await getImageApi(api).getItemImage({
|
||||
itemId,
|
||||
imageType: "Primary",
|
||||
quality: 90,
|
||||
width: 1000,
|
||||
});
|
||||
|
||||
console.log("getPrimaryImage ~", image.data);
|
||||
|
||||
return image.data;
|
||||
};
|
||||
51
utils/jellyfin/image/getPrimaryImageUrl.ts
Normal file
51
utils/jellyfin/image/getPrimaryImageUrl.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Api } from "@jellyfin/sdk";
|
||||
import {
|
||||
BaseItemDto,
|
||||
BaseItemPerson,
|
||||
} from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import { isBaseItemDto } from "../jellyfin";
|
||||
|
||||
/**
|
||||
* Retrieves the primary image URL for a given item.
|
||||
*
|
||||
* @param api - The Jellyfin API instance.
|
||||
* @param item - The media item to retrieve the backdrop image URL for.
|
||||
* @param quality - The desired image quality (default: 90).
|
||||
*/
|
||||
export const getPrimaryImageUrl = ({
|
||||
api,
|
||||
item,
|
||||
quality = 90,
|
||||
width = 500,
|
||||
}: {
|
||||
api?: Api | null;
|
||||
item?: BaseItemDto | BaseItemPerson | null;
|
||||
quality?: number | null;
|
||||
width?: number | null;
|
||||
}) => {
|
||||
if (!item || !api) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isBaseItemDto(item)) {
|
||||
return `${api?.basePath}/Items/${item?.Id}/Images/Primary`;
|
||||
}
|
||||
|
||||
const backdropTag = item.BackdropImageTags?.[0];
|
||||
const primaryTag = item.ImageTags?.["Primary"];
|
||||
|
||||
const params = new URLSearchParams({
|
||||
fillWidth: width ? String(width) : "500",
|
||||
quality: quality ? String(quality) : "90",
|
||||
});
|
||||
|
||||
if (primaryTag) {
|
||||
params.set("tag", primaryTag);
|
||||
} else if (backdropTag) {
|
||||
params.set("tag", backdropTag);
|
||||
}
|
||||
|
||||
return `${api?.basePath}/Items/${
|
||||
item.Id
|
||||
}/Images/Primary?${params.toString()}`;
|
||||
};
|
||||
31
utils/jellyfin/image/getPrimaryImageUrlById.ts
Normal file
31
utils/jellyfin/image/getPrimaryImageUrlById.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Api } from "@jellyfin/sdk";
|
||||
|
||||
/**
|
||||
* Retrieves the primary image URL for a given item.
|
||||
*
|
||||
* @param api - The Jellyfin API instance.
|
||||
* @param item - The media item to retrieve the backdrop image URL for.
|
||||
* @param quality - The desired image quality (default: 90).
|
||||
*/
|
||||
export const getPrimaryImageUrlById = ({
|
||||
api,
|
||||
id,
|
||||
quality = 90,
|
||||
width = 500,
|
||||
}: {
|
||||
api?: Api | null;
|
||||
id?: string | null;
|
||||
quality?: number | null;
|
||||
width?: number | null;
|
||||
}) => {
|
||||
if (!id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams({
|
||||
fillWidth: width ? String(width) : "500",
|
||||
quality: quality ? String(quality) : "90",
|
||||
});
|
||||
|
||||
return `${api?.basePath}/Items/${id}/Images/Primary?${params.toString()}`;
|
||||
};
|
||||
Reference in New Issue
Block a user