2023-02-23 12:46:33 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-02-27 19:02:30 +03:00
|
|
|
import 'dart:convert';
|
2023-02-23 12:46:33 +03:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-02-27 19:02:30 +03:00
|
|
|
import 'package:crypto/crypto.dart';
|
2023-02-23 18:38:03 +03:00
|
|
|
import 'package:path/path.dart';
|
|
|
|
|
2023-02-27 19:02:30 +03:00
|
|
|
String getLocalIconFileName(String iconPackFileName) {
|
|
|
|
final sha = sha256.convert(utf8.encode(iconPackFileName)).toString();
|
|
|
|
return sha.substring(0, sha.length ~/ 2) + extension(iconPackFileName);
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:46:33 +03:00
|
|
|
class IconPackIcon {
|
|
|
|
final String filename;
|
|
|
|
final String? category;
|
|
|
|
final List<String> issuer;
|
|
|
|
|
|
|
|
const IconPackIcon({
|
|
|
|
required this.filename,
|
|
|
|
required this.category,
|
|
|
|
required this.issuer,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class IconPack {
|
|
|
|
final String uuid;
|
|
|
|
final String name;
|
|
|
|
final int version;
|
|
|
|
final Directory directory;
|
|
|
|
final List<IconPackIcon> icons;
|
|
|
|
|
|
|
|
const IconPack({
|
|
|
|
required this.uuid,
|
|
|
|
required this.name,
|
|
|
|
required this.version,
|
|
|
|
required this.directory,
|
|
|
|
required this.icons,
|
|
|
|
});
|
|
|
|
|
|
|
|
File? getFileForIssuer(String? issuer) {
|
|
|
|
if (issuer == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final matching = icons.where((element) =>
|
|
|
|
element.issuer.any((element) => element == issuer.toUpperCase()));
|
|
|
|
|
|
|
|
final issuerImageFile = matching.isNotEmpty
|
2023-02-27 19:02:30 +03:00
|
|
|
? File(join(directory.path, getLocalIconFileName(matching.first.filename)))
|
2023-02-23 12:46:33 +03:00
|
|
|
: null;
|
|
|
|
|
|
|
|
if (issuerImageFile != null && !issuerImageFile.existsSync()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return issuerImageFile;
|
|
|
|
}
|
|
|
|
}
|