Skip to content

Commit

Permalink
feat: new web folder structure for images (#984)
Browse files Browse the repository at this point in the history
Deleted file:
* `suggestion_manager.dart`: unrelated removal of deprecated code

Impacted files:
* `image_helper.dart`: unrelated removal of deprecated code
* `json_helper.dart`: unrelated removal of deprecated code
* `open_food_api_client.dart`: unrelated removal of deprecated code
* `openfoodfacts.dart`: unrelated removal of deprecated code
* `pnns_groups.dart`: unrelated removal of deprecated code
* `status.dart`: unrelated removal of deprecated code
* `uri_helper.dart`: new web folder structure for images
  • Loading branch information
monsieurtanuki authored Oct 21, 2024
1 parent 5d13faf commit b99f119
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 197 deletions.
1 change: 0 additions & 1 deletion lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export 'src/utils/product_helper.dart';
export 'src/utils/product_query_configurations.dart';
export 'src/utils/product_search_query_configuration.dart';
export 'src/utils/server_type.dart';
export 'src/utils/suggestion_manager.dart';
export 'src/utils/tag_type.dart';
export 'src/utils/tag_type_autocompleter.dart';
export 'src/utils/too_many_requests_exception.dart';
Expand Down
5 changes: 0 additions & 5 deletions lib/src/model/status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ part 'status.g.dart';

@JsonSerializable()
class Status extends JsonObject {
// TODO: deprecated from 2023-11-24; remove when old enough
@Deprecated('Use wrongUserOrPasswordErrorMessage instead')
static const WRONG_USER_OR_PASSWORD_ERROR_MESSAGE =
wrongUserOrPasswordErrorMessage;

static const wrongUserOrPasswordErrorMessage =
'Incorrect user name or password';

Expand Down
42 changes: 0 additions & 42 deletions lib/src/open_food_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import 'model/user.dart';
import 'utils/abstract_query_configuration.dart';
import 'utils/country_helper.dart';
import 'utils/http_helper.dart';
import 'utils/image_helper.dart';
import 'utils/language_helper.dart';
import 'utils/ocr_field.dart';
import 'utils/open_food_api_configuration.dart';
Expand Down Expand Up @@ -309,47 +308,6 @@ class OpenFoodAPIClient {
return result;
}

/// Returns the ids of all uploaded images for that product.
///
/// To be used in combination with [ImageHelper.getUploadedImageUrl].
/// Does not depend on language or country.
// TODO: deprecated from 2023-11-25; remove when old enough
@Deprecated('Use product field "images" instead')
static Future<List<int>> getProductImageIds(
final String barcode, {
final User? user,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final ProductQueryConfiguration configuration = ProductQueryConfiguration(
barcode,
version: ProductQueryVersion.v3,
fields: <ProductField>[ProductField.IMAGES],
);
final String productString = await getProductString(
configuration,
user: user,
uriHelper: uriHelper,
);
final String jsonStr = _replaceQuotes(productString);
final json = HttpHelper().jsonDecode(jsonStr);
if (json['status'] != 'success') {
throw Exception('Error: ${json['status']}');
} else if (json['product']['images'] == null) {
return <int>[];
}

final Map<String, dynamic> images = json['product']['images'];
final List<int> result = <int>[];
for (final String key in images.keys) {
final int? value = int.tryParse(key);
if (value != null) {
result.add(value);
}
}
result.sort();
return result;
}

/// Returns the response body of "get product" API for the given barcode.
static Future<String> getProductString(
final ProductQueryConfiguration configuration, {
Expand Down
60 changes: 0 additions & 60 deletions lib/src/utils/image_helper.dart
Original file line number Diff line number Diff line change
@@ -1,68 +1,8 @@
import 'open_food_api_configuration.dart';
import 'uri_helper.dart';
import '../model/product_image.dart';

/// Helper class related to product pictures
class ImageHelper {
/// Minimum accepted width for an uploaded image.
static const int minimumWidth = 640;

/// Minimum accepted height for an uploaded image.
static const int minimumHeight = 160;

/// Returns the [image] full url - for a specific [imageSize] if needed.
///
/// E.g. "https://images.openfoodfacts.org/images/products/359/671/046/2858/front_fr.4.100.jpg"
// TODO: deprecated from 2023-11-25; remove when old enough
@Deprecated('Use ProductImage.getUrl instead')
static String getLocalizedProductImageUrl(
final String barcode,
final ProductImage image, {
final ImageSize? imageSize,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) =>
image.getUrl(barcode, uriHelper: uriHelper, imageSize: imageSize);

/// Returns the [image] full url for an uploaded ("raw") image.
///
/// E.g. "https://images.openfoodfacts.org/images/products/359/671/046/2858/1.400.jpg"
// TODO: deprecated from 2023-11-25; remove when old enough
@Deprecated('Use ProductImage.getUrl instead')
static String getUploadedImageUrl(
final String barcode,
final int imageId,
final ImageSize imageSize, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) =>
ProductImage.raw(imgid: imageId.toString(), size: imageSize).getUrl(
barcode,
uriHelper: uriHelper,
);

/// Returns the [image] filename - for a specific [imageSize] if needed.
///
/// By default uses the own [image]'s size field.
/// E.g. "front_fr.4.100.jpg"
/// cf. https://github.com/openfoodfacts/smooth-app/issues/3065
// TODO: deprecated from 2023-11-25; remove when old enough
@Deprecated('Use ProductImage.getUrlFilename instead')
static String getProductImageFilename(
final ProductImage image, {
final ImageSize? imageSize,
}) =>
image.getUrlFilename(imageSize: imageSize);

/// Returns the filename of an uploaded image.
///
/// cf. https://github.com/openfoodfacts/smooth-app/issues/3065
// TODO: deprecated from 2023-11-25; remove when old enough
@Deprecated('Use ProductImage.getUrlFilename instead')
static String getUploadedImageFilename(
final int imageId,
final ImageSize imageSize,
) =>
ProductImage.raw(
imgid: imageId.toString(),
size: imageSize,
).getUrlFilename();
}
19 changes: 0 additions & 19 deletions lib/src/utils/json_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,6 @@ class JsonHelper {
return result;
}

/// Returns [ProductImage]s from a JSON map for "Images".
///
/// For historical reasons we keep only the 4 main images here, on all sizes
/// and languages.
// TODO: deprecated from 2023-11-25; remove when old enough
@Deprecated('Use allImagesFromJson instead')
static List<ProductImage>? imagesFromJson(Map? json) => allImagesFromJson(
json,
onlyMain: true,
);

// only for main images
static const String _ALL_IMAGES_TAG_REVISION = 'rev';
static const String _ALL_IMAGES_TAG_ANGLE = 'angle';
Expand Down Expand Up @@ -226,14 +215,6 @@ class JsonHelper {
return imageList;
}

// TODO: deprecated from 2023-11-25; remove when old enough
@Deprecated('Use allImagesToJson instead')
static Map<String, dynamic> imagesToJson(List<ProductImage>? images) =>
allImagesToJson(
images,
onlyMain: true,
);

static Map<String, dynamic> allImagesToJson(
final List<ProductImage>? images, {
final bool onlyMain = false,
Expand Down
5 changes: 0 additions & 5 deletions lib/src/utils/pnns_groups.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,4 @@ enum PnnsGroup2 implements OffTagged {

/// Name in English
final String name;

/// Returns the id of the subgroup
// TODO: deprecated from 2023-11-15; remove when old enough
@Deprecated('Use offTag instead')
String get id => offTag;
}
53 changes: 0 additions & 53 deletions lib/src/utils/suggestion_manager.dart

This file was deleted.

26 changes: 14 additions & 12 deletions lib/src/utils/uri_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,23 @@ class UriProductHelper extends UriHelper {
/// Returns the barcode sub-folder (without trailing '/').
///
/// For instance:
/// * `12345678` for barcode `12345678`
/// * `123/456/789` for barcode `123456789`
/// * `123/456/789/0` for barcode `1234567890`
/// * `000/001/234/5678` for barcode `12345678`
/// * `000/012/345/6789` for barcode `123456789`
/// * `000/123/456/7890` for barcode `1234567890`
/// * `123/456/789/0123` for barcode `1234567890123`
static String getBarcodeSubPath(final String barcode) {
if (barcode.length < 9) {
return barcode;
}
final String p1 = barcode.substring(0, 3);
final String p2 = barcode.substring(3, 6);
final String p3 = barcode.substring(6, 9);
if (barcode.length == 9) {
return '$p1/$p2/$p3';
const int typicalLength = 13;
final int length = barcode.length;
final String workBarcode;
if (length >= typicalLength) {
workBarcode = barcode;
} else {
workBarcode = '${'0' * (typicalLength - length)}$barcode';
}
final String p4 = barcode.substring(9);
final String p1 = workBarcode.substring(0, 3);
final String p2 = workBarcode.substring(3, 6);
final String p3 = workBarcode.substring(6, 9);
final String p4 = workBarcode.substring(9);
return '$p1/$p2/$p3/$p4';
}
}

0 comments on commit b99f119

Please sign in to comment.