From 125b7ba1350a9bb76b6d8c9370a7b36f0e011430 Mon Sep 17 00:00:00 2001 From: Linloir <3145078758@qq.com> Date: Thu, 20 Oct 2022 22:22:41 +0800 Subject: [PATCH] Behavior Adjustment: - Change path to ${current}/.data/ --- Dockerfile | 11 +++++---- bin/tcp_server.dart | 7 ++++-- lib/database.dart | 40 ++++++++++++++++++++----------- lib/tcpcontroller/controller.dart | 4 ++-- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2d0e2d4..1c5040c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,13 +14,14 @@ RUN dart compile exe bin/tcp_server.dart -o bin/tcp_server FROM ubuntu:latest -RUN apt-get update -RUN apt-get -y install libsqlite3-0 libsqlite3-dev +RUN apt-get update && apt-get -y install libsqlite3-0 libsqlite3-dev # Copy the previously built executable into the scratch layer -COPY --from=compile /runtime/ ~/server/ -COPY --from=compile /lchatserver/bin/tcp_server ~/server/lchatserver/bin/ +RUN mkdir /lchatserver +COPY --from=compile /runtime/ /lchatserver/ +COPY --from=compile /lchatserver/bin/tcp_server /lchatserver/bin/ # Start server. EXPOSE 20706 -CMD ["~/server/lchatserver/bin/tcp_server"] \ No newline at end of file +WORKDIR /lchatserver/bin +CMD ["/lchatserver/bin/tcp_server"] \ No newline at end of file diff --git a/bin/tcp_server.dart b/bin/tcp_server.dart index f32d650..f045aa6 100644 --- a/bin/tcp_server.dart +++ b/bin/tcp_server.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-06 15:44:16 - * @LastEditTime : 2022-10-20 16:34:10 + * @LastEditTime : 2022-10-20 20:25:43 * @Description : */ @@ -17,10 +17,13 @@ import 'package:tcp_server/tcpcontroller/response.dart'; void main(List arguments) async { //Set address var port = arguments.isEmpty ? 20706 : int.tryParse(arguments[1]) ?? 20706; + + print('[L] [STARTUP ]-----------------------'); + print('[L] Running at directory ${Directory.current.path}'); //Create nessesary working directories - await Directory('${Directory.current.path}/.tmp').create(); await Directory('${Directory.current.path}/.data').create(); + await Directory('${Directory.current.path}/.data/.tmp').create(); await Directory('${Directory.current.path}/.data/files').create(); await DataBaseHelper().initialize(); diff --git a/lib/database.dart b/lib/database.dart index 764c500..2e5c7c7 100644 --- a/lib/database.dart +++ b/lib/database.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-06 16:15:01 - * @LastEditTime : 2022-10-20 13:14:40 + * @LastEditTime : 2022-10-20 20:58:57 * @Description : */ @@ -25,7 +25,7 @@ class DataBaseHelper { Future initialize() async { _database = await databaseFactoryFfi.openDatabase( - '${Directory.current.path}/.tmp/database.db', + '${Directory.current.path}/.data/.tmp/database.db', options: OpenDatabaseOptions( version: 1, onCreate: (db, version) async { @@ -34,7 +34,7 @@ class DataBaseHelper { ''' CREATE TABLE users ( userid integer primary key autoincrement, - username text not null, + username text unique not null, passwd text not null, avatar text ); @@ -241,17 +241,29 @@ class DataBaseHelper { //Insert into users try { - await _database.insert( - 'users', - { - 'username': identity.userName, - 'passwd': identity.userPasswd, - 'avatar': null - }, - conflictAlgorithm: ConflictAlgorithm.rollback - ); - } catch (conflict) { - throw Exception(['Database failure', conflict.toString()]); + await _database.transaction((txn) async { + var result = await txn.query( + 'users', + where: 'username = ?', + whereArgs: [ + identity.userName + ] + ); + if(result.isNotEmpty) { + throw Exception('Username already exists'); + } + await txn.insert( + 'users', + { + 'username': identity.userName, + 'passwd': identity.userPasswd, + 'avatar': null + }, + conflictAlgorithm: ConflictAlgorithm.rollback + ); + }); + } catch (e) { + rethrow; } //Get new userid diff --git a/lib/tcpcontroller/controller.dart b/lib/tcpcontroller/controller.dart index 0f3a6c7..41c0cb7 100644 --- a/lib/tcpcontroller/controller.dart +++ b/lib/tcpcontroller/controller.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-08 15:10:04 - * @LastEditTime : 2022-10-20 17:33:52 + * @LastEditTime : 2022-10-20 20:20:00 * @Description : */ @@ -133,7 +133,7 @@ class TCPController { //Create a future that listens to the status of the payload transmission () { var payloadPullStream = _payloadPullStreamController.stream; - var tempFile = File('${Directory.current.path}/.tmp/${DateTime.now().microsecondsSinceEpoch}$_fileCounter')..createSync(); + var tempFile = File('${Directory.current.path}/.data/.tmp/${DateTime.now().microsecondsSinceEpoch}$_fileCounter')..createSync(); _fileCounter += 1; _fileCounter %= 1000; Future(() async {