From b38c03dd64aa58ccf80d917098eda28ce34fc265 Mon Sep 17 00:00:00 2001 From: Violet Caulfield Date: Sun, 26 Jan 2025 10:30:23 -0600 Subject: [PATCH] this works still right? --- api/queries/functions/images.ts | 70 +++++++-------------------------- 1 file changed, 15 insertions(+), 55 deletions(-) diff --git a/api/queries/functions/images.ts b/api/queries/functions/images.ts index 20a8e953..f5a3f35f 100644 --- a/api/queries/functions/images.ts +++ b/api/queries/functions/images.ts @@ -1,63 +1,23 @@ -import { ImageFormat, ImageType } from "@jellyfin/sdk/lib/generated-client/models" +import { ImageType } from "@jellyfin/sdk/lib/generated-client/models" import { getImageApi } from "@jellyfin/sdk/lib/utils/api" import _ from "lodash" import Client from "../../../api/client" import { QueryConfig } from "../query.config"; import { Dirs, FileSystem } from 'react-native-file-access' -export async function fetchItemImage( - itemId: string, - imageType: ImageType = ImageType.Primary, - width?: number, - height?: number -) : Promise { +export function fetchItemImage(itemId: string, imageType?: ImageType, width?: number, height?: number) { - return new Promise(async (resolve, reject) => { - const imageExistsInStorage = await FileSystem.exists(`${Dirs.CacheDir}/Images/${imageType}/${itemId}`) - if (imageExistsInStorage) - resolve(await fetchItemImageFromStorage(itemId, imageType, width, height)); - - getImageApi(Client.api!) - .getItemImage({ - itemId, - imageType, - width: width ? Math.ceil(width * 2) : QueryConfig.playerArtwork.width, - height: height ? Math.ceil(height * 2) : QueryConfig.playerArtwork.height, - format: ImageFormat.Jpg, - }, { - responseType: 'blob' - }) - .then(async ({ data } : { data: Blob }) => { - - data.text() - .then((text) => { - console.debug("Writing image to file") - FileSystem.writeFile( - getImagePath(itemId, imageType, width, height), - text - ).then(() => { - console.debug("Successfully wrote image to file") - resolve(URL.createObjectURL(data)); - }).catch(() => { - console.debug("Unable to write image to file, exiting...") - resolve(URL.createObjectURL(data)); - }); - }) - .catch(() => { - console.debug("Unable to read blob, not storing") - resolve(URL.createObjectURL(data)); - }); - }) - .catch(error => { - reject(error); - }); - }); -} - -async function fetchItemImageFromStorage(itemId: string, imageType: ImageType, width?: number, height?: number) { - return await FileSystem.readFile(getImagePath(itemId, imageType, width, height)); -} - -function getImagePath(itemId: string, imageType: ImageType, width?: number, height?: number) { - return `${Dirs.CacheDir}/Images/${imageType}/${itemId}-${width ?? QueryConfig.playerArtwork.width}-${height ?? QueryConfig.playerArtwork.height}`; + return getImageApi(Client.api!) + .getItemImage({ + itemId, + imageType: imageType ? imageType : ImageType.Primary, + width: width ? Math.ceil(width * 2) : QueryConfig.playerArtwork.width, + height: height ? Math.ceil(height * 2) : QueryConfig.playerArtwork.height + }, { + responseType: 'blob' + }) + .then((response) => { + console.log(response) + return URL.createObjectURL(response.data) + }); } \ No newline at end of file