97 lines
3.1 KiB
Dart
97 lines
3.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:encrypt/encrypt.dart';
|
|
import 'package:manager_app/Components/message_notification.dart';
|
|
import 'package:manager_app/Models/session.dart';
|
|
import 'package:manager_app/client.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:managerapi/api.dart';
|
|
//import 'package:path_provider/path_provider.dart';
|
|
|
|
class FileHelper {
|
|
final key = Key.fromUtf8('aVs:ZMe3EK-yS<y:;k>vCGrj3T8]yG6E');
|
|
final iv = IV.fromLength(16);
|
|
|
|
|
|
Future<String> get _localPath async {
|
|
//final directory = await getApplicationDocumentsDirectory();
|
|
var directory;
|
|
return directory.path;
|
|
}
|
|
|
|
Future<File> get _localFile async {
|
|
final path = await _localPath;
|
|
return File('$path/session.json');
|
|
}
|
|
|
|
Future<File> writeSession(Session session) async {
|
|
final file = await _localFile;
|
|
|
|
if (session.password != null) {
|
|
var encrypter = Encrypter(AES(key));
|
|
var encrypted = encrypter.encrypt(session.password, iv: iv);
|
|
|
|
session.password = encrypted.base64;
|
|
}
|
|
// Write the file
|
|
return file.writeAsString(jsonEncode(session.toMap()));
|
|
}
|
|
|
|
Future<File> storeConfiguration(ExportConfigurationDTO exportConfigurationDTO) async {
|
|
final path = await _localPath;
|
|
new File('$path/'+exportConfigurationDTO.label+'.json').createSync(recursive: true);
|
|
|
|
// Write the file
|
|
File file = File('$path/'+exportConfigurationDTO.label+'.json');
|
|
return file.writeAsString(jsonEncode(exportConfigurationDTO.toJson()));
|
|
}
|
|
|
|
Future<void> importConfiguration(String path, Client client, context) async {
|
|
// Gets the file
|
|
File file = File(path);
|
|
|
|
final contents = await file.readAsString();
|
|
|
|
ExportConfigurationDTO export = ExportConfigurationDTO.fromJson(jsonDecode(contents));
|
|
try {
|
|
String test = await client.configurationApi.configurationImport(export);
|
|
if (test.contains("successfully")) {
|
|
showNotification(kSuccess, kWhite, 'La configuration a été importée avec succès', context, null);
|
|
} else {
|
|
showNotification(kPrimaryColor, kWhite, 'Une erreur est survenue lors de l''import de la configuration', context, null);
|
|
}
|
|
} catch (e) {
|
|
if (e.toString().contains('409')) {
|
|
showNotification(kSecond, kWhite, 'La configuration existe déjà dans le système', context, null);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<Session> readSession() async {
|
|
try {
|
|
final file = await _localFile;
|
|
// Read the file
|
|
final contents = await file.readAsString();
|
|
|
|
Session session = Session.fromJson(jsonDecode(contents));
|
|
|
|
var encrypter = Encrypter(AES(key));
|
|
|
|
if (session.password != null) {
|
|
session.password = encrypter.decrypt64(session.password, iv: iv);
|
|
}
|
|
|
|
return session;
|
|
} catch (e) {
|
|
if (e.toString().contains("cannot find the file")) {
|
|
final path = await _localPath;
|
|
new File('$path/session.json').createSync(recursive: true);
|
|
print("file created");
|
|
await writeSession(new Session(rememberMe: false));
|
|
}
|
|
// If encountering an error, return 0
|
|
return Session(rememberMe: false);
|
|
}
|
|
}
|
|
} |