From 0d1326a566bd31c4eb054b555de4ddaede057337 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Fri, 10 Feb 2023 10:37:23 +0100 Subject: [PATCH] Filestorage client now compiles. --- src/client/lib.cr | 28 +++++++++++++++++++--------- src/server/network.cr | 11 ----------- src/server/storage/file_info.cr | 12 ++++-------- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/client/lib.cr b/src/client/lib.cr index 8e58d9a..24d322c 100644 --- a/src/client/lib.cr +++ b/src/client/lib.cr @@ -2,10 +2,22 @@ require "ipc" class FileStorage::Client < IPC property auth_token : String + property server_fd : Int32 def initialize(@auth_token, service_name = "filestorage") - super - @server_fd = self.connect service_name + super() + @server_fd = 0 # Makes the compiler happy. + fd = self.connect service_name + if fd.nil? + raise "couldn't connect to '#{service_name}' IPC service" + end + @server_fd = fd.not_nil! + end + + def read + slice = self.read @server_fd + m = IPCMessage::TypedMessage.deserialize slice + m.not_nil! end # TODO: parse_message should raise exception if response not anticipated @@ -24,12 +36,10 @@ class FileStorage::Client < IPC em << FileStorage::Errors::FileTooBig em.parse_ipc_json message end -end -class FileStorage::Client < IPC def login request = FileStorage::Request::Login.new @auth_token - write @server_fd.not_nil!, request + write @server_fd, request parse_message [ FileStorage::Response::Login ], read end @@ -58,7 +68,7 @@ class FileStorage::Client < IPC counter, buffer[0 ... size] - write @server_fd.not_nil!, transfer_message + write @server_fd, transfer_message counter += 1 buffer = Bytes.new buffer_size @@ -81,7 +91,7 @@ class FileStorage::Client < IPC def download(filedigest = nil, name = nil, tags = nil) request = FileStorage::Request::Download.new filedigest, name, tags - write @server_fd.not_nil!, request + write @server_fd, request parse_message [ FileStorage::Response::Download ], read end @@ -97,7 +107,7 @@ class FileStorage::Client < IPC while counter < dl_response.file_info.nb_chunks Baguette::Log.debug "getting #{file_path}: chunk #{counter+1}/#{dl_response.file_info.nb_chunks}" get_chunk_message = FileStorage::Request::GetChunk.new digest, counter - write @server_fd.not_nil!, get_chunk_message + write @server_fd, get_chunk_message response = parse_message [ FileStorage::Response::GetChunk ], read case response @@ -135,7 +145,7 @@ class FileStorage::Client < IPC File.open(file) do |f| file_info = FileStorage::FileInfo.new f request = FileStorage::Request::Upload.new file_info - write @server_fd.not_nil!, request + write @server_fd, request end parse_message [ FileStorage::Response::Upload ], read diff --git a/src/server/network.cr b/src/server/network.cr index 1b024b4..8a0be36 100644 --- a/src/server/network.cr +++ b/src/server/network.cr @@ -15,17 +15,6 @@ module FileStorage class_getter errors = [] of IPC::JSON.class end -class FileStorage::Client < IPC - def initialize - super - fd = self.connect "filestorage" - if fd.nil? - raise "couldn't connect to 'auth' IPC service" - end - @server_fd = fd - end -end - require "../requests/login.cr" require "../requests/transfer.cr" require "../requests/upload.cr" diff --git a/src/server/storage/file_info.cr b/src/server/storage/file_info.cr index 508ce93..171c206 100644 --- a/src/server/storage/file_info.cr +++ b/src/server/storage/file_info.cr @@ -31,13 +31,9 @@ module FileStorage # private function def self.file_digest(file : File) - # 1M read buffer - buffer = Bytes.new(1_000_000) - - io = OpenSSL::DigestIO.new(file, "SHA256") - while io.read(buffer) > 0 ; end - - io.digest.hexstring + digest = OpenSSL::Digest.new "sha256" + digest.update file.gets_to_end() + digest.hexfinal end end @@ -68,7 +64,7 @@ class FileStorage::FileInfo def initialize(file : File, tags = nil) @name = File.basename file.path - @size = file.size + @size = file.size.to_u64 @digest = FileStorage.file_digest file @nb_chunks = (@size / FileStorage.message_buffer_size).ceil.to_i @tags = tags || [] of String