Skip to content

Commit

Permalink
add password back in link
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidatorCoder committed Nov 24, 2021
1 parent 5f44465 commit 14a1c51
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/services/encryption_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import 'package:path/path.dart';
class EncryptionService {
final RandomService _randomService = locator<RandomService>();

Future<File> encryptFile(File file) async {
Future<Map<String, dynamic>> encryptFile(File file) async {
logger.d('Started Encryption');
final crypt = AesCrypt();
final password = _randomService.getRandomString(16);
crypt.setPassword(password);
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<File> decryptFile(File file, String password) async {
Expand All @@ -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;
}
Expand Down
12 changes: 8 additions & 4 deletions lib/services/file_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ class FileService {
processing = true;
List<File> 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
Expand All @@ -49,10 +51,12 @@ class FileService {
final List<File> 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;
Expand Down
4 changes: 2 additions & 2 deletions lib/services/github_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GithubService {
final RandomService _randomService = locator<RandomService>();
final _env = dotenv.env;

Future<String> uploadFileAnonymous(File file) async {
Future<String> 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()),
Expand All @@ -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 ?? '';
}

Expand Down
5 changes: 3 additions & 2 deletions lib/services/shortener_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ class ShortenerService {
}
}

Future<String?> getShortUrl({required String url}) async {
Future<String?> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/shortner_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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"]));
});
});
Expand Down

0 comments on commit 14a1c51

Please sign in to comment.