pullCidFromIPFS method

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

Implementation

Future<Uint8List> pullCidFromIPFS(String cid, int 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://cloudflare-ipfs.com/ipfs/' + cid))
        .timeout(Duration(milliseconds: 3512)) // 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);

        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 r.sublist(0, ff);
      } else {
        _currentlyPulling.remove(cid);
        Random rnd = new Random();
        final rd = 30 + rnd.nextInt(200);
        Timer(Duration(milliseconds: rd), () {
          pullQueueRefresh;
        });
        return Uint8List(0);
      }
    });
  } on TimeoutException catch (e) {
    _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
  } catch (e) {
    setOffline();
    return Uint8List(0);
  }
}