Skip to content

Commit eaf7f33

Browse files
feat: Adds supportedExtensions for decoders (#47)
* ✨ Adds `supportedExtensions` for decoders * feat: update BaseDecoder - Add` BaseDecoder.isSupportExtension` method - Update `BaseDecoder.supportedExtensions`, use `@mustBeOverridden` replace abstract - Add test for the extensions Signed-off-by: Caijinglong <[email protected]> * feat: Update version to 2.4.0 - Update version for pubspec - Add changelog - Add example for README Signed-off-by: Caijinglong <[email protected]> --------- Signed-off-by: Caijinglong <[email protected]> Co-authored-by: Caijinglong <[email protected]>
1 parent a6d468b commit eaf7f33

File tree

15 files changed

+136
-5
lines changed

15 files changed

+136
-5
lines changed

example/test/issue_027_test.dart

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class MyJpegDecoder extends JpegDecoder {
2020
bool _checkHeader(List<int> bytes) {
2121
return bytes[0] == 0xFF && bytes[1] == 0xD8;
2222
}
23+
24+
@override
25+
List<String> get supportedExtensions => JpegDecoder().supportedExtensions;
2326
}
2427

2528
void main() {

packages/image_size_getter/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# CHANGELOG
22

33
- [CHANGELOG](#changelog)
4+
- [2.4.0](#240)
45
- [2.3.0+1](#2301)
56
- [2.3.0](#230)
67
- [2.2.0](#220)
@@ -17,6 +18,10 @@
1718
- [0.1.1](#011)
1819
- [0.1.0](#010)
1920

21+
## 2.4.0
22+
23+
- Add `isSupportExtension` to `BaseDecoder`
24+
2025
## 2.3.0+1
2126

2227
- Docs: Add docs for dartdoc.

packages/image_size_getter/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ If your data source is read asynchronously, consider using `AsyncImageInput`.
103103

104104
A typical use case is [http_input][HttpInput].
105105

106+
## Check the extension for decoder
107+
108+
> Since 2.4.0
109+
110+
```dart
111+
void checkExtension(String ext, BaseDecoder decoder) {
112+
final isSupport = decoder.isSupportExtension(ext);
113+
print('The decoder ${decoder.decoderName} support $ext: $isSupport');
114+
}
115+
```
116+
106117
## Custom
107118

108119
We can implement our own input or decoder.

packages/image_size_getter/lib/src/decoder/decoder.dart

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import 'package:meta/meta.dart';
12
import 'package:collection/collection.dart';
23
import 'package:image_size_getter/image_size_getter.dart';
34

45
/// {@template image_size_getter.BaseDecoder}
5-
///
66
/// Base of the decoder.
77
///
88
/// Implement this class to create a new decoder.
9-
///
109
/// {@endtemplate}
1110
abstract class BaseDecoder {
1211
/// {@macro image_size_getter.BaseDecoder}
@@ -15,6 +14,31 @@ abstract class BaseDecoder {
1514
/// The name of the decoder.
1615
String get decoderName;
1716

17+
/// How many file extensions are supported with the decoder.
18+
///
19+
/// The method must be overridden for each implementation.
20+
///
21+
/// - See also: https://developer.mozilla.org/docs/Web/Media/Formats/Image_types
22+
/// - See also: [isSupportExtension]
23+
@mustBeOverridden
24+
List<String> get supportedExtensions => [];
25+
26+
/// {@template image_size_getter.BaseDecoder.isSupportExtension}
27+
///
28+
/// Returns the [extension] is support or not.
29+
/// Ignore the case of letters.
30+
///
31+
/// {@endtemplate}
32+
bool isSupportExtension(String extension) {
33+
for (final ext in supportedExtensions) {
34+
// ignore the case.
35+
if (ext.toLowerCase() == extension.toLowerCase()) {
36+
return true;
37+
}
38+
}
39+
return false;
40+
}
41+
1842
/// {@template image_size_getter.BaseDecoder.isValid}
1943
///
2044
/// Returns the [input] is support or not.

packages/image_size_getter/lib/src/decoder/impl/bmp_decoder.dart

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class BmpDecoder extends BaseDecoder {
1212
@override
1313
String get decoderName => 'bmp';
1414

15+
@override
16+
List<String> get supportedExtensions => List.unmodifiable(['bmp']);
17+
1518
@override
1619
Size getSize(ImageInput input) {
1720
final widthList = input.getRange(0x12, 0x16);

packages/image_size_getter/lib/src/decoder/impl/gif_decoder.dart

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class GifDecoder extends BaseDecoder with MutilFileHeaderAndFooterValidator {
1111

1212
String get decoderName => 'gif';
1313

14+
@override
15+
List<String> get supportedExtensions => List.unmodifiable(['gif']);
16+
1417
Size _getSize(List<int> widthList, List<int> heightList) {
1518
final width = convertRadix16ToInt(widthList, reverse: true);
1619
final height = convertRadix16ToInt(heightList, reverse: true);

packages/image_size_getter/lib/src/decoder/impl/jpeg_decoder.dart

+5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ class JpegDecoder extends BaseDecoder with SimpleTypeValidator {
1414
});
1515

1616
final bool isStandardJpeg;
17+
1718
@override
1819
String get decoderName => isStandardJpeg ? 'jpeg' : 'non-standard-jpeg';
1920

21+
@override
22+
List<String> get supportedExtensions =>
23+
List.unmodifiable(['jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp']);
24+
2025
@override
2126
Size getSize(ImageInput input) {
2227
int start = 2;

packages/image_size_getter/lib/src/decoder/impl/png_decoder.dart

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class PngDecoder extends BaseDecoder with SimpleTypeValidator {
1212
@override
1313
String get decoderName => 'png';
1414

15+
@override
16+
List<String> get supportedExtensions => List.unmodifiable(['png']);
17+
1518
@override
1619
Size getSize(ImageInput input) {
1720
final widthList = input.getRange(0x10, 0x14);

packages/image_size_getter/lib/src/decoder/impl/webp_decoder.dart

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class WebpDecoder extends BaseDecoder {
1313
@override
1414
String get decoderName => 'webp';
1515

16+
@override
17+
List<String> get supportedExtensions => List.unmodifiable(['webp']);
18+
1619
@override
1720
Size getSize(ImageInput input) {
1821
final chunkHeader = input.getRange(12, 16);

packages/image_size_getter/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: image_size_getter
22
description: Get image width and height, the library does not completely decode the image file, just read the metadata to get the image width and height.
3-
version: 2.3.0+1
3+
version: 2.4.0
44
homepage: https://github.com/CaiJingLong/dart_image_size_getter
55

66
topics:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:image_size_getter/image_size_getter.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group('Test decoder extension', () {
6+
test('Test gif decoder', () {
7+
final GifDecoder decoder = GifDecoder();
8+
expect(decoder.isSupportExtension('gif'), true);
9+
expect(decoder.isSupportExtension('GIF'), true);
10+
11+
expect(decoder.isSupportExtension('jpg'), false);
12+
expect(decoder.isSupportExtension('JPG'), false);
13+
});
14+
15+
test('Test jpeg decoder', () {
16+
final JpegDecoder decoder = JpegDecoder();
17+
expect(decoder.isSupportExtension('jpg'), true);
18+
expect(decoder.isSupportExtension('JPG'), true);
19+
expect(decoder.isSupportExtension('jpeg'), true);
20+
expect(decoder.isSupportExtension('JPEG'), true);
21+
22+
expect(decoder.isSupportExtension('gif'), false);
23+
expect(decoder.isSupportExtension('GIF'), false);
24+
});
25+
26+
test('Test png decoder', () {
27+
final PngDecoder decoder = PngDecoder();
28+
expect(decoder.isSupportExtension('png'), true);
29+
expect(decoder.isSupportExtension('PNG'), true);
30+
31+
expect(decoder.isSupportExtension('jpg'), false);
32+
expect(decoder.isSupportExtension('JPG'), false);
33+
});
34+
35+
test('Test webp decoder', () {
36+
final WebpDecoder decoder = WebpDecoder();
37+
expect(decoder.isSupportExtension('webp'), true);
38+
expect(decoder.isSupportExtension('WEBP'), true);
39+
40+
expect(decoder.isSupportExtension('jpg'), false);
41+
expect(decoder.isSupportExtension('JPG'), false);
42+
});
43+
44+
test('Test bmp decoder', () {
45+
final BmpDecoder decoder = BmpDecoder();
46+
expect(decoder.isSupportExtension('bmp'), true);
47+
expect(decoder.isSupportExtension('BMP'), true);
48+
49+
expect(decoder.isSupportExtension('jpg'), false);
50+
expect(decoder.isSupportExtension('JPG'), false);
51+
});
52+
});
53+
}

packages/image_size_getter_heic/lib/src/image_size_getter_heic_base.dart

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class HeicDecoder extends BaseDecoder {
3535
@override
3636
String get decoderName => 'heic';
3737

38+
@override
39+
List<String> get supportedExtensions =>
40+
List.unmodifiable(['heic', 'heics', 'heif', 'heifs']);
41+
3842
/// Extracts the size information from a HEIC image synchronously.
3943
///
4044
/// This method parses the BMFF structure of the HEIC file to find the 'ispe' box

packages/image_size_getter_heic/test/image_size_getter_heic_test.dart

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ void main() {
1616
final asyncInput = AsyncImageInput.input(input);
1717
expect(decoder.isValidAsync(asyncInput), completion(equals(true)));
1818
});
19+
20+
test('extension', () {
21+
expect(decoder.isSupportExtension('heic'), equals(true));
22+
expect(decoder.isSupportExtension('HEIC'), equals(true));
23+
expect(decoder.isSupportExtension('heif'), equals(true));
24+
expect(decoder.isSupportExtension('HEIF'), equals(true));
25+
26+
expect(decoder.isSupportExtension('jpg'), equals(false));
27+
expect(decoder.isSupportExtension('JPG'), equals(false));
28+
});
1929
});
2030

2131
group('Test get heic size', () {

packages/image_size_getter_http_input/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.1.0
4+
5+
- Support library `2.4.0`.
6+
37
## 2.0.1+1
48

59
- Docs: Add docs for dartdoc.

packages/image_size_getter_http_input/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: image_size_getter_http_input
22
description: A http input for image_size_getter.
3-
version: 2.0.1+1
3+
version: 2.1.0
44
homepage: https://github.com/CaiJingLong/dart_image_size_getter/tree/master/image_size_getter_http_input
55
# publish_to: https://pub.dev/
66

@@ -11,7 +11,7 @@ dependencies:
1111
http: ^1.0.0
1212
# image_size_getter:
1313
# path: ../library
14-
image_size_getter: ^2.0.0
14+
image_size_getter: ^2.4.0
1515
# path: ^1.8.0
1616

1717
dev_dependencies:

0 commit comments

Comments
 (0)