mirror of
https://github.com/wgh136/pixes.git
synced 2025-09-27 04:57:23 +00:00
improve download subpath
This commit is contained in:
@@ -16,8 +16,6 @@ class _Appdata {
|
||||
Map<String, dynamic> settings = {
|
||||
"downloadPath": null,
|
||||
"downloadSubPath": r"/${id}-p${index}.${ext}",
|
||||
"tagsWeight": "風景 ロリ 巨乳 女の子",
|
||||
"useTranslatedNameForDownload": true,
|
||||
"maxParallels": 3,
|
||||
"proxy": "",
|
||||
};
|
||||
|
@@ -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() {
|
||||
|
@@ -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}
|
||||
""";
|
||||
}
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user