pullCidFromIPFS method

Future<Uint8List> pullCidFromIPFS(
  1. String cid,
  2. String xxInt
)

Implementation

Future<Uint8List> pullCidFromIPFS(String cid, String xxInt) async {
  if (!online) return Uint8List(0);
  if (_brokenCIDs.contains(cid)) Uint8List(0);
  if (xxInt != 0) {
    _xxToCid[xxInt] = cid;
  }
  if (_currentlyPulling.containsKey(cid)) return Uint8List(0);
  if (_currentlyPulling.length > 5) {
    _pullQueue[cid] = xxInt;
    return Uint8List(0);
  }
  _currentlyPulling[cid] = xxInt;

  try {
    return HttpClient()
        .getUrl(Uri.parse('https://ipfs.io/ipfs/' + cid))
        .timeout(Duration(milliseconds: 13512)) // produces a request object
        .then((request) => request.close()) // sends the request
        .then((response) async {
      //print(response.toString());

      //print(utf8.decode(rp[0]));
      if (response.statusCode == 200) {
        final rp = await response.toList();
        Uint8List r = Uint8List(50000000);
        //TODO check withxx, add when match
        int ff = 0;
        for (var i in rp) {
          for (var ii in i) {
            if (ff > r.lengthInBytes) {
              _xxWants.remove(xxInt);
              _currentlyPulling.remove(cid);
              _brokenCIDs.add(cid);
              Random rnd = new Random();
              final rd = 30 + rnd.nextInt(200);
              Timer(Duration(milliseconds: rd), () {
                pullQueueRefresh;
              });
              return Uint8List(0);
            }
            r[ff++] = ii;
          }
        }
        //_io?.commitCid(r.sublist(0, ff), cid);
        _currentlyPulling.remove(cid);
        final dd = r.sublist(0, ff);
        /*xxHash(dd).bake().then((value) async {
          print('ipfs pull xxBaked');
          print(value.toString());
          final tar = HEX.decode(value);
          final al = Int64List.fromList(tar);
          print(al.first);
          //print(base64Encode(value.toString()));
          print(xxInt);
          print(base64Decode(xxInt));
          print(base64Decode(xxInt).buffer.asInt64List());
          //final vs = base64Encode(value.toString());
          _xxStash[value.toString()] = dd;
          _xxWants.remove(xxInt);
        });*/
        _xxStash[xxInt] = dd;
        _xxWants.remove(xxInt);
        //if (xxInt != 0) {
        //verifying takes a week, ignore ignore
        //xxInt = await xxHash(r.sublist(0, ff)).bake();
        //_xxStash[xxInt] = r.sublist(0, ff);
        //_io?.mapCidToXX(cid, xxInt);
        //_xxWants.remove(xxInt);
        //}

        Random rnd = new Random();
        final rd = 30 + rnd.nextInt(200);
        Timer(Duration(milliseconds: rd), () {
          pullQueueRefresh;
        });

        return dd;
      } else {
        _currentlyPulling.remove(cid);
        Random rnd = new Random();
        final rd = 30 + rnd.nextInt(200);
        Timer(Duration(milliseconds: rd), () {
          pullQueueRefresh;
        });
        return Uint8List(0);
      }
    });
  } catch (e) {
    setOffline();
    _xxWants.remove(xxInt);
    _currentlyPulling.remove(cid);
    Random rnd = new Random();
    final rd = 30 + rnd.nextInt(200);
    Timer(Duration(milliseconds: rd), () {
      pullQueueRefresh;
    });
    return Uint8List(0);

    // handle timeout
  }
}