content validation

This commit is contained in:
Adam Velebil 2023-02-28 12:09:02 +01:00
parent a4cc159f60
commit 2f2fca78ff
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
2 changed files with 42 additions and 20 deletions

View File

@ -44,37 +44,45 @@ class IconPackManager extends StateNotifier<AsyncValue<IconPack?>> {
void readPack() async {
final packDirectory = await _packDirectory;
final packFile = File(join(packDirectory.path, getLocalIconFileName('pack.json')));
final packFile =
File(join(packDirectory.path, getLocalIconFileName('pack.json')));
_log.debug('Looking for file: ${packFile.path}');
if (!await packFile.exists()) {
_log.debug('Failed to find icons pack ${packFile.path}');
state = AsyncValue.error(
'Failed to find icons pack ${packFile.path}', StackTrace.current);
'Failed to find icon pack ${packFile.path}', StackTrace.current);
return;
}
var packContent = await packFile.readAsString();
Map<String, dynamic> pack = const JsonDecoder().convert(packContent);
try {
var packContent = await packFile.readAsString();
Map<String, dynamic> pack = const JsonDecoder().convert(packContent);
final icons = List<IconPackIcon>.from(pack['icons'].map((icon) =>
IconPackIcon(
filename: icon['filename'],
category: icon['category'],
issuer: List<String>.from(icon['issuer'])
.map((e) => e.toUpperCase())
.toList(growable: false))));
final icons = List<IconPackIcon>.from(pack['icons'].map((icon) =>
IconPackIcon(
filename: icon['filename'],
category: icon['category'],
issuer: List<String>.from(icon['issuer'])
.map((e) => e.toUpperCase())
.toList(growable: false))));
state = AsyncValue.data(IconPack(
uuid: pack['uuid'],
name: pack['name'],
version: pack['version'],
directory: packDirectory,
icons: icons));
state = AsyncValue.data(IconPack(
uuid: pack['uuid'],
name: pack['name'],
version: pack['version'],
directory: packDirectory,
icons: icons));
_log.debug(
'Parsed ${state.value?.name} with ${state.value?.icons.length} icons');
_log.debug(
'Parsed ${state.value?.name} with ${state.value?.icons.length} icons');
} catch (e) {
_log.debug('Failed to parse icons pack ${packFile.path}');
state = AsyncValue.error(
'Failed to parse icon pack ${packFile.path}', StackTrace.current);
return;
}
}
Future<bool> importPack(AppLocalizations l10n, String filePath) async {
@ -121,7 +129,8 @@ class IconPackManager extends StateNotifier<AsyncValue<IconPack?>> {
}
// check that there is pack.json
final packJsonFile = File(join(unpackDirectory.path, getLocalIconFileName('pack.json')));
final packJsonFile =
File(join(unpackDirectory.path, getLocalIconFileName('pack.json')));
if (!await packJsonFile.exists()) {
_log.error('File is not an icon pack: missing pack.json');
_lastError = l10n.oath_custom_icons_err_invalid_icon_pack;
@ -130,6 +139,18 @@ class IconPackManager extends StateNotifier<AsyncValue<IconPack?>> {
return false;
}
// test pack.json
try {
var packContent = await packJsonFile.readAsString();
const JsonDecoder().convert(packContent);
} catch (e) {
_log.error('Failed to parse pack.json: $e');
_lastError = l10n.oath_custom_icons_err_invalid_icon_pack;
state = AsyncValue.error('File is not an icon pack', StackTrace.current);
await _deleteDirectory(tempDirectory);
return false;
}
// remove old icons pack and icon pack cache
final packDirectory = await _packDirectory;
if (!await _deleteDirectory(packDirectory)) {

View File

@ -97,6 +97,7 @@ class AccountIcon extends ConsumerWidget {
alignment: Alignment.center,
width: _width,
height: _height,
errorBuilder: (_, __, ___) => defaultWidget,
),
);
}