From cf4d1cb50b45481de818c62ddbc7206d72b6faf3 Mon Sep 17 00:00:00 2001 From: Nitesh Sharma Date: Sat, 2 Jul 2022 16:19:38 +0530 Subject: [PATCH] Update Library --- .github/scripts/pub_login.sh | 35 ------------------- .github/workflows/publish.yaml | 32 ----------------- example/mime_dart_example.dart | 20 +++++------ lib/src/mime_dart_base.dart | 63 +++++++++++++++++----------------- pubspec.yaml | 2 +- 5 files changed, 43 insertions(+), 109 deletions(-) delete mode 100644 .github/scripts/pub_login.sh delete mode 100644 .github/workflows/publish.yaml diff --git a/.github/scripts/pub_login.sh b/.github/scripts/pub_login.sh deleted file mode 100644 index d37fb64..0000000 --- a/.github/scripts/pub_login.sh +++ /dev/null @@ -1,35 +0,0 @@ -# This script creates/updates credentials.json file which is used -# to authorize publisher when publishing packages to pub.dev - -# Checking whether the secrets are available as environment -# variables or not. -if [ -z "${PUB_DEV_PUBLISH_ACCESS_TOKEN}" ]; then - echo "Missing PUB_DEV_PUBLISH_ACCESS_TOKEN environment variable" - exit 1 -fi - -if [ -z "${PUB_DEV_PUBLISH_REFRESH_TOKEN}" ]; then - echo "Missing PUB_DEV_PUBLISH_REFRESH_TOKEN environment variable" - exit 1 -fi - -if [ -z "${PUB_DEV_PUBLISH_TOKEN_ENDPOINT}" ]; then - echo "Missing PUB_DEV_PUBLISH_TOKEN_ENDPOINT environment variable" - exit 1 -fi - -if [ -z "${PUB_DEV_PUBLISH_EXPIRATION}" ]; then - echo "Missing PUB_DEV_PUBLISH_EXPIRATION environment variable" - exit 1 -fi - -# Create credentials.json file. -cat < ~/.pub-cache/credentials.json -{ - "accessToken":"${PUB_DEV_PUBLISH_ACCESS_TOKEN}", - "refreshToken":"${PUB_DEV_PUBLISH_REFRESH_TOKEN}", - "tokenEndpoint":"${PUB_DEV_PUBLISH_TOKEN_ENDPOINT}", - "scopes":["https://www.googleapis.com/auth/userinfo.email","openid"], - "expiration":${PUB_DEV_PUBLISH_EXPIRATION} -} -EOF diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml deleted file mode 100644 index 8d667e7..0000000 --- a/.github/workflows/publish.yaml +++ /dev/null @@ -1,32 +0,0 @@ -name: Publish Package - -on: - release: - types: - - published - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: dart-lang/setup-dart@v1 - - name: Install Dependencies - run: dart pub get - - - name: Run Tests - run: dart test - - - name: Setup Pub Credentials - shell: bash - env: - PUB_DEV_PUBLISH_ACCESS_TOKEN: ${{ secrets.PUB_DEV_PUBLISH_ACCESS_TOKEN }} - PUB_DEV_PUBLISH_REFRESH_TOKEN: ${{ secrets.PUB_DEV_PUBLISH_REFRESH_TOKEN }} - PUB_DEV_PUBLISH_TOKEN_ENDPOINT: ${{ secrets.PUB_DEV_PUBLISH_TOKEN_ENDPOINT }} - PUB_DEV_PUBLISH_EXPIRATION: ${{ secrets.PUB_DEV_PUBLISH_EXPIRATION }} - run: | - sh .github/scripts/pub_login.sh - - name: Check Publish Warnings - run: dart pub publish --dry-run - - name: Publish Package - run: dart pub publish -f diff --git a/example/mime_dart_example.dart b/example/mime_dart_example.dart index 98570a4..f680467 100644 --- a/example/mime_dart_example.dart +++ b/example/mime_dart_example.dart @@ -1,17 +1,17 @@ import 'package:mime_dart/mime_dart.dart'; void main() { - print(Mime.getExtensionFromType('application/pdf')); - // returns `pdf` + print(Mime.getExtensionsFromType('application/xml')); + // returns `[xml, xsl, xsd, rng]` - print(Mime.getTypeFromExtension('pdf')); - // returns `application/pdf` + print(Mime.getTypesFromExtension('xml')); + // returns `[application/xml, text/xml]` - print(Mime.getMime('application/pdf')); + print(Mime.getMimeData('application/xml')); // returns MimeData( - // charset: null, - // source: iana, - // compressible: false, - // extensions: [pdf] - // ) + // charset: null, + // source: iana, + // compressible: true, + // extensions: [xml, xsl, xsd, rng], + // ); } diff --git a/lib/src/mime_dart_base.dart b/lib/src/mime_dart_base.dart index 7871d76..5b34910 100644 --- a/lib/src/mime_dart_base.dart +++ b/lib/src/mime_dart_base.dart @@ -2,58 +2,59 @@ import 'package:mime_dart/src/db.dart'; import 'package:mime_dart/src/mime_data/mime_data.dart'; /// Mime Base Class -/// +/// /// It contains three methods for getting different mime types -/// -/// - [getExtensionFromType] -/// -/// - [getTypeFromExtension] -/// -/// - [getMime] +/// +/// - [getExtensionsFromType] +/// +/// - [getTypesFromExtension] +/// +/// - [getMimeData] class Mime { - /// This method helps in getting extension from type - /// + /// This method helps in getting extensions from type + /// /// [type] takes a [String] input. For Example: `application/pdf` - /// - /// Returns `pdf` - static String? getExtensionFromType(String type) { - if (!database.containsKey(type)) { - return null; - } - - MimeData mime = MimeData.fromJson(database[type] as Map); - - if (mime.extensions != null && mime.extensions!.isNotEmpty) { - return mime.extensions?.first; + /// + /// Returns `[pdf]` + static List? getExtensionsFromType(String type) { + if (type.isEmpty) return null; + + if (database.containsKey(type)) { + MimeData mime = MimeData.fromJson(database[type] as Map); + return mime.extensions; } return null; } - /// This method helps in getting type from extension - /// + /// This method helps in getting types from extension + /// /// [extension] takes [String] as input. For Example: `pdf` - /// - /// Returns `application/pdf` - static String? getTypeFromExtension(String extension) { + /// + /// Returns `[application/pdf]` + static List? getTypesFromExtension(String extension) { + if (extension.isEmpty) return null; + + List types = []; + for (final entry in database.entries) { final mime = MimeData.fromJson(entry.value as Map); if (mime.extensions != null && mime.extensions!.contains(extension)) { - return entry.key; + types.add(entry.key); } } - return null; + return types.isNotEmpty ? types : null; } /// This method helps in getting MimeData from type - /// + /// /// [type] takes [String] as input. For Example: `application/pdf` - /// + /// /// returns [MimeData] object with relevant fields - static MimeData? getMime(String type) { - if (!database.containsKey(type)) { + static MimeData? getMimeData(String type) { + if (type.isEmpty || !database.containsKey(type)) { return null; } diff --git a/pubspec.yaml b/pubspec.yaml index 4633927..e47a99c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: mime_dart description: Mime Types for Dart and Flutter. -version: 1.0.1 +version: 2.0.0 homepage: https://github.com/niteshsh4rma/mime_dart environment: