mirror of
https://github.com/streamyfin/streamyfin.git
synced 2025-08-20 18:37:18 +02:00
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
// hooks/useFileOpener.ts
|
|
|
|
import { usePlaySettings } from "@/providers/PlaySettingsProvider";
|
|
import { writeToLog } from "@/utils/log";
|
|
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
|
import * as FileSystem from "expo-file-system";
|
|
import { useRouter } from "expo-router";
|
|
import { useCallback } from "react";
|
|
import { Platform } from "react-native";
|
|
|
|
export const useFileOpener = () => {
|
|
const router = useRouter();
|
|
const { setPlayUrl, setOfflineSettings } = usePlaySettings();
|
|
|
|
const openFile = useCallback(async (item: BaseItemDto) => {
|
|
const directory = FileSystem.documentDirectory;
|
|
|
|
if (!directory) {
|
|
throw new Error("Document directory is not available");
|
|
}
|
|
|
|
if (!item.Id) {
|
|
throw new Error("Item ID is not available");
|
|
}
|
|
|
|
try {
|
|
const files = await FileSystem.readDirectoryAsync(directory);
|
|
const path = item.Id!;
|
|
const matchingFile = files.find((file) => file.startsWith(path));
|
|
|
|
if (!matchingFile) {
|
|
throw new Error(`No file found for item ${path}`);
|
|
}
|
|
|
|
const url = `${directory}${matchingFile}`;
|
|
|
|
setOfflineSettings({
|
|
item,
|
|
});
|
|
setPlayUrl(url);
|
|
|
|
if (Platform.OS === "ios") router.push("/offline-vlc-player");
|
|
else router.push("/offline-player");
|
|
} catch (error) {
|
|
writeToLog("ERROR", "Error opening file", error);
|
|
console.error("Error opening file:", error);
|
|
}
|
|
}, []);
|
|
|
|
return { openFile };
|
|
};
|