make reading of images a native module

This commit is contained in:
Violet Caulfield
2025-03-23 07:57:37 -05:00
parent 306cd9fca5
commit 5f077809b4
3 changed files with 29 additions and 10 deletions

View File

@@ -0,0 +1,15 @@
@ReactMethod
public void readBlobInBackground(ReadableMap blob, Promise promise) {
new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] bytes = Base64.decode(blob.getString("data"), Base64.DEFAULT);
String base64String = Base64.encodeToString(bytes, Base64.NO_WRAP);
promise.resolve(base64String);
} catch (Exception e) {
promise.reject("Error", e);
}
}
}).start();
}

View File

@@ -2,8 +2,9 @@ import { ImageFormat, ImageType } from "@jellyfin/sdk/lib/generated-client/model
import { getImageApi } from "@jellyfin/sdk/lib/utils/api"
import _ from "lodash"
import Client from "../../../api/client"
import { backgroundRuntime } from "../../../App";
import { runOnRuntime } from "react-native-reanimated";
import { NativeModules } from "react-native";
const { BackgroundFileReader } = NativeModules;
export function fetchItemImage(itemId: string, imageType: ImageType, width: number, height: number) {
@@ -40,13 +41,7 @@ export function fetchItemImage(itemId: string, imageType: ImageType, width: numb
}
function blobToBase64(blob : Blob) {
return new Promise<string>((resolve, _) => {
runOnRuntime(backgroundRuntime, (blob : Blob) => {
'worklet';
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result as string);
reader.readAsDataURL(blob);
})(blob)
return new Promise<string>((resolve, reject) => {
BackgroundFileReader.readBlobInBackground(blob, resolve, reject)
});
}

9
ios/FileReader.swift Normal file
View File

@@ -0,0 +1,9 @@
@objc(BackgroundFileReader)
class BackgroundFileReader: NSObject {
@objc func readBlobInBackground(_ blob: NSData, resolver: @escaping (String) -> Void, rejecter: @escaping (String) -> Void) {
DispatchQueue.global(qos: .background).async {
let base64String = blob.base64EncodedString(options: [])
resolver(base64String)
}
}
}