diff --git a/app/(auth)/(tabs)/(home)/downloads.tsx b/app/(auth)/(tabs)/(home)/downloads.tsx index 185ecbee..5430c27d 100644 --- a/app/(auth)/(tabs)/(home)/downloads.tsx +++ b/app/(auth)/(tabs)/(home)/downloads.tsx @@ -70,28 +70,28 @@ const downloads: React.FC = () => { if (info.exists) { if (info.isDirectory) { // List items in the directory - const subItems = await FileSystem.readDirectoryAsync(fullPath); - if (subItems.length === 0) { - console.log(`Directory ${item} is empty.`); - } else { - console.log(`Items in ${item}:`, subItems); - // If item ends in m3u8, print the content of the file - const m3u8Files = subItems.filter((subItem) => - subItem.endsWith(".m3u8") - ); - if (m3u8Files.length === 0) { - console.log(`No .m3u8 files found in ${item}.`); - } else { - for (let subItem of m3u8Files) { - console.log( - `Content of ${subItem}:`, - await FileSystem.readAsStringAsync( - `${fullPath}/${subItem}` - ) - ); - } - } - } + // const subItems = await FileSystem.readDirectoryAsync(fullPath); + // if (subItems.length === 0) { + // console.log(`Directory ${item} is empty.`); + // } else { + // console.log(`Items in ${item}:`, subItems); + // // If item ends in m3u8, print the content of the file + // const m3u8Files = subItems.filter((subItem) => + // subItem.endsWith(".m3u8") + // ); + // if (m3u8Files.length === 0) { + // console.log(`No .m3u8 files found in ${item}.`); + // } else { + // for (let subItem of m3u8Files) { + // console.log( + // `Content of ${subItem}:`, + // await FileSystem.readAsStringAsync( + // `${fullPath}/${subItem}` + // ) + // ); + // } + // } + // } } else { console.log(`${item} is a file`); } diff --git a/components/DownloadItem.tsx b/components/DownloadItem.tsx index 4dfd9015..652b8131 100644 --- a/components/DownloadItem.tsx +++ b/components/DownloadItem.tsx @@ -32,6 +32,7 @@ import { MediaSourceSelector } from "./MediaSourceSelector"; import ProgressCircle from "./ProgressCircle"; import { SubtitleTrackSelector } from "./SubtitleTrackSelector"; import { useDownloadM3U8Files } from "@/hooks/useDownloadM3U8Files"; +import * as FileSystem from "expo-file-system"; interface DownloadProps extends ViewProps { item: BaseItemDto; @@ -45,8 +46,7 @@ export const DownloadItem: React.FC = ({ item, ...props }) => { const [settings] = useSettings(); // const { startRemuxing } = useRemuxHlsToMp4(item); - const { cancelDownload, startBackgroundDownload } = - useDownloadM3U8Files(item); + const { startBackgroundDownload } = useDownloadM3U8Files(item); const [selectedMediaSource, setSelectedMediaSource] = useState(null); @@ -175,13 +175,36 @@ export const DownloadItem: React.FC = ({ item, ...props }) => { const { data: downloaded, isFetching } = useQuery({ queryKey: ["downloaded", item.Id], queryFn: async () => { - if (!item.Id) return false; + if (!item.Id) { + return false; + } - const data: BaseItemDto[] = JSON.parse( - (await AsyncStorage.getItem("downloaded_files")) || "[]" - ); + try { + // Check if the item exists in AsyncStorage + const downloadedItems = await AsyncStorage.getItem("downloadedItems"); + const items: BaseItemDto[] = downloadedItems + ? JSON.parse(downloadedItems) + : []; + const isInStorage = items.some( + (storedItem) => storedItem.Id === item.Id + ); - return data.some((d) => d.Id === item.Id); + if (!isInStorage) { + return false; + } + + // Check if the directory and m3u8 file exist + const directoryPath = `${FileSystem.documentDirectory}${item.Id}`; + const m3u8FilePath = `${directoryPath}/local.m3u8`; + + const dirInfo = await FileSystem.getInfoAsync(directoryPath); + const fileInfo = await FileSystem.getInfoAsync(m3u8FilePath); + + return dirInfo.exists && fileInfo.exists; + } catch (error) { + console.error("Error checking download status:", error); + return false; + } }, enabled: !!item.Id, }); diff --git a/components/downloads/EpisodeCard.tsx b/components/downloads/EpisodeCard.tsx index 92ac061b..41e48df0 100644 --- a/components/downloads/EpisodeCard.tsx +++ b/components/downloads/EpisodeCard.tsx @@ -28,9 +28,18 @@ export const EpisodeCard: React.FC = ({ item }) => { const { startDownloadedFilePlayback } = usePlayback(); const handleOpenFile = useCallback(async () => { + const url = `${FileSystem.documentDirectory}${item.Id}/0.ts`; + console.log(url); + + const fileInfo = await FileSystem.getInfoAsync(url); + + if (!fileInfo.exists) { + console.warn("m3u8 file does not exist:", url); + } + startDownloadedFilePlayback({ item, - url: `${FileSystem.documentDirectory}/${item.Id}.mp4`, + url, }); router.push("/play"); }, [item, startDownloadedFilePlayback]); diff --git a/hooks/useDownloadM3U8Files.ts b/hooks/useDownloadM3U8Files.ts index 65b62911..f3e1b909 100644 --- a/hooks/useDownloadM3U8Files.ts +++ b/hooks/useDownloadM3U8Files.ts @@ -134,6 +134,7 @@ async function createLocalM3U8File(segments: Segment[], directoryPath: string) { localM3U8Content += "#EXT-X-MEDIA-SEQUENCE:0\n"; segments.forEach((segment, index) => { + console.log(segment.path.split(".")[1]); localM3U8Content += `#EXTINF:${segment.duration.toFixed(3)},\n`; localM3U8Content += `${directoryPath}/${index}.ts\n`; });