Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
56 lines
No EOL
1.3 KiB
Dart
56 lines
No EOL
1.3 KiB
Dart
// Extensions and utilities for PackManager
|
|
|
|
import 'dart:typed_data';
|
|
import 'dart:io';
|
|
import 'dart:convert';
|
|
import 'package:image/image.dart';
|
|
import 'pack_manager.dart';
|
|
|
|
enum _ImageSize {
|
|
big,
|
|
medium,
|
|
small,
|
|
extraSmall,
|
|
}
|
|
|
|
extension on _ImageSize {
|
|
int get width => switch (this) {
|
|
_ImageSize.big => 1024,
|
|
_ImageSize.medium => 512,
|
|
_ImageSize.small => 320,
|
|
_ImageSize.extraSmall => 192,
|
|
};
|
|
}
|
|
|
|
extension CoverStringExt on String? {
|
|
Future<String> get base64Image => _base64Image();
|
|
|
|
Future<String> _base64Image([_ImageSize? size]) async {
|
|
if (this == null) return '';
|
|
try {
|
|
final bytes = _getById(this!, 'covers', size);
|
|
return base64Encode(bytes);
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
Uint8List _getById(String id, String type, _ImageSize? size) {
|
|
try {
|
|
return File('${PackManagerUtils.assetsDirectory.path}/$type/$id')
|
|
.readAsBytesSync();
|
|
} catch (e) {
|
|
print('Error loading image $id of type $type: $e');
|
|
return Uint8List(0);
|
|
}
|
|
}
|
|
|
|
Future<String> get smallBase64Image => _base64Image(_ImageSize.small);
|
|
|
|
Future<String> get extraSmallBase64Image =>
|
|
_base64Image(_ImageSize.extraSmall);
|
|
|
|
Future<String> get mediumBase64Image => _base64Image(_ImageSize.medium);
|
|
|
|
Future<String> get bigBase64Image => _base64Image(null);
|
|
} |