Skip to content

Commit

Permalink
[tekartik_app_media] extensionFromMimeType
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Nov 29, 2024
1 parent 7c8e857 commit fb16def
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ updates:
directory: "app_lints"
schedule:
interval: "monthly"
- package-ecosystem: "pub"
directory: "app_media"
schedule:
interval: "monthly"
- package-ecosystem: "pub"
directory: "app_mirrors"
schedule:
Expand Down
1 change: 1 addition & 0 deletions app_media/lib/mime_type.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'src/mapping.dart' show extensionFromMimeType;
export 'src/mime_type_constant.dart';
13 changes: 13 additions & 0 deletions app_media/lib/src/mapping.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:tekartik_app_media/mime_type.dart';

const _mimeTypeExtensions = {
mimeTypeApplicationJson: extensionApplicationJson,
mimeTypeImageJpg: extensionImageJpg,
mimeTypeImagePng: extensionImagePng,
mimeTypeImageWebp: extensionImageWebp,
};

/// Get the extension from a mime type
String? extensionFromMimeType(String mimeType) {
return _mimeTypeExtensions[mimeType];
}
21 changes: 21 additions & 0 deletions app_media/lib/src/mime_type_constant.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
/// Mime type for application/json
const mimeTypeApplicationJson = 'application/json';

/// Extension for json
const extensionApplicationJson = '.json';

/// Mime type for jpg
const mimeTypeImageJpg = 'image/jpeg';

/// Extension for jpg
const extensionImageJpg = '.jpg';

/// Mime type for png
const mimeTypeImagePng = 'image/png';

/// Extension for jpg
const extensionImagePng = '.png';

/// Mime type for webp
const mimeTypeImageWebp = 'image/webp';

/// Extension for webp
const extensionImageWebp = '.webp';
2 changes: 1 addition & 1 deletion app_media/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: tekartik_app_media
description: Media type constants
description: Media type constants and helpers
version: 1.0.0
publish_to: none

Expand Down
24 changes: 22 additions & 2 deletions app_media/test/mime_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@ void main() {
group('mime_type', () {
test('constants', () {
// Ensure it is a const
const types = [mimeTypeApplicationJson];
expect(types, ['application/json']);
const types = [
mimeTypeApplicationJson,
mimeTypeImagePng,
mimeTypeImageJpg,
mimeTypeImageWebp
];
expect(
types, ['application/json', 'image/png', 'image/jpeg', 'image/webp']);
const extensions = [
extensionApplicationJson,
extensionImagePng,
extensionImageJpg,
extensionImageWebp,
];
expect(extensions, ['.json', '.png', '.jpg', '.webp']);
});
test('extensionFromMimeType', () {
expect(extensionFromMimeType(mimeTypeApplicationJson),
extensionApplicationJson);
expect(extensionFromMimeType(mimeTypeImagePng), extensionImagePng);
expect(extensionFromMimeType(mimeTypeImageJpg), extensionImageJpg);
expect(extensionFromMimeType(mimeTypeImageWebp), extensionImageWebp);
});
});
}

0 comments on commit fb16def

Please sign in to comment.