mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 06:43:42 +00:00
feat(download): auto-categorize downloads into folders by file extension (#1572)
This commit is contained in:
1 parent
d125d4ab75
commit
9aae13453a
55 files changed
+4403
-217
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
part 'downloader_config.g.dart';
|
||||
|
||||
@@ -17,12 +18,16 @@ class DownloaderConfig {
|
||||
ApiServerConfig api = ApiServerConfig();
|
||||
bool autoStartTasks;
|
||||
bool autoDeleteMissingFileTasks;
|
||||
List<DownloadCategory> categories;
|
||||
bool autoCategorize;
|
||||
|
||||
DownloaderConfig({
|
||||
this.downloadDir = '',
|
||||
this.maxRunning = 0,
|
||||
this.autoStartTasks = false,
|
||||
this.autoDeleteMissingFileTasks = false,
|
||||
this.categories = const [],
|
||||
this.autoCategorize = false,
|
||||
});
|
||||
|
||||
factory DownloaderConfig.fromJson(Map<String, dynamic> json) => _$DownloaderConfigFromJson(json);
|
||||
@@ -192,7 +197,6 @@ class ExtraConfig {
|
||||
bool runAsMenubarApp;
|
||||
bool analyticsEnabled;
|
||||
String analyticsClientId;
|
||||
List<DownloadCategory> downloadCategories;
|
||||
|
||||
ExtraConfig({
|
||||
this.themeMode = '',
|
||||
@@ -211,7 +215,6 @@ class ExtraConfig {
|
||||
this.runAsMenubarApp = false,
|
||||
this.analyticsEnabled = true,
|
||||
this.analyticsClientId = '',
|
||||
this.downloadCategories = const [],
|
||||
}) : windowState = windowState ?? WindowStateConfig();
|
||||
|
||||
factory ExtraConfig.fromJson(Map<String, dynamic>? json) =>
|
||||
@@ -241,6 +244,9 @@ class DownloadCategory {
|
||||
bool isBuiltIn;
|
||||
String? nameKey; // i18n key for built-in categories (e.g., 'categoryMusic')
|
||||
bool isDeleted; // Mark built-in categories as deleted instead of removing them
|
||||
// File extensions routed to this category, without leading dots, e.g. ['exe', 'msi'].
|
||||
// An empty list means the category never matches automatically.
|
||||
List<String> extensions;
|
||||
|
||||
DownloadCategory({
|
||||
required this.name,
|
||||
@@ -248,13 +254,93 @@ class DownloadCategory {
|
||||
this.isBuiltIn = false,
|
||||
this.nameKey,
|
||||
this.isDeleted = false,
|
||||
this.extensions = const [],
|
||||
});
|
||||
|
||||
factory DownloadCategory.fromJson(Map<String, dynamic> json) => _$DownloadCategoryFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$DownloadCategoryToJson(this);
|
||||
|
||||
/// The effective extension list. The result is always normalized: split on
|
||||
/// commas/whitespace, trimmed, lowercased, without leading dots and without
|
||||
/// duplicates; the original order is preserved. An empty list means the
|
||||
/// category never matches automatically.
|
||||
List<String> effectiveExtensions() => normalizeCategoryExtensions(extensions);
|
||||
}
|
||||
|
||||
/// Lowercases the ascii letters of [value] and leaves the rest alone. The
|
||||
/// backend lowercases the same way, because the unicode case tables of the two
|
||||
/// languages disagree for some code points and would let the two sides pick a
|
||||
/// different category for the same file.
|
||||
String asciiLower(String value) {
|
||||
final lowered = StringBuffer();
|
||||
for (final unit in value.codeUnits) {
|
||||
lowered.writeCharCode(unit >= 0x41 && unit <= 0x5A ? unit + 0x20 : unit);
|
||||
}
|
||||
return lowered.toString();
|
||||
}
|
||||
|
||||
/// Normalizes free form extension input into tokens: splits on commas and
|
||||
/// whitespace, trims, lowercases, strips leading dots, drops empties and
|
||||
/// deduplicates while keeping the original input order.
|
||||
List<String> normalizeCategoryExtensions(Iterable<String> raw) {
|
||||
final result = <String>{};
|
||||
for (final item in raw) {
|
||||
for (final part in item.split(RegExp(r'[,\s]+'))) {
|
||||
var extension = asciiLower(part.trim());
|
||||
while (extension.startsWith('.')) {
|
||||
extension = extension.substring(1);
|
||||
}
|
||||
if (extension.isNotEmpty) {
|
||||
result.add(extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toList();
|
||||
}
|
||||
|
||||
/// Default extension lists of the built-in categories, keyed by the i18n
|
||||
/// nameKey. Used to seed the built-in categories on first launch and to offer
|
||||
/// the restore-defaults action in the category dialog.
|
||||
const kDefaultCategoryExtensions = <String, List<String>>{
|
||||
'categoryProgram': ['exe', 'msi', 'msix', 'apk', 'dmg', 'deb', 'rpm', 'pkg', 'appimage'],
|
||||
'categoryVideo': ['mp4', 'mkv', 'avi', 'mov', 'wmv', 'flv', 'webm', 'm4v', 'mpg', 'mpeg', '3gp'],
|
||||
'categoryMusic': ['mp3', 'flac', 'wav', 'aac', 'ogg', 'm4a', 'wma', 'ape'],
|
||||
'categoryDocument': ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'md', 'csv', 'epub', 'rtf'],
|
||||
};
|
||||
|
||||
/// Rebases built-in category paths that still live under [oldDir] onto
|
||||
/// [newDir] after the default download directory changed. Categories the
|
||||
/// user moved elsewhere, and user created categories, are left alone.
|
||||
void rebaseBuiltinCategoryPaths(DownloaderConfig config, {required String oldDir, required String newDir}) {
|
||||
if (oldDir.trim().isEmpty || newDir.trim().isEmpty) {
|
||||
return;
|
||||
}
|
||||
// The backend seeds the categories with the separator of its platform, which
|
||||
// the path package of the client does not always understand, so both sides
|
||||
// are compared in the forward slash form.
|
||||
final from = _forwardSlash(oldDir);
|
||||
final to = _forwardSlash(newDir);
|
||||
if (path.equals(from, to)) {
|
||||
return;
|
||||
}
|
||||
for (final category in config.categories) {
|
||||
if (!category.isBuiltIn || category.isDeleted || category.path.trim().isEmpty) {
|
||||
continue;
|
||||
}
|
||||
final current = _forwardSlash(category.path);
|
||||
if (path.equals(current, from)) {
|
||||
category.path = to;
|
||||
} else if (path.isWithin(from, current)) {
|
||||
category.path = path.join(to, path.relative(current, from: from));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewrites the windows separator of [value] to the forward slash the path
|
||||
/// package uses.
|
||||
String _forwardSlash(String value) => value.replaceAll('\\', '/');
|
||||
|
||||
@JsonSerializable()
|
||||
class WebhookConfig {
|
||||
bool enable;
|
||||
|
||||
Reference in new issue
Block a user