2023-05-09 04:49:20 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2023-05-01 13:13:40 -04:00
|
|
|
import 'package:contacts_plus_plus/config.dart';
|
2023-05-02 12:22:18 -04:00
|
|
|
import 'package:html/parser.dart' as htmlparser;
|
2023-05-09 04:49:20 -04:00
|
|
|
import 'package:uuid/uuid.dart';
|
2023-04-30 03:01:59 -04:00
|
|
|
|
|
|
|
enum NeosDBEndpoint
|
|
|
|
{
|
|
|
|
def,
|
|
|
|
blob,
|
|
|
|
cdn,
|
|
|
|
videoCDN,
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:14:29 -04:00
|
|
|
class Aux {
|
2023-05-04 13:04:33 -04:00
|
|
|
static String neosDbToHttp(String? neosdb) {
|
|
|
|
if (neosdb == null || neosdb.isEmpty) return "";
|
2023-05-05 12:02:43 -04:00
|
|
|
if (neosdb.startsWith("http")) return neosdb;
|
2023-04-30 17:14:29 -04:00
|
|
|
final fullUri = neosdb.replaceFirst("neosdb:///", Config.neosCdnUrl);
|
|
|
|
final lastPeriodIndex = fullUri.lastIndexOf(".");
|
|
|
|
if (lastPeriodIndex != -1 && fullUri.length - lastPeriodIndex < 8) {
|
|
|
|
// I feel like 8 is a good maximum for file extension length? Can neosdb Uris even come without file extensions?
|
|
|
|
return fullUri.substring(0, lastPeriodIndex);
|
|
|
|
}
|
|
|
|
return fullUri;
|
|
|
|
}
|
2023-05-09 04:49:20 -04:00
|
|
|
|
|
|
|
static String toURLBase64(Uint8List data) => base64.encode(data)
|
|
|
|
.replaceAll("+", "-")
|
|
|
|
.replaceAll("/", "_")
|
|
|
|
.replaceAll("=", "");
|
|
|
|
|
|
|
|
static String generateMachineId() => Aux.toURLBase64((const Uuid().v1obj().toBytes())).toLowerCase();
|
2023-05-01 12:50:54 -04:00
|
|
|
}
|
2023-05-02 04:04:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
extension Unique<E, Id> on List<E> {
|
|
|
|
List<E> unique([Id Function(E element)? id, bool inplace = true]) {
|
|
|
|
final ids = <Id>{};
|
|
|
|
var list = inplace ? this : List<E>.from(this);
|
|
|
|
list.retainWhere((x) => ids.add(id != null ? id(x) : x as Id));
|
|
|
|
return list;
|
|
|
|
}
|
2023-05-02 12:22:18 -04:00
|
|
|
}
|
|
|
|
|
2023-05-05 05:29:54 -04:00
|
|
|
extension Strip on String {
|
2023-05-02 12:22:18 -04:00
|
|
|
String stripHtml() {
|
|
|
|
final document = htmlparser.parse(this);
|
|
|
|
return htmlparser.parse(document.body?.text).documentElement?.text ?? "";
|
|
|
|
}
|
2023-05-05 05:29:54 -04:00
|
|
|
|
2023-05-07 06:53:19 -04:00
|
|
|
// This won't be accurate since userIds can't contain certain characters that usernames can
|
|
|
|
// but it's fine for just having a name to display
|
2023-05-05 05:29:54 -04:00
|
|
|
String stripUid() => startsWith("U-") ? substring(2) : this;
|
2023-05-02 15:34:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Format on Duration {
|
|
|
|
String format() {
|
|
|
|
final hh = (inHours).toString().padLeft(2, '0');
|
|
|
|
final mm = (inMinutes % 60).toString().padLeft(2, '0');
|
|
|
|
final ss = (inSeconds % 60).toString().padLeft(2, '0');
|
|
|
|
if (inHours == 0) {
|
|
|
|
return "$mm:$ss";
|
|
|
|
} else {
|
|
|
|
return "$hh:$mm:$ss";
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 04:04:54 -04:00
|
|
|
}
|