image filesystem stuff

This commit is contained in:
Violet Caulfield
2025-01-26 07:54:00 -06:00
parent 54d87a6c19
commit d95402cd45

View File

@@ -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<string> {
if (await FileSystem.exists(`${Dirs.CacheDir}/Images/${imageType ?? ImageType.Primary}/${itemId}`))
return FileSystem.readFile(getImagePath(itemId, imageType, width, height))
return new Promise<string>((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}`;
}