fix: handle corrupted download paths missing leading slash
This commit is contained in:
@@ -311,27 +311,41 @@ class DownloadManagerService {
|
||||
|
||||
// One-time migration: normalize stored file paths that may contain a
|
||||
// doubled base-dir prefix from an earlier bug in the recovery callback.
|
||||
// Re-run on v2 to also fix paths without a leading / that the v1 migration missed.
|
||||
final prefs = (await SettingsService.getInstance()).prefs;
|
||||
if (!(prefs.getBool('download_paths_normalized') ?? false)) {
|
||||
if ((prefs.getInt('download_paths_normalized_version') ?? 0) < 2) {
|
||||
final allItems = await _database.select(_database.downloadedMedia).get();
|
||||
var fixed = 0;
|
||||
for (final item in allItems) {
|
||||
if (item.videoFilePath != null) {
|
||||
final normalized = await _storageService.toRelativePath(item.videoFilePath!);
|
||||
if (normalized != item.videoFilePath) {
|
||||
final vfp = item.videoFilePath!;
|
||||
var normalized = await _storageService.toRelativePath(vfp);
|
||||
// If toRelativePath didn't help, try extracting from downloads/ onward
|
||||
// for paths that lack a leading / but contain nested base-dir fragments
|
||||
if (normalized == vfp) {
|
||||
final idx = vfp.indexOf('downloads/');
|
||||
if (idx > 0) normalized = vfp.substring(idx);
|
||||
}
|
||||
appLogger.d('Path migration: videoFilePath="$vfp", normalized="$normalized"');
|
||||
if (normalized != vfp) {
|
||||
await _database.updateVideoFilePath(item.globalKey, normalized);
|
||||
fixed++;
|
||||
}
|
||||
}
|
||||
if (item.thumbPath != null) {
|
||||
final normalized = await _storageService.toRelativePath(item.thumbPath!);
|
||||
if (normalized != item.thumbPath) {
|
||||
final tp = item.thumbPath!;
|
||||
var normalized = await _storageService.toRelativePath(tp);
|
||||
if (normalized == tp) {
|
||||
final idx = tp.indexOf('downloads/');
|
||||
if (idx > 0) normalized = tp.substring(idx);
|
||||
}
|
||||
if (normalized != tp) {
|
||||
await _database.updateArtworkPaths(globalKey: item.globalKey, thumbPath: normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fixed > 0) appLogger.i('Normalized $fixed corrupted download path(s)');
|
||||
await prefs.setBool('download_paths_normalized', true);
|
||||
await prefs.setInt('download_paths_normalized_version', 2);
|
||||
}
|
||||
|
||||
// Scan drift for orphaned items stuck in 'downloading'
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import '../models/plex_metadata.dart';
|
||||
import '../utils/app_logger.dart';
|
||||
import '../utils/formatters.dart';
|
||||
import 'settings_service.dart';
|
||||
import 'saf_storage_service.dart';
|
||||
@@ -398,25 +399,42 @@ class DownloadStorageService {
|
||||
}
|
||||
|
||||
/// Convert a potentially absolute path (from old database entries) to absolute
|
||||
/// This handles both old absolute paths and new relative paths
|
||||
/// This handles both old absolute paths and new relative paths, including
|
||||
/// corrupted paths that contain nested base-dir fragments without a leading slash
|
||||
/// (e.g. "data/user/0/.../app_flutter/downloads/...").
|
||||
Future<String> ensureAbsolutePath(String storedPath) async {
|
||||
appLogger.d('ensureAbsolutePath: input="$storedPath", isAbsolute=${path.isAbsolute(storedPath)}');
|
||||
|
||||
String result;
|
||||
if (path.isAbsolute(storedPath)) {
|
||||
// Already absolute - check if file exists at this path
|
||||
if (await File(storedPath).exists()) {
|
||||
return storedPath;
|
||||
result = storedPath;
|
||||
} else {
|
||||
// File doesn't exist at absolute path - try to reconstruct
|
||||
// Extract the relative portion (everything after 'downloads/')
|
||||
final downloadsIndex = storedPath.indexOf('downloads/');
|
||||
if (downloadsIndex != -1) {
|
||||
final relativePart = storedPath.substring(downloadsIndex);
|
||||
result = await toAbsolutePath(relativePart);
|
||||
} else {
|
||||
// Can't reconstruct, return original
|
||||
result = storedPath;
|
||||
}
|
||||
}
|
||||
// File doesn't exist at absolute path - try to reconstruct
|
||||
// Extract the relative portion (everything after 'downloads/')
|
||||
} else {
|
||||
// Relative path — if it contains a nested base-dir fragment
|
||||
// (e.g. "data/.../app_flutter/downloads/..."), extract from downloads/ onward
|
||||
final downloadsIndex = storedPath.indexOf('downloads/');
|
||||
if (downloadsIndex != -1) {
|
||||
final relativePart = storedPath.substring(downloadsIndex);
|
||||
return await toAbsolutePath(relativePart);
|
||||
if (downloadsIndex > 0) {
|
||||
result = await toAbsolutePath(storedPath.substring(downloadsIndex));
|
||||
} else {
|
||||
result = await toAbsolutePath(storedPath);
|
||||
}
|
||||
// Can't reconstruct, return original
|
||||
return storedPath;
|
||||
}
|
||||
// Relative path - convert to absolute
|
||||
return await toAbsolutePath(storedPath);
|
||||
|
||||
appLogger.d('ensureAbsolutePath: resolved="$result"');
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Calculate total storage used by downloads
|
||||
|
||||
Reference in New Issue
Block a user