Surface/lib/providers/link_preview.dart
LittleSheep 80a66136ce Link preview in posts
🐛 Fix link preview icon bugged when site icon is svg
2024-12-14 15:21:34 +08:00

36 lines
959 B
Dart

import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:surface/providers/sn_network.dart';
import 'package:surface/types/link.dart';
class SnLinkPreviewProvider {
late final SnNetworkProvider _sn;
final Map<String, SnLinkMeta> _cache = {};
SnLinkPreviewProvider(BuildContext context) {
_sn = context.read<SnNetworkProvider>();
}
Future<SnLinkMeta?> getLinkMeta(String url) async {
final b64 = utf8.fuse(base64Url);
final target = b64.encode(url);
if (_cache.containsKey(target)) return _cache[target];
log('[LinkPreview] Fetching $url ($target)');
try {
final resp = await _sn.client.get('/cgi/re/link/$target');
final meta = SnLinkMeta.fromJson(resp.data);
_cache[url] = meta;
return meta;
} catch (err) {
log('[LinkPreview] Failed to fetch $url ($target)...');
return null;
}
}
}