From e4d1f74aa18d827d56f9ac801491d47e15f08898 Mon Sep 17 00:00:00 2001 From: LiquidatorCoder Date: Thu, 25 Nov 2021 13:23:54 +0530 Subject: [PATCH] add dynamic link builder method --- lib/services/github_service.dart | 6 ++-- lib/services/shortener_service.dart | 48 ++++++++++++++++++++++++----- lib/services/zip_service.dart | 13 ++++++++ lib/utilities/byte_formatter.dart | 8 +++++ test/shortner_service_test.dart | 2 +- 5 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 lib/utilities/byte_formatter.dart diff --git a/lib/services/github_service.dart b/lib/services/github_service.dart index f4eb706..279f529 100644 --- a/lib/services/github_service.dart +++ b/lib/services/github_service.dart @@ -31,8 +31,10 @@ class GithubService { ); final _downloadLink = await _shortenerService.getShortUrl( - jsonDecode(response.body)["content"]["download_url"] ?? '', password); - return _downloadLink ?? ''; + jsonDecode(response.body)["content"]["download_url"] ?? ''); + final dynamicLink = + await _shortenerService.getDynamicLink(_downloadLink ?? '', password); + return dynamicLink; } Future request( diff --git a/lib/services/shortener_service.dart b/lib/services/shortener_service.dart index 6eecaa6..4dcd976 100644 --- a/lib/services/shortener_service.dart +++ b/lib/services/shortener_service.dart @@ -1,9 +1,13 @@ import 'dart:io'; import 'package:dio/dio.dart'; +import 'package:firebase_dynamic_links/firebase_dynamic_links.dart'; +import 'package:odin/services/locator.dart'; import 'package:odin/services/logger.dart'; +import 'package:odin/services/zip_service.dart'; class ShortenerService { + final _zipService = locator(); final _dio = Dio(BaseOptions( baseUrl: 'https://api.shrtco.de/v2/', )); @@ -13,8 +17,6 @@ class ShortenerService { Map? headers, Map? body, }) async { - final String time = DateTime.now().toString(); - logger.d("GET:: $time : ${uri.toString()}"); try { final Stopwatch stopwatch = Stopwatch()..start(); final Map query = {}; @@ -23,7 +25,6 @@ class ShortenerService { queryParameters: query, options: Options(headers: headers)); stopwatch.stop(); logger.d("Last request took : ${stopwatch.elapsedMilliseconds} ms."); - logger.d("Request : ${response.realUri}"); if (response.statusCode != 201) { throw HttpException(response.statusCode.toString()); } @@ -39,8 +40,6 @@ class ShortenerService { Map? headers, Map? body, }) async { - final String time = DateTime.now().toString(); - logger.d("POST:: $time : ${uri.toString()}"); try { final Stopwatch stopwatch = Stopwatch()..start(); final Map query = {}; @@ -49,7 +48,6 @@ class ShortenerService { queryParameters: query, options: Options(headers: headers)); stopwatch.stop(); logger.d("Last request took : ${stopwatch.elapsedMilliseconds} ms."); - logger.d("Request : ${response.realUri}"); if (response.statusCode != 201) { throw HttpException(response.statusCode.toString()); } @@ -60,13 +58,47 @@ class ShortenerService { } } - Future getShortUrl(String url, String password) async { + Future getShortUrl(String url) async { + logger.d('Fetching short link'); final Response? response = await post(uri: 'shorten?url=$url'); if (response != null) { final shortLink = response.data["result"]["full_short_link"]; - return shortLink + "?password=$password"; + return shortLink; } else { return null; } } + + Future getDynamicLink(String shortUrl, String password) async { + logger.d('Started building dynamic link'); + final initialLink = Uri.parse( + 'https://getodin.com/files/${shortUrl.replaceAll("https://shrtco.de/", "")}$password'); + final DynamicLinkParameters parameters = DynamicLinkParameters( + uriPrefix: 'https://getodin.page.link', + link: initialLink, + androidParameters: AndroidParameters( + packageName: 'com.odin.odin', + minimumVersion: 1, + ), + iosParameters: IosParameters( + bundleId: 'com.odin.odin', + minimumVersion: '0.1.0', + appStoreId: + '123456789', // Update this value with your app's App Store ID + ), + socialMetaTagParameters: SocialMetaTagParameters( + title: _zipService.linkTitle, + description: _zipService.linkDesc, + ), + ); + final Uri dynamicLink = await parameters.buildUrl(); + final ShortDynamicLink shortenedLink = + await DynamicLinkParameters.shortenUrl( + dynamicLink, + DynamicLinkParametersOptions( + shortDynamicLinkPathLength: ShortDynamicLinkPathLength.short), + ); + logger.d('Finished building dynamic link'); + return shortenedLink.shortUrl.toString(); + } } diff --git a/lib/services/zip_service.dart b/lib/services/zip_service.dart index 73f4894..bbffd9b 100644 --- a/lib/services/zip_service.dart +++ b/lib/services/zip_service.dart @@ -4,11 +4,14 @@ import 'package:archive/archive_io.dart'; import 'package:odin/services/locator.dart'; import 'package:odin/services/logger.dart'; import 'package:odin/services/random_service.dart'; +import 'package:odin/utilities/byte_formatter.dart'; import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; class ZipService { final RandomService _randomService = locator(); + String linkTitle = ""; + String linkDesc = ""; Future zipFile({ required List fileToZips, @@ -27,6 +30,16 @@ class ZipService { } encoder.close(); logger.d('Finished Zipping Files'); + if (fileToZips.length == 1) { + linkTitle = basename(fileToZips.first.path); + } else if (fileToZips.length == 2) { + linkTitle = + "${basename(fileToZips.first.path)} & ${fileToZips.length - 1} more file."; + } else { + linkTitle = + "${basename(fileToZips.first.path)} & ${fileToZips.length - 1} more files."; + } + linkDesc = formatBytes(File(zipFilePath).lengthSync(), 2); return File(zipFilePath); } } diff --git a/lib/utilities/byte_formatter.dart b/lib/utilities/byte_formatter.dart new file mode 100644 index 0000000..d81c1b3 --- /dev/null +++ b/lib/utilities/byte_formatter.dart @@ -0,0 +1,8 @@ +import 'dart:math'; + +String formatBytes(int bytes, int decimals) { + if (bytes <= 0) return "0 B"; + const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; + var i = (log(bytes) / log(1024)).floor(); + return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + ' ' + suffixes[i]; +} diff --git a/test/shortner_service_test.dart b/test/shortner_service_test.dart index 6a366c7..1f8cdf6 100644 --- a/test/shortner_service_test.dart +++ b/test/shortner_service_test.dart @@ -6,7 +6,7 @@ void main() { test("Url should contain shrtco.de", () async { final ShortenerService _shortnerService = ShortenerService(); const String url = "https://www.google.com"; - final String? shortUrl = await _shortnerService.getShortUrl(url, "test"); + final String? shortUrl = await _shortnerService.getShortUrl(url); expect(shortUrl, stringContainsInOrder(["https://", "shrtco.de"])); }); });