Filestorage client now compiles.

This commit is contained in:
Philippe Pittoli 2023-02-10 10:37:23 +01:00
parent 2b29d0db80
commit 0d1326a566
3 changed files with 23 additions and 28 deletions

View File

@ -2,10 +2,22 @@ require "ipc"
class FileStorage::Client < IPC class FileStorage::Client < IPC
property auth_token : String property auth_token : String
property server_fd : Int32
def initialize(@auth_token, service_name = "filestorage") def initialize(@auth_token, service_name = "filestorage")
super super()
@server_fd = self.connect service_name @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 end
# TODO: parse_message should raise exception if response not anticipated # TODO: parse_message should raise exception if response not anticipated
@ -24,12 +36,10 @@ class FileStorage::Client < IPC
em << FileStorage::Errors::FileTooBig em << FileStorage::Errors::FileTooBig
em.parse_ipc_json message em.parse_ipc_json message
end end
end
class FileStorage::Client < IPC
def login def login
request = FileStorage::Request::Login.new @auth_token request = FileStorage::Request::Login.new @auth_token
write @server_fd.not_nil!, request write @server_fd, request
parse_message [ FileStorage::Response::Login ], read parse_message [ FileStorage::Response::Login ], read
end end
@ -58,7 +68,7 @@ class FileStorage::Client < IPC
counter, counter,
buffer[0 ... size] buffer[0 ... size]
write @server_fd.not_nil!, transfer_message write @server_fd, transfer_message
counter += 1 counter += 1
buffer = Bytes.new buffer_size buffer = Bytes.new buffer_size
@ -81,7 +91,7 @@ class FileStorage::Client < IPC
def download(filedigest = nil, name = nil, tags = nil) def download(filedigest = nil, name = nil, tags = nil)
request = FileStorage::Request::Download.new filedigest, name, tags request = FileStorage::Request::Download.new filedigest, name, tags
write @server_fd.not_nil!, request write @server_fd, request
parse_message [ FileStorage::Response::Download ], read parse_message [ FileStorage::Response::Download ], read
end end
@ -97,7 +107,7 @@ class FileStorage::Client < IPC
while counter < dl_response.file_info.nb_chunks while counter < dl_response.file_info.nb_chunks
Baguette::Log.debug "getting #{file_path}: chunk #{counter+1}/#{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 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 response = parse_message [ FileStorage::Response::GetChunk ], read
case response case response
@ -135,7 +145,7 @@ class FileStorage::Client < IPC
File.open(file) do |f| File.open(file) do |f|
file_info = FileStorage::FileInfo.new f file_info = FileStorage::FileInfo.new f
request = FileStorage::Request::Upload.new file_info request = FileStorage::Request::Upload.new file_info
write @server_fd.not_nil!, request write @server_fd, request
end end
parse_message [ FileStorage::Response::Upload ], read parse_message [ FileStorage::Response::Upload ], read

View File

@ -15,17 +15,6 @@ module FileStorage
class_getter errors = [] of IPC::JSON.class class_getter errors = [] of IPC::JSON.class
end 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/login.cr"
require "../requests/transfer.cr" require "../requests/transfer.cr"
require "../requests/upload.cr" require "../requests/upload.cr"

View File

@ -31,13 +31,9 @@ module FileStorage
# private function # private function
def self.file_digest(file : File) def self.file_digest(file : File)
# 1M read buffer digest = OpenSSL::Digest.new "sha256"
buffer = Bytes.new(1_000_000) digest.update file.gets_to_end()
digest.hexfinal
io = OpenSSL::DigestIO.new(file, "SHA256")
while io.read(buffer) > 0 ; end
io.digest.hexstring
end end
end end
@ -68,7 +64,7 @@ class FileStorage::FileInfo
def initialize(file : File, tags = nil) def initialize(file : File, tags = nil)
@name = File.basename file.path @name = File.basename file.path
@size = file.size @size = file.size.to_u64
@digest = FileStorage.file_digest file @digest = FileStorage.file_digest file
@nb_chunks = (@size / FileStorage.message_buffer_size).ceil.to_i @nb_chunks = (@size / FileStorage.message_buffer_size).ceil.to_i
@tags = tags || [] of String @tags = tags || [] of String