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/ .dart_tool/
.packages .packages
# server.dart compiled executable # server.dart compiled executable
pubspec.lock
server server
server.exe server.exe
# Conventional directory for build output. # Conventional directory for build output.

@ -12,7 +12,7 @@ final _router = Router()
..get('/admin/users', API.getAllUsers) ..get('/admin/users', API.getAllUsers)
..post('/user/salt', API.getSalt) ..post('/user/salt', API.getSalt)
// POST (EN VRAI C'EST DES GET AVEC UN BODY) // 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('/auth', API.authenticator)
..post('/user/account', API.createAccount) // vrai post ..post('/user/account', API.createAccount) // vrai post
// PUT // PUT
@ -51,7 +51,7 @@ void main(List<String> args) async {
final handler = Pipeline().addMiddleware(logRequests()).addHandler(_router); final handler = Pipeline().addMiddleware(logRequests()).addHandler(_router);
// For running in containers, we respect the PORT environment variable. // 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); final server = await serve(handler, ip, port);
print('Server listening on port ${server.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 // Create account
static Future<Response> createAccount(Request req) async { static Future<Response> createAccount(Request req) async {
final List<String> required = ["email", "password", "salt"]; final List<String> required = ["email", "password", "salt"];
@ -157,24 +136,36 @@ class API {
// Upload sqlite password file // Upload sqlite password file
static Future<Response> uploadPasswordDb(Request req) async { static Future<Response> uploadPasswordDb(Request req) async {
sleep(Duration(seconds: 20)); final List<String> required = ["email", "password", "file"];
Stream<List<int>> fileStream = final body = await bodyToJson(req); // await is needed even if IDE say no
await req.read(); // await is needed even if IDE say no
List<List<int>> tmpFile = await fileStream.toList(); String fileAsBytes = body[required[2]];
List<int> fileAsBytes = tmpFile[0];
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 file = File("./passfile");
file.writeAsBytes(fileAsBytes); file.writeAsBytes(arrayBytes2);
print(await file.stat()); print(await file.stat());
return Response.ok("API: file received");
}
//File test = File("./haha.yu"); // Download sqlite password file
//await test.writeAsBytes(listBytes); static Future<Response> downloadPasswordDb(Request req) async {
//print(await test.stat()); final List<String> required = ["email", "password"];
final body = await bodyToJson(req);
//print("Bytes: $listBytes"); List<int> file =
//print("Lenght: $size"); await AccountsToPostgres.getPasswordFile(body[required[0]]);
return Response.ok("API: file received"); for (int i in file) {
print(i);
}
return Response.ok(file.toString());
} }
// Check if required fields are in req body // Check if required fields are in req body

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

Loading…
Cancel
Save