chore: refactor

This commit is contained in:
Fredrik Burmester
2024-08-07 08:15:30 +02:00
parent d4d3cbbc43
commit 49b2e594d6
31 changed files with 525 additions and 487 deletions

View File

@@ -5,6 +5,7 @@ export type ProcessItem = {
item: BaseItemDto;
progress: number;
speed?: number;
startTime?: Date;
};
export const runningProcesses = atom<ProcessItem | null>(null);

View File

@@ -1,363 +0,0 @@
import { Api } from "@jellyfin/sdk";
import {
BaseItemDto,
BaseItemPerson,
MediaSourceInfo,
PlaybackInfoResponse,
} from "@jellyfin/sdk/lib/generated-client/models";
import {
getMediaInfoApi,
getUserLibraryApi,
} from "@jellyfin/sdk/lib/utils/api";
import ios12 from "./profiles/ios12";
/**
* Retrieves the playback URL for the given item ID and user ID.
*
* @param api - The Jellyfin API instance.
* @param itemId - The ID of the media item to retrieve playback URL for.
* @param userId - The ID of the user requesting the playback URL.
* @returns The playback URL as a string.
*/
export const getPlaybackUrl = async (
api: Api,
itemId: string,
userId: string,
): Promise<string> => {
const playbackData = await getMediaInfoApi(api).getPlaybackInfo({
itemId,
userId,
});
const mediaSources = playbackData.data?.MediaSources;
if (!mediaSources || mediaSources.length === 0) {
throw new Error(
"No media sources available for the requested item and user.",
);
}
const mediaSource = mediaSources[0];
const transcodeUrl = mediaSource.TranscodingUrl;
if (transcodeUrl) {
return transcodeUrl;
}
// Construct a fallback URL if the TranscodingUrl is not available
const { Id, ETag } = mediaSource;
if (!Id) {
throw new Error("Media source ID is missing.");
}
const queryParams = new URLSearchParams({
videoBitRate: "4000",
videoCodec: "h264",
audioCodec: "aac",
container: "mp4",
SegmentContainer: "mp4",
deviceId: api.deviceInfo?.id || "",
api_key: api.accessToken || "",
Tag: ETag || "",
MediaSourceId: Id || "",
});
return `/Videos/${Id}/stream?${queryParams}`;
};
/**
* Retrieves an item by its ID from the API.
*
* @param api - The Jellyfin API instance.
* @param itemId - The ID of the item to retrieve.
* @returns The item object or undefined if no item matches the ID.
*/
export const getItemById = async (
api?: Api | null | undefined,
itemId?: string | null | undefined,
): Promise<BaseItemDto | undefined> => {
if (!api || !itemId) {
return undefined;
}
try {
const itemData = await getUserLibraryApi(api).getItem({ itemId });
const item = itemData.data;
if (!item) {
console.error("No items found with the specified ID:", itemId);
return undefined;
}
return item;
} catch (error) {
console.error("Failed to retrieve the item:", error);
throw new Error(`Failed to retrieve the item due to an error: ${error}`);
}
};
export const getStreamUrl = async ({
api,
item,
userId,
startTimeTicks = 0,
maxStreamingBitrate,
sessionData,
deviceProfile = ios12,
}: {
api: Api | null | undefined;
item: BaseItemDto | null | undefined;
userId: string | null | undefined;
startTimeTicks: number;
maxStreamingBitrate?: number;
sessionData: PlaybackInfoResponse;
deviceProfile: any;
}) => {
if (!api || !userId || !item?.Id) {
return null;
}
const itemId = item.Id;
const response = await api.axiosInstance.post(
`${api.basePath}/Items/${itemId}/PlaybackInfo`,
{
DeviceProfile: deviceProfile,
UserId: userId,
MaxStreamingBitrate: maxStreamingBitrate,
StartTimeTicks: startTimeTicks,
EnableTranscoding: maxStreamingBitrate ? true : undefined,
AutoOpenLiveStream: true,
MediaSourceId: itemId,
AllowVideoStreamCopy: maxStreamingBitrate ? false : true,
},
{
headers: {
Authorization: `MediaBrowser DeviceId="${api.deviceInfo.id}", Token="${api.accessToken}"`,
},
},
);
const mediaSource = response.data.MediaSources?.[0] as MediaSourceInfo;
if (!mediaSource) {
throw new Error("No media source");
}
if (!sessionData.PlaySessionId) {
throw new Error("no PlaySessionId");
}
if (mediaSource.SupportsDirectPlay) {
console.log("Using direct stream!");
return `${api.basePath}/Videos/${itemId}/stream.mp4?playSessionId=${sessionData.PlaySessionId}&mediaSourceId=${itemId}&static=true`;
}
console.log("Using transcoded stream!");
return `${api.basePath}${mediaSource.TranscodingUrl}`;
};
/**
* 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 getPrimaryImage = ({
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()}`;
};
/**
* 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 getPrimaryImageById = ({
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()}`;
};
function isBaseItemDto(item: any): item is BaseItemDto {
return item && "BackdropImageTags" in item && "ImageTags" in item;
}
/**
* 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 getLogoImageById = ({
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()}`;
};
/**
* 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 getBackdrop = ({
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 getPrimaryImage({ api, item, quality, width });
}
};
/**
* Builds a stream URL for the given item ID, user ID, and other parameters.
*
* @param {object} options - Options to build the stream URL.
* @param {string} options.deviceId - The device ID of the requesting client.
* @param {string} options.apiKey - The API key used for authentication.
* @param {string} options.sessionId - The session ID of the user requesting the stream.
* @param {string} options.itemId - The ID of the media item to retrieve the stream URL for.
* @param {string} options.serverUrl - The base URL of the Jellyfin server.
* @param {string} options.mediaSourceId - The ID of the media source requested.
* @param {string} options.tag - The ETag tag of the media source.
*
* @returns {URL} A URL that can be used to stream the media item.
*/
function buildStreamUrl({
deviceId,
apiKey,
sessionId,
itemId,
serverUrl,
mediaSourceId,
tag,
}: {
deviceId: string;
apiKey: string;
sessionId: string;
itemId: string;
serverUrl: string;
mediaSourceId: string;
tag: string;
}): URL {
const streamParams = new URLSearchParams({
Static: "true",
deviceId,
api_key: apiKey,
playSessionId: sessionId,
videoCodec: "h264",
audioCodec: "aac,mp3,ac3,eac3,flac,alac",
maxAudioChannels: "6",
mediaSourceId,
Tag: tag,
});
return new URL(
`${serverUrl}/Videos/${itemId}/stream.mp4?${streamParams.toString()}`,
);
}
export const bitrateToString = (bitrate: number) => {
const kbps = bitrate / 1000;
const mbps = (bitrate / 1000000).toFixed(2);
return `${mbps} Mb/s`;
};

View 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 });
}
};

View 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()}`;
};

View File

@@ -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;
};

View 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()}`;
};

View 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()}`;
};

View File

@@ -1,8 +1,29 @@
import { Api } from "@jellyfin/sdk";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
/**
* Generates the authorization headers for Jellyfin API requests.
*
* @param {Api} api - The Jellyfin API instance.
* @returns {Record<string, string>} - The authorization headers.
*/
export const getAuthHeaders = (api: Api) => ({
export const getAuthHeaders = (api: Api): Record<string, string> => ({
Authorization: `MediaBrowser DeviceId="${api.deviceInfo.id}", Token="${api.accessToken}"`,
});
/**
* Converts a bitrate to a human-readable string.
*
* @param {number} bitrate - The bitrate to convert.
* @returns {string} - The bitrate as a human-readable string.
*/
export const bitrateToString = (bitrate: number): string => {
const kbps = bitrate / 1000;
const mbps = (bitrate / 1000000).toFixed(2);
return `${mbps} Mb/s`;
};
export function isBaseItemDto(item: any): item is BaseItemDto {
return item && "BackdropImageTags" in item && "ImageTags" in item;
}

View File

@@ -0,0 +1,19 @@
import { Api } from "@jellyfin/sdk";
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api";
export const getPlaybackInfo = async (
api?: Api | null | undefined,
itemId?: string | null | undefined,
userId?: string | null | undefined,
) => {
if (!api || !itemId || !userId) {
return null;
}
const a = await getMediaInfoApi(api).getPlaybackInfo({
itemId,
userId,
});
return a.data;
};

View File

@@ -0,0 +1,49 @@
import { Api } from "@jellyfin/sdk";
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api";
/**
* Retrieves the playback URL for the given item ID and user ID.
*
* @param api - The Jellyfin API instance.
* @param itemId - The ID of the media item to retrieve playback URL for.
* @param userId - The ID of the user requesting the playback URL.
* @returns The playback URL as a string.
*/
export const getPlaybackUrl = async (
api: Api,
itemId: string,
userId: string,
): Promise<string> => {
const playbackData = await getMediaInfoApi(api).getPlaybackInfo({
itemId,
userId,
});
const mediaSources = playbackData.data?.MediaSources;
if (!mediaSources || mediaSources.length === 0) {
throw new Error(
"No media sources available for the requested item and user.",
);
}
const mediaSource = mediaSources[0];
const transcodeUrl = mediaSource.TranscodingUrl;
if (transcodeUrl) {
return transcodeUrl;
}
// Construct a fallback URL if the TranscodingUrl is not available
const { Id, ETag } = mediaSource;
if (!Id) {
throw new Error("Media source ID is missing.");
}
const queryParams = new URLSearchParams({
deviceId: api.deviceInfo?.id || "",
api_key: api.accessToken || "",
Tag: ETag || "",
MediaSourceId: Id || "",
});
return `/Videos/${Id}/stream?${queryParams}`;
};

View File

@@ -0,0 +1,67 @@
import ios12 from "@/utils/profiles/ios12";
import { Api } from "@jellyfin/sdk";
import {
BaseItemDto,
PlaybackInfoResponse,
MediaSourceInfo,
} from "@jellyfin/sdk/lib/generated-client/models";
export const getStreamUrl = async ({
api,
item,
userId,
startTimeTicks = 0,
maxStreamingBitrate,
sessionData,
deviceProfile = ios12,
}: {
api: Api | null | undefined;
item: BaseItemDto | null | undefined;
userId: string | null | undefined;
startTimeTicks: number;
maxStreamingBitrate?: number;
sessionData: PlaybackInfoResponse;
deviceProfile: any;
}) => {
if (!api || !userId || !item?.Id) {
return null;
}
const itemId = item.Id;
const response = await api.axiosInstance.post(
`${api.basePath}/Items/${itemId}/PlaybackInfo`,
{
DeviceProfile: deviceProfile,
UserId: userId,
MaxStreamingBitrate: maxStreamingBitrate,
StartTimeTicks: startTimeTicks,
EnableTranscoding: maxStreamingBitrate ? true : undefined,
AutoOpenLiveStream: true,
MediaSourceId: itemId,
AllowVideoStreamCopy: maxStreamingBitrate ? false : true,
},
{
headers: {
Authorization: `MediaBrowser DeviceId="${api.deviceInfo.id}", Token="${api.accessToken}"`,
},
},
);
const mediaSource = response.data.MediaSources?.[0] as MediaSourceInfo;
if (!mediaSource) {
throw new Error("No media source");
}
if (!sessionData.PlaySessionId) {
throw new Error("no PlaySessionId");
}
if (mediaSource.SupportsDirectPlay) {
console.log("Using direct stream!");
return `${api.basePath}/Videos/${itemId}/stream.mp4?playSessionId=${sessionData.PlaySessionId}&mediaSourceId=${itemId}&static=true`;
}
console.log("Using transcoded stream!");
return `${api.basePath}${mediaSource.TranscodingUrl}`;
};

View File

@@ -0,0 +1,34 @@
import { Api } from "@jellyfin/sdk";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api";
/**
* Retrieves an item by its ID from the API.
*
* @param api - The Jellyfin API instance.
* @param itemId - The ID of the item to retrieve.
* @returns The item object or undefined if no item matches the ID.
*/
export const getItemById = async (
api?: Api | null | undefined,
itemId?: string | null | undefined,
): Promise<BaseItemDto | undefined> => {
if (!api || !itemId) {
return undefined;
}
try {
const itemData = await getUserLibraryApi(api).getItem({ itemId });
const item = itemData.data;
if (!item) {
console.error("No items found with the specified ID:", itemId);
return undefined;
}
return item;
} catch (error) {
console.error("Failed to retrieve the item:", error);
throw new Error(`Failed to retrieve the item due to an error: ${error}`);
}
};

View File

@@ -1,8 +1,6 @@
import { Api } from "@jellyfin/sdk";
import {
getMediaInfoApi,
getUserLibraryApi,
} from "@jellyfin/sdk/lib/utils/api";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api";
/**
* Fetches the media info for a given item.
@@ -21,26 +19,9 @@ export const getUserItemData = async ({
api: Api | null | undefined;
itemId: string | null | undefined;
userId: string | null | undefined;
}) => {
}): Promise<BaseItemDto | null> => {
if (!api || !itemId || !userId) {
return null;
}
return (await getUserLibraryApi(api).getItem({ itemId, userId })).data;
};
export const getPlaybackInfo = async (
api?: Api | null | undefined,
itemId?: string | null | undefined,
userId?: string | null | undefined,
) => {
if (!api || !itemId || !userId) {
return null;
}
const a = await getMediaInfoApi(api).getPlaybackInfo({
itemId,
userId,
});
return a.data;
};