diff --git a/assets/tr.json b/assets/tr.json index e74989a..52a388b 100644 --- a/assets/tr.json +++ b/assets/tr.json @@ -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": "多個路徑分隔符號將自動替換為單一" } } \ No newline at end of file diff --git a/lib/appdata.dart b/lib/appdata.dart index c228b55..5e2fb12 100644 --- a/lib/appdata.dart +++ b/lib/appdata.dart @@ -16,8 +16,6 @@ class _Appdata { Map settings = { "downloadPath": null, "downloadSubPath": r"/${id}-p${index}.${ext}", - "tagsWeight": "風景 ロリ 巨乳 女の子", - "useTranslatedNameForDownload": true, "maxParallels": 3, "proxy": "", }; diff --git a/lib/network/download.dart b/lib/network/download.dart index 7ff65c2..6332919 100644 --- a/lib/network/download.dart +++ b/lib/network/download.dart @@ -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.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 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() { diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart index 0c5b088..756a044 100644 --- a/lib/pages/settings_page.dart +++ b/lib/pages/settings_page.dart @@ -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} """; } diff --git a/lib/utils/io.dart b/lib/utils/io.dart index 51af035..3624348 100644 --- a/lib/utils/io.dart +++ b/lib/utils/io.dart @@ -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;