improve download subpath

This commit is contained in:
wgh136
2024-05-16 10:55:49 +08:00
parent 945d386d17
commit 1698928212
5 changed files with 39 additions and 52 deletions

View File

@@ -106,7 +106,10 @@
"Popular(Female)": "热门(女性向)",
"Start Time": "开始时间",
"End Time": "结束时间",
"Max parallels": "最大并行数"
"Max parallels": "最大并行数",
"Replace with 'AI' if the work was generated by AI, otherwise replace with blank": "替换为'AI'如果作品是由AI生成的, 否则替换为空白",
"Replace with * if the work have tag *, otherwise replace with blank.": "替换为*如果作品包含标签*, 否则替换为空白",
"Multiple path separators will be automatically replaced with a single": "多个路径分隔符将被自动替换为单个"
},
"zh_TW": {
"Search": "搜索",
@@ -215,6 +218,9 @@
"Popular(Female)": "熱門(女性)",
"Start Time": "開始時間",
"End Time": "結束時間",
"Max parallels": "最大並行數"
"Max parallels": "最大並行數",
"Replace with 'AI' if the work was generated by AI, otherwise replace with blank": "替換為'AI'如果作品是由AI生成的, 否則替換為空白",
"Replace with * if the work have tag *, otherwise replace with blank.": "替換為*如果作品包含標籤*, 否則替換為空白",
"Multiple path separators will be automatically replaced with a single": "多個路徑分隔符號將自動替換為單一"
}
}

View File

@@ -16,8 +16,6 @@ class _Appdata {
Map<String, dynamic> settings = {
"downloadPath": null,
"downloadSubPath": r"/${id}-p${index}.${ext}",
"tagsWeight": "風景 ロリ 巨乳 女の子",
"useTranslatedNameForDownload": true,
"maxParallels": 3,
"proxy": "",
};

View File

@@ -127,28 +127,37 @@ class DownloadingTask {
static String _generateFilePath(Illust illust, int index, String ext) {
final String downloadPath = appdata.settings["downloadPath"];
String subPathPatten = appdata.settings["downloadSubPath"];
final tagsWeight = (appdata.settings["tagsWeight"] as String).split(' ');
final originalTags = List<Tag>.from(illust.tags);
originalTags.sort((a, b){
var aWeight = tagsWeight.indexOf(a.name);
if(aWeight == -1) aWeight = tagsWeight.length;
var bWeight = tagsWeight.indexOf(b.name);
if(bWeight == -1) bWeight = tagsWeight.length;
return aWeight - bWeight;
});
final tags = appdata.settings["useTranslatedNameForDownload"] == false
? originalTags.map((e) => e.name).toList()
: originalTags.map((e) => e.translatedName ?? e.name).toList();
subPathPatten = subPathPatten.replaceAll(r"${id}", illust.id.toString());
subPathPatten = subPathPatten.replaceAll(r"${title}", illust.title);
subPathPatten = subPathPatten.replaceAll(r"${author}", illust.author.name);
subPathPatten = subPathPatten.replaceAll(r"${index}", index.toString());
subPathPatten = subPathPatten.replaceAll(r"${ext}", ext);
for(int i=0; i<tags.length; i++) {
subPathPatten = subPathPatten.replaceAll("\${tag$i}", tags[i]);
subPathPatten = subPathPatten.replaceAll(r"${AI}", illust.isAi ? "AI" : "");
List<String> extractTags(String input) {
final regex = RegExp(r'\$\{tag\((.*?)\)\}');
final matches = regex.allMatches(input);
return matches.map((match) => match.group(1)!).toList();
}
return "$downloadPath$subPathPatten";
var tags = extractTags(subPathPatten);
print(illust.tags);
for(var tag in tags) {
print(tag);
if (illust.tags.where((e) => e.name == tag || e.translatedName == tag).isNotEmpty) {
subPathPatten = subPathPatten.replaceAll("\${tag($tag)}", tag);
}
}
return _cleanFilePath("$downloadPath$subPathPatten");
}
static String _cleanFilePath(String filePath) {
const invalidChars = ['*', '?', '"', '<', '>', '|'];
String cleanedPath =
filePath.replaceAll(RegExp('[${invalidChars.join(' ')}]'), '');
cleanedPath = cleanedPath.replaceAll(RegExp(r'[/\\]+'), '/');
return cleanedPath;
}
void retry() {

View File

@@ -282,8 +282,6 @@ class _SetDownloadSubPathPage extends StatefulWidget {
class __SetDownloadSubPathPageState extends State<_SetDownloadSubPathPage> {
final controller =
TextEditingController(text: appdata.settings["downloadSubPath"]);
final controller2 =
TextEditingController(text: appdata.settings["tagsWeight"]);
@override
Widget build(BuildContext context) {
@@ -297,26 +295,6 @@ class __SetDownloadSubPathPageState extends State<_SetDownloadSubPathPage> {
TextBox(
controller: controller,
).paddingHorizontal(16),
Text("Weights of the tags".tl)
.padding(const EdgeInsets.symmetric(vertical: 8, horizontal: 16)),
TextBox(
controller: controller2,
).paddingHorizontal(16),
const SizedBox(
height: 8,
),
ListTile(
title: Text("Use translated tag name".tl),
trailing: ToggleSwitch(
checked: appdata.settings["useTranslatedNameForDownload"],
onChanged: (value) {
setState(() {
appdata.settings["useTranslatedNameForDownload"] = value;
});
appdata.writeSettings();
},
),
),
const SizedBox(
height: 8,
),
@@ -326,7 +304,6 @@ class __SetDownloadSubPathPageState extends State<_SetDownloadSubPathPage> {
var text = controller.text;
if (check(text)) {
appdata.settings["downloadSubPath"] = text;
appdata.settings["tagsWeight"] = controller2.text;
appdata.writeData();
context.pop();
} else {
@@ -360,15 +337,9 @@ ${"Some keywords will be replaced by the following rule:".tl}
\${id} -> ${"Artwork ID".tl}
\${index} -> ${"Index of the image in the artwork".tl}
\${ext} -> ${"File extension".tl}
\${AI} -> ${"Replace with 'AI' if the work was generated by AI, otherwise replace with blank".tl}
\${tag{*}} -> ${"Replace with * if the work have tag *, otherwise replace with blank.".tl}
${"Tags: Tags will be sorted by the \"Weights of tags\" setting and replaced by the following rule:".tl}
${"The final text will be affected by the \"Use translated tag name\" setting.".tl}
\${tag0} -> ${"The first tag of the artwork".tl}
\${tag1} -> ${"The second tag of the artwork".tl}
...
${"Weights of the tags".tl}:
${"Filled with tags. The tags should be separated by a space. The tag in front has higher weight.".tl}
${"It is required to use the original name instead of the translated name.".tl}
${"Multiple path separators will be automatically replaced with a single".tl}
""";
}

View File

@@ -29,6 +29,9 @@ extension FSExt on FileSystemEntity {
extension DirectoryExt on Directory {
bool havePermission() {
if(!existsSync()) return false;
if(App.isMacOS) {
return true;
}
try {
listSync();
return true;