upload and download file works
continuous-integration/drone/push Build was killed Details

master
remrem 3 years ago
parent 40f39801cd
commit 16639a0a25

1
.gitignore vendored

@ -2,6 +2,7 @@
.dart_tool/
.packages
# server.dart compiled executable
pubspec.lock
server
server.exe
# Conventional directory for build output.

@ -12,7 +12,7 @@ final _router = Router()
..get('/admin/users', API.getAllUsers)
..post('/user/salt', API.getSalt)
// POST (EN VRAI C'EST DES GET AVEC UN BODY)
..post('/user/password-file', API.downloadPasswordDb)
..post('/user/password-file-download', API.downloadPasswordDb)
..post('/auth', API.authenticator)
..post('/user/account', API.createAccount) // vrai post
// PUT
@ -51,7 +51,7 @@ void main(List<String> args) async {
final handler = Pipeline().addMiddleware(logRequests()).addHandler(_router);
// For running in containers, we respect the PORT environment variable.
final port = int.parse(Platform.environment['PORT'] ?? '8080');
final port = int.parse(Platform.environment['PORT'] ?? '8989');
final server = await serve(handler, ip, port);
print('Server listening on port ${server.port}');
}

@ -46,27 +46,6 @@ class API {
}
}
// Download sqlite password file
static Response downloadPasswordDb(Request req) {
final mail = req.params['mail'];
final password = req.params['cyphered_password_hash'];
// Database query -> return file (List<int>)
// Create stream from List<int>
// Rename file -> db_password_<mail>_<date>
// Send file
return Response.ok("");
/*
Stream<List<int>> fileStream = file.openRead();
return Response.ok(fileStream, headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment, filename="$reqFile"'
});
*/
}
// Create account
static Future<Response> createAccount(Request req) async {
final List<String> required = ["email", "password", "salt"];
@ -157,24 +136,36 @@ class API {
// Upload sqlite password file
static Future<Response> uploadPasswordDb(Request req) async {
sleep(Duration(seconds: 20));
Stream<List<int>> fileStream =
await req.read(); // await is needed even if IDE say no
List<List<int>> tmpFile = await fileStream.toList();
List<int> fileAsBytes = tmpFile[0];
final List<String> required = ["email", "password", "file"];
final body = await bodyToJson(req); // await is needed even if IDE say no
String fileAsBytes = body[required[2]];
var arrayBytes = fileAsBytes.split(',');
arrayBytes.removeLast();
var arrayBytes2 = arrayBytes.map(int.parse).toList();
await AccountsToPostgres.updatePasswordFile(body[required[0]], arrayBytes2);
File file = File("./passfile");
file.writeAsBytes(fileAsBytes);
file.writeAsBytes(arrayBytes2);
print(await file.stat());
return Response.ok("API: file received");
}
//File test = File("./haha.yu");
//await test.writeAsBytes(listBytes);
//print(await test.stat());
// Download sqlite password file
static Future<Response> downloadPasswordDb(Request req) async {
final List<String> required = ["email", "password"];
final body = await bodyToJson(req);
//print("Bytes: $listBytes");
//print("Lenght: $size");
return Response.ok("API: file received");
List<int> file =
await AccountsToPostgres.getPasswordFile(body[required[0]]);
for (int i in file) {
print(i);
}
return Response.ok(file.toString());
}
// Check if required fields are in req body

@ -116,17 +116,21 @@ class AccountsToPostgres {
}
// Update user password file
static Future<void> updatePasswordFile(String mail, File passwordFile) async {
List<int> passwordBlob =
utf8.encode(await passwordFile.readAsString(encoding: utf8));
static Future<void> updatePasswordFile(
String mail, List<int> passwordFile) async {
await connection.query(
"UPDATE \"Account\" SET password_file=@p WHERE mail=@mail",
substitutionValues: {"mail": mail, "p": passwordFile});
}
if (selectHashByMail(mail) == null) {
return;
} else {
await connection.query(
"UPDATE \"Account\" SET passwords=@p WHERE id=@identifiant",
substitutionValues: {"identifiant": mail, "p": passwordBlob});
}
static Future<List<int>> getPasswordFile(String mail) async {
List<List<dynamic>> data = await connection.query(
"SELECT password_file FROM \"Account\" WHERE mail=@mail",
substitutionValues: {
"mail": mail,
});
return data[0][0];
}
// Update user twoFa

Loading…
Cancel
Save