This commit is contained in:
Fredrik Burmester
2024-09-27 17:35:51 +02:00
parent 41d209f3b7
commit a46737442d
4 changed files with 63 additions and 30 deletions

View File

@@ -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`);
}

View File

@@ -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<DownloadProps> = ({ item, ...props }) => {
const [settings] = useSettings();
// const { startRemuxing } = useRemuxHlsToMp4(item);
const { cancelDownload, startBackgroundDownload } =
useDownloadM3U8Files(item);
const { startBackgroundDownload } = useDownloadM3U8Files(item);
const [selectedMediaSource, setSelectedMediaSource] =
useState<MediaSourceInfo | null>(null);
@@ -175,13 +175,36 @@ export const DownloadItem: React.FC<DownloadProps> = ({ 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,
});

View File

@@ -28,9 +28,18 @@ export const EpisodeCard: React.FC<EpisodeCardProps> = ({ 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]);

View File

@@ -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`;
});