getImageByHash method

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

Implementation

RequestAndParser<http.BaseResponse, ImageData?> getImageByHash(String hash,
    {bool compressed = false, Data? owner}) {
  final isPathHash = hash.contains('/');
  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 {
        final scope = owner != null ? API().local.scopeFor(owner) : '';
        final name =
            compressed ? OP.convertToCompressedHashName(hash) : hash;
        final scopedName =
            (!isPathHash && scope.isNotEmpty) ? '$scope/$name' : name;
        await API().local.storeImage(res.bodyBytes, scopedName);
        return ImageData(
          (await API().local.readImage(scopedName,
              cacheSize: compressed ? CACHESIZE : null))!,
          id: hash,
        );
      } catch (e) {
        debugPrint("failed to load webimg: " + e.toString());
      }
    }
  }

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