Merge branch 'claude/nostalgic-wright-acfc25': inline offer photos
Market offers carry a small base64 data-URI thumbnail embedded in the Nostr event (no media server); full photo stays in the encrypted inventory. Blossom alternative parked on branch parked/offer-photos-blossom. # Conflicts: # apps/app_seeds/lib/i18n/strings.g.dart
This commit is contained in:
commit
e2b88b4f26
20 changed files with 520 additions and 16 deletions
61
apps/app_seeds/test/services/offer_thumbnail_test.dart
Normal file
61
apps/app_seeds/test/services/offer_thumbnail_test.dart
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:image/image.dart' as img;
|
||||
import 'package:tane/services/offer_thumbnail.dart';
|
||||
|
||||
void main() {
|
||||
/// A real, decodable photo far bigger than a thumbnail, with varied pixels so
|
||||
/// JPEG can't compress it to nothing.
|
||||
Uint8List bigPhoto() {
|
||||
final image = img.Image(width: 1280, height: 960);
|
||||
for (var y = 0; y < image.height; y++) {
|
||||
for (var x = 0; x < image.width; x++) {
|
||||
image.setPixelRgb(x, y, (x * 7) % 256, (y * 5) % 256, (x + y) % 256);
|
||||
}
|
||||
}
|
||||
return img.encodeJpg(image, quality: 90);
|
||||
}
|
||||
|
||||
test('produces a small jpeg data-URI that fits the byte budget', () {
|
||||
final uri = offerThumbnailDataUri(bigPhoto(), maxBytes: 40000);
|
||||
|
||||
expect(uri, isNotNull);
|
||||
expect(uri!, startsWith('data:image/jpeg;base64,'));
|
||||
final b64 = uri.substring('data:image/jpeg;base64,'.length);
|
||||
expect(b64.length, lessThanOrEqualTo(40000));
|
||||
|
||||
// The payload really is a smaller image than the source.
|
||||
final decoded = img.decodeJpg(base64.decode(b64))!;
|
||||
expect(decoded.width, lessThanOrEqualTo(320));
|
||||
expect(decoded.height, lessThanOrEqualTo(320));
|
||||
});
|
||||
|
||||
test('never upscales an already-tiny image', () {
|
||||
final small = img.encodeJpg(img.Image(width: 64, height: 48), quality: 80);
|
||||
final uri = offerThumbnailDataUri(small);
|
||||
|
||||
expect(uri, isNotNull);
|
||||
final b64 = uri!.substring('data:image/jpeg;base64,'.length);
|
||||
final decoded = img.decodeJpg(base64.decode(b64))!;
|
||||
expect(decoded.width, 64);
|
||||
expect(decoded.height, 48);
|
||||
});
|
||||
|
||||
test('returns null for bytes that are not an image', () {
|
||||
expect(offerThumbnailDataUri(Uint8List.fromList([1, 2, 3, 4])), isNull);
|
||||
});
|
||||
|
||||
group('decodeDataUri', () {
|
||||
test('round-trips a base64 data URI to its bytes', () {
|
||||
final bytes = Uint8List.fromList([9, 8, 7, 6]);
|
||||
final uri = 'data:image/jpeg;base64,${base64.encode(bytes)}';
|
||||
expect(decodeDataUri(uri), bytes);
|
||||
});
|
||||
|
||||
test('returns null for a plain http url', () {
|
||||
expect(decodeDataUri('https://example.org/a.jpg'), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue