Compare commits
No commits in common. "fe51811b39f9a73824394e6225e560a065fdac0d" and "5ed74aa5af7730226c1992b94e121a29cd87e0da" have entirely different histories.
fe51811b39
...
5ed74aa5af
@ -1,30 +1,31 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
typedef Validator = bool Function(String);
|
||||
String formatMountName(String type, String name) {
|
||||
if (type == 'remote') {
|
||||
return name.replaceAll('_', ':');
|
||||
}
|
||||
|
||||
bool containsRestrictedChar(String value) {
|
||||
const invalidChars = [
|
||||
'!',
|
||||
'"',
|
||||
'\$',
|
||||
'&',
|
||||
'\'',
|
||||
'(',
|
||||
')',
|
||||
'*',
|
||||
';',
|
||||
'<',
|
||||
'>',
|
||||
'?',
|
||||
'[',
|
||||
']',
|
||||
'`',
|
||||
'{',
|
||||
'}',
|
||||
'|',
|
||||
];
|
||||
return invalidChars.firstWhereOrNull((char) => value.contains(char)) != null;
|
||||
return name;
|
||||
}
|
||||
|
||||
String getBaseUri() {
|
||||
if (kDebugMode || !kIsWeb) {
|
||||
return 'http://127.0.0.1:30000';
|
||||
}
|
||||
|
||||
return Uri.base.origin;
|
||||
}
|
||||
|
||||
String initialCaps(String txt) {
|
||||
if (txt.isEmpty) {
|
||||
return txt;
|
||||
}
|
||||
|
||||
if (txt.length == 1) {
|
||||
return txt[0].toUpperCase();
|
||||
}
|
||||
|
||||
return txt[0].toUpperCase() + txt.substring(1).toLowerCase();
|
||||
}
|
||||
|
||||
Map<String, dynamic> createDefaultSettings(String mountType) {
|
||||
@ -51,7 +52,7 @@ Map<String, dynamic> createDefaultSettings(String mountType) {
|
||||
return {
|
||||
'HostConfig': {
|
||||
'ApiPassword': '',
|
||||
'ApiPort': 9980,
|
||||
'ApiPort': '9980',
|
||||
'HostNameOrIp': 'localhost',
|
||||
},
|
||||
'SiaConfig': {'Bucket': 'default'},
|
||||
@ -60,100 +61,3 @@ Map<String, dynamic> createDefaultSettings(String mountType) {
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
String formatMountName(String type, String name) {
|
||||
if (type == 'remote') {
|
||||
return name.replaceAll('_', ':');
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
String getBaseUri() {
|
||||
if (kDebugMode || !kIsWeb) {
|
||||
return 'http://127.0.0.1:30000';
|
||||
}
|
||||
|
||||
return Uri.base.origin;
|
||||
}
|
||||
|
||||
List<Validator> getSettingValidators(String settingPath) {
|
||||
switch (settingPath) {
|
||||
case 'EncryptConfig.EncryptionToken':
|
||||
return [(value) => value.isNotEmpty];
|
||||
case 'EncryptConfig.Path':
|
||||
return [
|
||||
(value) => value.trim().isNotEmpty,
|
||||
(value) => !containsRestrictedChar(value),
|
||||
];
|
||||
case 'HostConfig.ApiPassword':
|
||||
return [(value) => value.isNotEmpty];
|
||||
case 'HostConfig.ApiPort':
|
||||
return [
|
||||
(value) {
|
||||
int? intValue = int.tryParse(value);
|
||||
if (intValue == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (intValue > 0 && intValue < 65536);
|
||||
},
|
||||
(value) => Uri.tryParse('http://localhost:$value/') != null,
|
||||
];
|
||||
case 'HostConfig.HostNameOrIp':
|
||||
return [
|
||||
(value) => value.trim().isNotEmpty,
|
||||
(value) => Uri.tryParse('http://$value:9000/') != null,
|
||||
];
|
||||
case 'HostConfig.Protocol':
|
||||
return [(value) => value == "http" || value == "https"];
|
||||
case 'S3Config.AccessKey':
|
||||
return [(value) => value.isNotEmpty];
|
||||
case 'S3Config.Bucket':
|
||||
return [(value) => value.trim().isNotEmpty];
|
||||
case 'S3Config.SecretKey':
|
||||
return [(value) => value.isNotEmpty];
|
||||
case 'S3Config.URL':
|
||||
return [(value) => Uri.tryParse(value) != null];
|
||||
case 'SiaConfig.Bucket':
|
||||
return [(value) => value.trim().isNotEmpty];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
String initialCaps(String txt) {
|
||||
if (txt.isEmpty) {
|
||||
return txt;
|
||||
}
|
||||
|
||||
if (txt.length == 1) {
|
||||
return txt[0].toUpperCase();
|
||||
}
|
||||
|
||||
return txt[0].toUpperCase() + txt.substring(1).toLowerCase();
|
||||
}
|
||||
|
||||
bool validateSettings(
|
||||
Map<String, dynamic> settings,
|
||||
List<String> failed, {
|
||||
String? rootKey,
|
||||
}) {
|
||||
settings.forEach((key, value) {
|
||||
if (value is Map) {
|
||||
validateSettings(
|
||||
value as Map<String, dynamic>,
|
||||
failed,
|
||||
rootKey: rootKey == null ? key : '$rootKey.$key',
|
||||
);
|
||||
} else {
|
||||
for (var validator in getSettingValidators(key)) {
|
||||
if (!validator(value.toString())) {
|
||||
failed.add(rootKey == null ? key : '$rootKey.$key');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return failed.isEmpty;
|
||||
}
|
||||
|
@ -121,17 +121,11 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
||||
if (_mount != null)
|
||||
ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
List<String> failed = [];
|
||||
if (!validateSettings(_settings[_mountType]!, failed)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Provider.of<MountList>(context, listen: false).add(
|
||||
_mountType,
|
||||
_mountNameController.text,
|
||||
_settings[_mountType]!,
|
||||
);
|
||||
|
||||
Navigator.pop(context);
|
||||
},
|
||||
label: const Text('Add'),
|
||||
|
@ -2,7 +2,6 @@ import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:repertory/constants.dart';
|
||||
import 'package:repertory/helpers.dart' show Validator, getSettingValidators;
|
||||
import 'package:repertory/models/mount.dart';
|
||||
import 'package:settings_ui/settings_ui.dart';
|
||||
|
||||
@ -49,14 +48,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
void _addIntSetting(
|
||||
list,
|
||||
root,
|
||||
key,
|
||||
value,
|
||||
isAdvanced, {
|
||||
List<Validator> validators = const [],
|
||||
}) {
|
||||
void _addIntSetting(list, root, key, value, isAdvanced) {
|
||||
if (!isAdvanced || widget.showAdvanced) {
|
||||
list.add(
|
||||
SettingsTile.navigation(
|
||||
@ -77,15 +69,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
TextButton(
|
||||
child: Text('OK'),
|
||||
onPressed: () {
|
||||
final result = validators.firstWhereOrNull(
|
||||
(validator) => !validator(updatedValue),
|
||||
);
|
||||
if (result != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("'$key' is not valid")),
|
||||
);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
root[key] = int.parse(updatedValue);
|
||||
widget.onChanged?.call(widget.settings);
|
||||
@ -193,15 +176,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
void _addStringSetting(
|
||||
list,
|
||||
root,
|
||||
key,
|
||||
value,
|
||||
icon,
|
||||
isAdvanced, {
|
||||
List<Validator> validators = const [],
|
||||
}) {
|
||||
void _addStringSetting(list, root, key, value, icon, isAdvanced) {
|
||||
if (!isAdvanced || widget.showAdvanced) {
|
||||
list.add(
|
||||
SettingsTile.navigation(
|
||||
@ -222,15 +197,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
TextButton(
|
||||
child: Text('OK'),
|
||||
onPressed: () {
|
||||
final result = validators.firstWhereOrNull(
|
||||
(validator) => !validator(updatedValue),
|
||||
);
|
||||
if (result != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("'$key' is not valid")),
|
||||
);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
root[key] = updatedValue;
|
||||
widget.onChanged?.call(widget.settings);
|
||||
@ -268,14 +234,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
if (key == 'ApiAuth') {
|
||||
_addPasswordSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'ApiPort') {
|
||||
_addIntSetting(
|
||||
commonSettings,
|
||||
widget.settings,
|
||||
key,
|
||||
value,
|
||||
true,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
_addIntSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'ApiUser') {
|
||||
_addStringSetting(
|
||||
commonSettings,
|
||||
@ -284,7 +243,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
value,
|
||||
Icons.person,
|
||||
true,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
} else if (key == 'DatabaseType') {
|
||||
_addListSetting(
|
||||
@ -297,14 +255,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
true,
|
||||
);
|
||||
} else if (key == 'DownloadTimeoutSeconds') {
|
||||
_addIntSetting(
|
||||
commonSettings,
|
||||
widget.settings,
|
||||
key,
|
||||
value,
|
||||
true,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
_addIntSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'EnableDownloadTimeout') {
|
||||
_addBooleanSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'EnableDriveEvents') {
|
||||
@ -320,43 +271,15 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
false,
|
||||
);
|
||||
} else if (key == 'EvictionDelayMinutes') {
|
||||
_addIntSetting(
|
||||
commonSettings,
|
||||
widget.settings,
|
||||
key,
|
||||
value,
|
||||
true,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
_addIntSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'EvictionUseAccessedTime') {
|
||||
_addBooleanSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'MaxCacheSizeBytes') {
|
||||
_addIntSetting(
|
||||
commonSettings,
|
||||
widget.settings,
|
||||
key,
|
||||
value,
|
||||
false,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
_addIntSetting(commonSettings, widget.settings, key, value, false);
|
||||
} else if (key == 'MaxUploadCount') {
|
||||
_addIntSetting(
|
||||
commonSettings,
|
||||
widget.settings,
|
||||
key,
|
||||
value,
|
||||
true,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
_addIntSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'OnlineCheckRetrySeconds') {
|
||||
_addIntSetting(
|
||||
commonSettings,
|
||||
widget.settings,
|
||||
key,
|
||||
value,
|
||||
true,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
_addIntSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'PreferredDownloadType') {
|
||||
_addListSetting(
|
||||
commonSettings,
|
||||
@ -368,14 +291,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
false,
|
||||
);
|
||||
} else if (key == 'RetryReadCount') {
|
||||
_addIntSetting(
|
||||
commonSettings,
|
||||
widget.settings,
|
||||
key,
|
||||
value,
|
||||
true,
|
||||
validators: getSettingValidators(key),
|
||||
);
|
||||
_addIntSetting(commonSettings, widget.settings, key, value, true);
|
||||
} else if (key == 'RingBufferFileSize') {
|
||||
_addIntListSetting(
|
||||
commonSettings,
|
||||
@ -405,7 +321,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.folder,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -419,7 +334,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.support_agent,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'ApiPassword') {
|
||||
_addPasswordSetting(
|
||||
@ -436,7 +350,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'ApiUser') {
|
||||
_addStringSetting(
|
||||
@ -446,7 +359,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.person,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'HostNameOrIp') {
|
||||
_addStringSetting(
|
||||
@ -465,7 +377,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.route,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'Protocol') {
|
||||
_addListSetting(
|
||||
@ -484,7 +395,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -497,7 +407,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'EncryptionToken') {
|
||||
_addPasswordSetting(
|
||||
@ -515,7 +424,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.computer,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'MaxConnections') {
|
||||
_addIntSetting(
|
||||
@ -524,7 +432,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'ReceiveTimeoutMs') {
|
||||
_addIntSetting(
|
||||
@ -533,7 +440,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'SendTimeoutMs') {
|
||||
_addIntSetting(
|
||||
@ -542,7 +448,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -565,7 +470,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'ClientPoolSize') {
|
||||
_addIntSetting(
|
||||
@ -574,7 +478,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'EncryptionToken') {
|
||||
_addPasswordSetting(
|
||||
@ -604,7 +507,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.folder,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'EncryptionToken') {
|
||||
_addPasswordSetting(
|
||||
@ -622,7 +524,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.map,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'SecretKey') {
|
||||
_addPasswordSetting(
|
||||
@ -639,7 +540,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subKey,
|
||||
subValue,
|
||||
true,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'URL') {
|
||||
_addStringSetting(
|
||||
@ -649,7 +549,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.http,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
} else if (subKey == 'UsePathStyle') {
|
||||
_addBooleanSetting(
|
||||
@ -679,7 +578,6 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
subValue,
|
||||
Icons.folder,
|
||||
false,
|
||||
validators: getSettingValidators('$key.$subKey'),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user