mirror of
https://github.com/Jellify-Music/App.git
synced 2026-02-22 03:39:12 -06:00
make reading of images a native module
This commit is contained in:
@@ -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();
|
||||
}
|
||||
@@ -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
9
ios/FileReader.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user