Limit download directory length. Close #311

This commit is contained in:
2025-04-01 15:49:22 +08:00
parent daa6e8ce18
commit 05fcb23a4d
3 changed files with 18 additions and 12 deletions

View File

@@ -461,6 +461,10 @@ class LocalManager with ChangeNotifier {
if (comic != null) {
return Directory(FilePath.join(path, comic.directory));
}
const comicDirectoryMaxLength = 128;
if (name.length > comicDirectoryMaxLength) {
name = name.substring(0, comicDirectoryMaxLength);
}
var dir = findValidDirectoryName(path, name);
return Directory(FilePath.join(path, dir)).create().then((value) => value);
}

View File

@@ -52,7 +52,7 @@ class _SearchBar extends StatelessWidget {
Widget build(BuildContext context) {
return SliverToBoxAdapter(
child: Container(
height: 52,
height: App.isMobile ? 52 : 46,
width: double.infinity,
margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: Material(

View File

@@ -1,4 +1,3 @@
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
@@ -132,25 +131,28 @@ extension DirectoryExtension on Directory {
}
/// Sanitize the file name. Remove invalid characters and trim the file name.
String sanitizeFileName(String fileName) {
String sanitizeFileName(String fileName, {String? dir, int? maxLength}) {
if (fileName.endsWith('.')) {
fileName = fileName.substring(0, fileName.length - 1);
}
const maxLength = 255;
var maxLength = 255;
if (dir != null) {
if (!dir.endsWith('/') && !dir.endsWith('\\')) {
dir = "$dir/";
}
maxLength -= dir.length;
}
final invalidChars = RegExp(r'[<>:"/\\|?*]');
final sanitizedFileName = fileName.replaceAll(invalidChars, ' ');
var trimmedFileName = sanitizedFileName.trim();
if (trimmedFileName.isEmpty) {
throw Exception('Invalid File Name: Empty length.');
}
while (true) {
final bytes = utf8.encode(trimmedFileName);
if (bytes.length > maxLength) {
trimmedFileName =
trimmedFileName.substring(0, trimmedFileName.length - 1);
} else {
break;
}
if (maxLength <= 0) {
throw Exception('Invalid File Name: Max length is less than 0.');
}
if (trimmedFileName.length > maxLength) {
trimmedFileName = trimmedFileName.substring(0, maxLength);
}
return trimmedFileName;
}