mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-04-20 15:54:42 +01:00
Merge branch 'develop' into feat/i18n
This commit is contained in:
106
hooks/useJellyfinDiscovery.tsx
Normal file
106
hooks/useJellyfinDiscovery.tsx
Normal file
@@ -0,0 +1,106 @@
|
||||
import { useState, useCallback } from "react";
|
||||
import dgram from "react-native-udp";
|
||||
|
||||
const JELLYFIN_DISCOVERY_PORT = 7359;
|
||||
const DISCOVERY_MESSAGE = "Who is JellyfinServer?";
|
||||
|
||||
interface ServerInfo {
|
||||
address: string;
|
||||
port: number;
|
||||
serverId?: string;
|
||||
serverName?: string;
|
||||
}
|
||||
|
||||
export const useJellyfinDiscovery = () => {
|
||||
const [servers, setServers] = useState<ServerInfo[]>([]);
|
||||
const [isSearching, setIsSearching] = useState(false);
|
||||
|
||||
const startDiscovery = useCallback(() => {
|
||||
setIsSearching(true);
|
||||
setServers([]);
|
||||
|
||||
const discoveredServers = new Set<string>();
|
||||
let discoveryTimeout: NodeJS.Timeout;
|
||||
|
||||
const socket = dgram.createSocket({
|
||||
type: "udp4",
|
||||
reusePort: true,
|
||||
debug: __DEV__,
|
||||
});
|
||||
|
||||
socket.on("error", (err) => {
|
||||
console.error("Socket error:", err);
|
||||
socket.close();
|
||||
setIsSearching(false);
|
||||
});
|
||||
|
||||
socket.bind(0, () => {
|
||||
console.log("UDP socket bound successfully");
|
||||
|
||||
try {
|
||||
socket.setBroadcast(true);
|
||||
const messageBuffer = new TextEncoder().encode(DISCOVERY_MESSAGE);
|
||||
|
||||
socket.send(
|
||||
messageBuffer,
|
||||
0,
|
||||
messageBuffer.length,
|
||||
JELLYFIN_DISCOVERY_PORT,
|
||||
"255.255.255.255",
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.error("Failed to send discovery message:", err);
|
||||
return;
|
||||
}
|
||||
console.log("Discovery message sent successfully");
|
||||
}
|
||||
);
|
||||
|
||||
discoveryTimeout = setTimeout(() => {
|
||||
setIsSearching(false);
|
||||
socket.close();
|
||||
}, 5000);
|
||||
} catch (error) {
|
||||
console.error("Error during discovery:", error);
|
||||
setIsSearching(false);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("message", (msg, rinfo: any) => {
|
||||
if (discoveredServers.has(rinfo.address)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = new TextDecoder().decode(msg);
|
||||
const serverInfo = JSON.parse(response);
|
||||
discoveredServers.add(rinfo.address);
|
||||
|
||||
const newServer: ServerInfo = {
|
||||
address: `http://${rinfo.address}:${serverInfo.Port || 8096}`,
|
||||
port: serverInfo.Port || 8096,
|
||||
serverId: serverInfo.Id,
|
||||
serverName: serverInfo.Name,
|
||||
};
|
||||
|
||||
setServers((prev) => [...prev, newServer]);
|
||||
} catch (error) {
|
||||
console.error("Error parsing server response:", error);
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
clearTimeout(discoveryTimeout);
|
||||
if (isSearching) {
|
||||
setIsSearching(false);
|
||||
}
|
||||
socket.close();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
servers,
|
||||
isSearching,
|
||||
startDiscovery,
|
||||
};
|
||||
};
|
||||
@@ -35,6 +35,11 @@ import {
|
||||
} from "@/utils/jellyseerr/server/models/Person";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import {GenreSliderItem} from "@/utils/jellyseerr/server/interfaces/api/discoverInterfaces";
|
||||
import {UserResultsResponse} from "@/utils/jellyseerr/server/interfaces/api/userInterfaces";
|
||||
import {
|
||||
ServiceCommonServer,
|
||||
ServiceCommonServerWithDetails
|
||||
} from "@/utils/jellyseerr/server/interfaces/api/serviceInterfaces";
|
||||
|
||||
interface SearchParams {
|
||||
query: string;
|
||||
@@ -67,6 +72,8 @@ export enum Endpoints {
|
||||
MOVIE = "/movie",
|
||||
RATINGS = "/ratings",
|
||||
ISSUE = "/issue",
|
||||
USER = "/user",
|
||||
SERVICE = "/service",
|
||||
TV = "/tv",
|
||||
SETTINGS = "/settings",
|
||||
NETWORK = "/network",
|
||||
@@ -283,6 +290,12 @@ export class JellyseerrApi {
|
||||
});
|
||||
}
|
||||
|
||||
async user(params: any) {
|
||||
return this.axios
|
||||
?.get<UserResultsResponse>(`${Endpoints.API_V1}${Endpoints.USER}`, { params })
|
||||
.then(({data}) => data.results)
|
||||
}
|
||||
|
||||
imageProxy(
|
||||
path?: string,
|
||||
filter: string = "original",
|
||||
@@ -316,6 +329,18 @@ export class JellyseerrApi {
|
||||
});
|
||||
}
|
||||
|
||||
async service(type: 'radarr' | 'sonarr') {
|
||||
return this.axios
|
||||
?.get<ServiceCommonServer[]>(Endpoints.API_V1 + Endpoints.SERVICE + `/${type}`)
|
||||
.then(({data}) => data);
|
||||
}
|
||||
|
||||
async serviceDetails(type: 'radarr' | 'sonarr', id: number) {
|
||||
return this.axios
|
||||
?.get<ServiceCommonServerWithDetails>(Endpoints.API_V1 + Endpoints.SERVICE + `/${type}` + `/${id}`)
|
||||
.then(({data}) => data);
|
||||
}
|
||||
|
||||
private setInterceptors() {
|
||||
this.axios.interceptors.response.use(
|
||||
async (response) => {
|
||||
|
||||
Reference in New Issue
Block a user