getBytesFromCID method
Given a compliant CID string, it fetches and caches that assets. NOTE: Because of limitations, this will skip the caching section if it is running from the web
Implementation
Future<Uint8List> getBytesFromCID(String cid) async {
// check for local existance of the file
if (!kIsWeb) {
try {
// only inits if cache dir is empty
(cacheDir == null || !cacheDir!.existsSync()) ? await init() : null;
File cidCache = await getCacheFile(cid);
if (cidCache.existsSync()) {
return cidCache.readAsBytesSync();
} else {
final Uint8List cidContents =
await s5.api.downloadRawFile(CID.decode(cid).hash);
if (cidContents.isNotEmpty) {
await cidCache.writeAsBytes(cidContents);
return cidContents;
}
}
} catch (e) {
print(e);
}
}
// if not, return the web fetched version
return s5.api.downloadRawFile(CID.decode(cid).hash);
}