From d95402cd45166df27f5c50cded33492da9b2292a Mon Sep 17 00:00:00 2001 From: Violet Caulfield Date: Sun, 26 Jan 2025 07:54:00 -0600 Subject: [PATCH] image filesystem stuff --- api/queries/functions/images.ts | 60 ++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/api/queries/functions/images.ts b/api/queries/functions/images.ts index a4c52afb..526da1c3 100644 --- a/api/queries/functions/images.ts +++ b/api/queries/functions/images.ts @@ -1,33 +1,53 @@ -import { ImageType } from "@jellyfin/sdk/lib/generated-client/models" +import { ImageFormat, 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, width?: number, height?: number) { +export async function fetchItemImage( + itemId: string, + imageType: ImageType = ImageType.Primary, + width?: number, + height?: number +) : Promise { - if (await FileSystem.exists(`${Dirs.CacheDir}/Images/${imageType ?? ImageType.Primary}/${itemId}`)) - return FileSystem.readFile(getImagePath(itemId, imageType, width, height)) + return new Promise((resolve, reject) => { + FileSystem.exists(`${Dirs.CacheDir}/Images/${imageType}/${itemId}`) + .then((imageExists) => { + if (imageExists) + resolve(fetchItemImageFromStorage(itemId, imageType, width, height)) + }) - return FileSystem.fetch( - getImageApi(Client.api!) - .getItemImageUrlById( - itemId, - imageType ?? ImageType.Primary, - { - width: width ? Math.ceil(width * 2) : QueryConfig.playerArtwork.width, - height: height ? Math.ceil(height * 2) : QueryConfig.playerArtwork.height + FileSystem.fetch( + getImageApi(Client.api!) + .getItemImageUrlById( + itemId, + imageType, + { + format: ImageFormat.Jpg, + width: width ? Math.ceil(width * 2) : QueryConfig.playerArtwork.width, + height: height ? Math.ceil(height * 2) : QueryConfig.playerArtwork.height + } + ), + { + path: getImagePath(itemId, imageType, width, height), } - ), - { - path: getImagePath(itemId, imageType, width, height), - } - ).then((result) => { - return result.url + ).then((result) => { + if (result.ok) + resolve(result.url) + else + reject(result.headers) + }).catch(error => { + reject(error) + }) }) } -function getImagePath(itemId: string, imageType?: ImageType, width?: number, height?: number) { - return `${Dirs.CacheDir}/Images/${imageType ?? ImageType.Primary}/${itemId}-${width ?? QueryConfig.playerArtwork.width}-${height ?? QueryConfig.playerArtwork.height}`; +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}`; } \ No newline at end of file