From 16639a0a2563080dd7e32aa7cfb8732458a56314 Mon Sep 17 00:00:00 2001 From: "remi.arnal" Date: Thu, 19 Jan 2023 01:11:35 +0100 Subject: [PATCH] upload and download file works --- .gitignore | 1 + bin/server.dart | 4 +- lib/api/api.dart | 57 +++++++++++--------------- lib/database/accounts_to_postgres.dart | 24 ++++++----- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index db5f872..c06da3e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .dart_tool/ .packages # server.dart compiled executable +pubspec.lock server server.exe # Conventional directory for build output. diff --git a/bin/server.dart b/bin/server.dart index 3fe0514..6099240 100755 --- a/bin/server.dart +++ b/bin/server.dart @@ -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 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}'); } diff --git a/lib/api/api.dart b/lib/api/api.dart index 8a8e1fc..d88ca11 100644 --- a/lib/api/api.dart +++ b/lib/api/api.dart @@ -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) - // Create stream from List - // Rename file -> db_password__ - // Send file - - return Response.ok(""); - - /* - Stream> fileStream = file.openRead(); - return Response.ok(fileStream, headers: { - 'Content-Type': 'application/octet-stream', - 'Content-Disposition': 'attachment, filename="$reqFile"' - }); - */ - } - // Create account static Future createAccount(Request req) async { final List required = ["email", "password", "salt"]; @@ -157,24 +136,36 @@ class API { // Upload sqlite password file static Future uploadPasswordDb(Request req) async { - sleep(Duration(seconds: 20)); - Stream> fileStream = - await req.read(); // await is needed even if IDE say no - List> tmpFile = await fileStream.toList(); - List fileAsBytes = tmpFile[0]; + final List 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 downloadPasswordDb(Request req) async { + final List required = ["email", "password"]; + final body = await bodyToJson(req); - //print("Bytes: $listBytes"); - //print("Lenght: $size"); - return Response.ok("API: file received"); + List 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 diff --git a/lib/database/accounts_to_postgres.dart b/lib/database/accounts_to_postgres.dart index 984b898..a9c9766 100644 --- a/lib/database/accounts_to_postgres.dart +++ b/lib/database/accounts_to_postgres.dart @@ -116,17 +116,21 @@ class AccountsToPostgres { } // Update user password file - static Future updatePasswordFile(String mail, File passwordFile) async { - List passwordBlob = - utf8.encode(await passwordFile.readAsString(encoding: utf8)); + static Future updatePasswordFile( + String mail, List 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> getPasswordFile(String mail) async { + List> data = await connection.query( + "SELECT password_file FROM \"Account\" WHERE mail=@mail", + substitutionValues: { + "mail": mail, + }); + + return data[0][0]; } // Update user twoFa