worldhopper/lib/services/epub_download_service.dart
2026-04-17 11:45:41 +02:00

154 lines
4.3 KiB
Dart

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:worldhopper/models/entry.dart';
import 'package:worldhopper/models/server.dart';
import 'package:worldhopper/services/auth_service.dart';
/// Service for downloading and caching EPUB files
class EpubDownloadService {
final AuthService _authService;
final CacheManager _cacheManager;
EpubDownloadService({
AuthService? authService,
CacheManager? cacheManager,
}) : _authService = authService ?? AuthService(),
_cacheManager = cacheManager ?? DefaultCacheManager();
/// Download EPUB file from server with authentication
/// Returns the local file path of the downloaded EPUB
Future<File> downloadEpub(
Entry entry,
Server server, {
String? savePath,
void Function(double progress)? onProgress,
CancelToken? cancelToken,
}) async {
final acquisitionLink = entry.acquisitionLink;
if (acquisitionLink == null) {
throw Exception('No acquisition link found for entry: ${entry.title}');
}
final url = acquisitionLink.href;
if (url.isEmpty) {
throw Exception('Invalid acquisition link URL');
}
// When a specific savePath is requested, bypass the in-memory cache and
// write directly — the caller owns the destination file's lifecycle.
if (savePath != null) {
final dio = _authService.createAuthenticatedClient(server);
await dio.download(
url,
savePath,
cancelToken: cancelToken,
onReceiveProgress: (received, total) {
if (total != -1 && onProgress != null) {
onProgress(received / total);
}
},
);
return File(savePath);
}
final fileInfo = await _cacheManager.getFileFromCache(url);
if (fileInfo != null && await fileInfo.file.exists()) {
return fileInfo.file;
}
final dio = _authService.createAuthenticatedClient(server);
final cacheDir = await getTemporaryDirectory();
final fileName = '${entry.id}.epub';
final filePath = path.join(cacheDir.path, fileName);
final file = File(filePath);
await dio.download(
url,
filePath,
cancelToken: cancelToken,
onReceiveProgress: (received, total) {
if (total != -1 && onProgress != null) {
final progress = received / total;
onProgress(progress);
}
},
);
await _cacheManager.putFile(
url,
await file.readAsBytes(),
fileExtension: 'epub',
);
return file;
}
/// Get cached EPUB file if it exists
Future<File?> getCachedEpub(Entry entry) async {
final acquisitionLink = entry.acquisitionLink;
if (acquisitionLink == null) return null;
final url = acquisitionLink.href;
if (url.isEmpty) return null;
final fileInfo = await _cacheManager.getFileFromCache(url);
if (fileInfo != null && await fileInfo.file.exists()) {
return fileInfo.file;
}
return null;
}
/// Remove cached EPUB file
Future<void> removeCachedEpub(Entry entry) async {
final acquisitionLink = entry.acquisitionLink;
if (acquisitionLink == null) return;
final url = acquisitionLink.href;
if (url.isEmpty) return;
await _cacheManager.removeFile(url);
}
/// Check if EPUB is cached
Future<bool> isEpubCached(Entry entry) async {
final acquisitionLink = entry.acquisitionLink;
if (acquisitionLink == null) return false;
final url = acquisitionLink.href;
if (url.isEmpty) return false;
final fileInfo = await _cacheManager.getFileFromCache(url);
return fileInfo != null && await fileInfo.file.exists();
}
/// Get download progress for an EPUB
Stream<double> getDownloadProgress(Entry entry, Server server) async* {
final acquisitionLink = entry.acquisitionLink;
if (acquisitionLink == null) {
throw Exception('No acquisition link found for entry: ${entry.title}');
}
final url = acquisitionLink.href;
if (url.isEmpty) {
throw Exception('Invalid acquisition link URL');
}
double progress = 0.0;
yield progress;
await downloadEpub(
entry,
server,
onProgress: (p) {
progress = p;
},
);
yield progress;
}
}