diff --git a/lib/services/encryption_service.dart b/lib/services/encryption_service.dart index c581734..3994f7f 100644 --- a/lib/services/encryption_service.dart +++ b/lib/services/encryption_service.dart @@ -9,7 +9,7 @@ import 'package:path/path.dart'; class EncryptionService { final RandomService _randomService = locator(); - Future encryptFile(File file) async { + Future> encryptFile(File file) async { logger.d('Started Encryption'); final crypt = AesCrypt(); final password = _randomService.getRandomString(16); @@ -17,8 +17,9 @@ class EncryptionService { final encryptedFilePath = join(file.parent.path, basenameWithoutExtension(file.path) + '.odin'); crypt.encryptFileSync(file.path, encryptedFilePath); + file.deleteSync(); // Delete the original zip file logger.d('Finished Encryption'); - return File(encryptedFilePath); + return {'file': File(encryptedFilePath), 'password': password}; } Future decryptFile(File file, String password) async { @@ -29,6 +30,7 @@ class EncryptionService { final decryptedFilePath = crypt.decryptFileSync(file.path, join(file.parent.path, basenameWithoutExtension(file.path) + '.zip')); File decryptedFile = File(decryptedFilePath); + file.deleteSync(); // Delete the original AES file logger.d('Finished Decryption'); return decryptedFile; } diff --git a/lib/services/file_service.dart b/lib/services/file_service.dart index 7f5c68c..428fadf 100644 --- a/lib/services/file_service.dart +++ b/lib/services/file_service.dart @@ -27,10 +27,12 @@ class FileService { processing = true; List files = result.paths.map((path) => File(path!)).toList(); final zippedFile = await _zipService.zipFile(fileToZips: files); - final encryptedFile = await _encrytionService.encryptFile(zippedFile); + final encryptedFileDetails = + await _encrytionService.encryptFile(zippedFile); zipfileName = basename(zippedFile.path); processing = false; - _path = await _githubService.uploadFileAnonymous(encryptedFile); + _path = await _githubService.uploadFileAnonymous( + encryptedFileDetails['file'], encryptedFileDetails['password']); loading = false; } else { // User canceled the picker @@ -49,10 +51,12 @@ class FileService { final List fileToZips = urls.map((e) => File(e.toFilePath())).toList(); final zippedFile = await _zipService.zipFile(fileToZips: fileToZips); - final encryptedFile = await _encrytionService.encryptFile(zippedFile); + final encryptedFileDetails = + await _encrytionService.encryptFile(zippedFile); zipfileName = basename(zippedFile.path); processing = false; - _path = await _githubService.uploadFileAnonymous(encryptedFile); + _path = await _githubService.uploadFileAnonymous( + encryptedFileDetails['file'], encryptedFileDetails['password']); loading = false; } else { _path = null; diff --git a/lib/services/github_service.dart b/lib/services/github_service.dart index 996ded6..f4eb706 100644 --- a/lib/services/github_service.dart +++ b/lib/services/github_service.dart @@ -17,7 +17,7 @@ class GithubService { final RandomService _randomService = locator(); final _env = dotenv.env; - Future uploadFileAnonymous(File file) async { + Future uploadFileAnonymous(File file, String password) async { final uploadTime = DateFormat('dd-MM-yyyy hh:mm:ss').format(DateTime.now()); final createFile = CreateFile( content: base64Encode(file.readAsBytesSync()), @@ -31,7 +31,7 @@ class GithubService { ); final _downloadLink = await _shortenerService.getShortUrl( - url: jsonDecode(response.body)["content"]["download_url"] ?? ''); + jsonDecode(response.body)["content"]["download_url"] ?? '', password); return _downloadLink ?? ''; } diff --git a/lib/services/shortener_service.dart b/lib/services/shortener_service.dart index 30c0de6..6eecaa6 100644 --- a/lib/services/shortener_service.dart +++ b/lib/services/shortener_service.dart @@ -60,10 +60,11 @@ class ShortenerService { } } - Future getShortUrl({required String url}) async { + Future getShortUrl(String url, String password) async { final Response? response = await post(uri: 'shorten?url=$url'); if (response != null) { - return response.data["result"]["full_short_link"]; + final shortLink = response.data["result"]["full_short_link"]; + return shortLink + "?password=$password"; } else { return null; } diff --git a/test/shortner_service_test.dart b/test/shortner_service_test.dart index 6423571..6a366c7 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: url); + final String? shortUrl = await _shortnerService.getShortUrl(url, "test"); expect(shortUrl, stringContainsInOrder(["https://", "shrtco.de"])); }); });