getProductImageRootUrl static method

String getProductImageRootUrl(
  1. String barcode, {
  2. QueryType? queryType,
})

Returns the web folder of the product images (without trailing '/')

E.g. "https://static.openfoodfacts.org/images/products/359/671/046/2858"

Implementation

static String getProductImageRootUrl(
  final String barcode, {
  final QueryType? queryType,
}) {
  final String barcodePath;
  if (barcode.length >= 9) {
    var p1 = barcode.substring(0, 3);
    var p2 = barcode.substring(3, 6);
    var p3 = barcode.substring(6, 9);
    if (barcode.length == 9) {
      barcodePath = '$p1/$p2/$p3';
    } else {
      var p4 = barcode.substring(9);
      barcodePath = '$p1/$p2/$p3/$p4';
    }
  } else {
    barcodePath = barcode;
  }

  final String root =
      OpenFoodAPIConfiguration.getQueryType(queryType) == QueryType.PROD
          ? OpenFoodAPIConfiguration.imageProdUrlBase
          : OpenFoodAPIConfiguration.imageTestUrlBase;
  final String separator = root.endsWith('/') ? '' : '/';
  return '$root$separator$barcodePath';
}