import 'package:epubx/epubx.dart'; import '../models/book.dart'; import 'dart:io'; //解析epub文件的服务类 class EpubParserService { Future parseEpubFile(String filePath) async { try { final bytes = await File(filePath).readAsBytes(); return await EpubReader.readBook(bytes); } catch (e) { throw Exception('EPUB解析失败: $e'); } } Future extractBookMetadata(EpubBook epubBook, String filePath) async { try { // 基本信息 final title = epubBook.Title ?? '未知标题'; // 处理作者列表 final authorList = epubBook.AuthorList?.map((author) => author.toString()).toList() ?? []; final primaryAuthor = authorList.isNotEmpty ? authorList.first : '未知作者'; // 计算章节数 final chapters = epubBook.Chapters ?? []; final chapterCount = chapters.isNotEmpty ? chapters.length : null; // 如果有章节数,使用章节数作为totalPages final totalPages = chapterCount ?? 0; return Book.create( title: title, filePath: filePath, format: BookFormat.epub, fileSize: await File(filePath).length(), author: primaryAuthor, authors: authorList, chapterCount: chapterCount, totalPages: totalPages, ); } catch (e) { throw Exception('提取元数据失败: $e'); } } Future saveCoverImage(EpubBook epubBook, String bookId) async { // 如果没有封面图片,返回null if (epubBook.CoverImage == null) return null; // TODO: 这里需要实现保存封面图片的逻辑 // 将封面图片保存到本地存储,并返回文件路径 return null; // 暂时返回null,后续完善 } }