Skip to content

Commit

Permalink
fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidatorCoder committed Nov 27, 2021
1 parent 36ba1a4 commit d4b1c29
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
19 changes: 12 additions & 7 deletions lib/services/download_service.dart
Expand Up @@ -2,21 +2,17 @@ import 'dart:io';

import 'package:android_path_provider/android_path_provider.dart';
import 'package:dio/dio.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/logger.dart';
import 'package:odin/services/random_service.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class DownloadService {
final RandomService _randomService = locator<RandomService>();
String progress = '0%';
bool downloading = false;

Dio dio = Dio();
Future<String> _getFilePath() async {
final fileName = "odin_" + _randomService.getRandomString(10);
Future<String> _getFilePath(String fileName) async {
Directory? dir;
if (Platform.isAndroid) {
var status = await Permission.storage.status;
Expand All @@ -29,15 +25,24 @@ class DownloadService {
} else {
dir = await getDownloadsDirectory();
}
String path = join(dir?.path ?? '', fileName + ".odin");
String path = join(dir?.path ?? '', fileName);
logger.d("File path : $path");
return path;
}

Future<File> downloadFile(String url) async {
final filePath = await _getFilePath();
progress = '0%';
downloading = true;
Response response = await Dio().get(
url,
options: Options(
followRedirects: false,
validateStatus: (status) {
return (status ?? 0) < 500;
}),
);
final filePath = await _getFilePath(
basename(response.headers.map["location"]?[0] ?? ''));
await dio.download(
url,
filePath,
Expand Down
6 changes: 3 additions & 3 deletions lib/services/encryption_service.dart
Expand Up @@ -15,7 +15,7 @@ class EncryptionService {
final password = _randomService.getRandomString(16);
crypt.setPassword(password);
final encryptedFilePath =
join(file.parent.path, basenameWithoutExtension(file.path) + '.odin');
join(file.parent.path, basename(file.path) + '.odin');
crypt.encryptFileSync(file.path, encryptedFilePath);
file.deleteSync(); // Delete the original zip file
logger.d('Finished Encryption');
Expand All @@ -28,8 +28,8 @@ class EncryptionService {
crypt.setPassword(password);
file = await file
.rename(file.path.substring(0, file.path.length - 5) + ".aes");
final decryptedFilePath = crypt.decryptFileSync(file.path,
join(file.parent.path, basenameWithoutExtension(file.path) + '.zip'));
final decryptedFilePath = crypt.decryptFileSync(
file.path, join(file.parent.path, basenameWithoutExtension(file.path)));
File decryptedFile = File(decryptedFilePath);
file.deleteSync(); // Delete the original AES file
logger.d('Finished Decryption');
Expand Down
40 changes: 31 additions & 9 deletions lib/services/file_service.dart
Expand Up @@ -9,6 +9,7 @@ import 'package:odin/services/logger.dart';
import 'package:odin/services/shortener_service.dart';
import 'package:odin/services/zip_service.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

class FileService {
final _shortenerService = locator<ShortenerService>();
Expand All @@ -32,7 +33,15 @@ class FileService {
uploading = true;
processing = true;
List<File> files = result.paths.map((path) => File(path!)).toList();
final zippedFile = await _zipService.zipFile(fileToZips: files);
File zippedFile;
if (files.length > 1) {
zippedFile = await _zipService.zipFile(fileToZips: files);
} else {
final Directory cacheDir = await getTemporaryDirectory();
zippedFile = files[0];
zippedFile =
zippedFile.copySync(join(cacheDir.path, basename(files[0].path)));
}
final encryptedFileDetails =
await _encrytionService.encryptFile(zippedFile);
zipfileName = basename(zippedFile.path);
Expand All @@ -54,9 +63,16 @@ class FileService {
zipfileName = '';
uploading = true;
processing = true;
final List<File> fileToZips =
urls.map((e) => File(e.toFilePath())).toList();
final zippedFile = await _zipService.zipFile(fileToZips: fileToZips);
final List<File> files = urls.map((e) => File(e.toFilePath())).toList();
File zippedFile;
if (files.length > 1) {
zippedFile = await _zipService.zipFile(fileToZips: files);
} else {
final Directory cacheDir = await getTemporaryDirectory();
zippedFile = files[0];
zippedFile =
zippedFile.copySync(join(cacheDir.path, basename(files[0].path)));
}
final encryptedFileDetails =
await _encrytionService.encryptFile(zippedFile);
zipfileName = basename(zippedFile.path);
Expand All @@ -81,10 +97,16 @@ class FileService {
processing = true;
final decryptedFile = await _encrytionService.decryptFile(file, password);
logger.d("decryptedFile path : ${decryptedFile.path}");
final unzippedFilePath = await _zipService.unzipFile(decryptedFile);
processing = false;
downloading = false;
logger.d("unzippedFile path : $unzippedFilePath");
return unzippedFilePath;
if (decryptedFile.path.endsWith('.zip')) {
final unzippedFilePath = await _zipService.unzipFile(decryptedFile);
logger.d("unzippedFile path : $unzippedFilePath");
processing = false;
downloading = false;
return unzippedFilePath;
} else {
processing = false;
downloading = false;
return decryptedFile.parent.path;
}
}
}

0 comments on commit d4b1c29

Please sign in to comment.