getImageByHash method

RequestAndParser<BaseResponse, ImageData<Object>?> getImageByHash(
  1. String hash, {
  2. bool compressed = false,
})

Implementation

RequestAndParser<http.BaseResponse, ImageData?> getImageByHash(String hash,
    {bool compressed = false}) {
  final rd = switch (kIsWeb) {
    true => RequestData('/login'),
    false => RequestData(
        _getImageFromHash_r,
        json: {
          'hash': hash,
          'compressed': compressed,
        },
        returnsBinary: true,
      )
  };

  parser(http.BaseResponse _res) async {
    if (kIsWeb)
      return ImageData(
          Image(
              image: NetworkImage("$_baseurl/get/compressed/$hash",
                  headers: {HttpHeaders.authorizationHeader: _api_key})),
          id: hash);
    final res = _res.forceRes();
    if (res == null || res.statusCode ~/ 100 != 2)
      return null;
    else {
      try {
        await API().local.storeImage(res.bodyBytes,
            compressed ? OP.convertToCompressedHashName(hash) : hash);
        return ImageData(
          (await API().local.readImage(
              compressed ? OP.convertToCompressedHashName(hash) : hash,
              cacheSize: compressed ? CACHESIZE : null))!,
          id: hash,
        );
      } catch (e) {
        debugPrint("failed to load webimg: " + e.toString());
      }
    }
  }

  return RequestAndParser(rd: rd, parser: parser);
}