Filestoraged can now be compiled.
parent
1c311178e6
commit
2b29d0db80
|
@ -1,4 +1,4 @@
|
||||||
name: filestorage
|
name: filestoraged
|
||||||
version: 0.1.0
|
version: 0.1.0
|
||||||
|
|
||||||
authors:
|
authors:
|
||||||
|
@ -14,11 +14,8 @@ dependencies:
|
||||||
git: https://git.baguette.netlib.re/Baguette/dodb.cr
|
git: https://git.baguette.netlib.re/Baguette/dodb.cr
|
||||||
authd:
|
authd:
|
||||||
git: https://git.baguette.netlib.re/Baguette/authd.git
|
git: https://git.baguette.netlib.re/Baguette/authd.git
|
||||||
branch: master
|
|
||||||
baguette-crystal-base:
|
baguette-crystal-base:
|
||||||
git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base
|
git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base
|
||||||
branch: master
|
|
||||||
|
|
||||||
|
|
||||||
targets:
|
targets:
|
||||||
filestoraged:
|
filestoraged:
|
||||||
|
@ -30,4 +27,4 @@ targets:
|
||||||
json-tests:
|
json-tests:
|
||||||
main: src/tests/json_tests.cr
|
main: src/tests/json_tests.cr
|
||||||
|
|
||||||
license: MIT
|
license: ISC
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
require "ipc"
|
require "ipc"
|
||||||
|
|
||||||
class FileStorage::Client < IPC::Client
|
class FileStorage::Client < IPC
|
||||||
property auth_token : String
|
property auth_token : String
|
||||||
|
|
||||||
def initialize(@auth_token, service_name = "filestorage")
|
def initialize(@auth_token, service_name = "filestorage")
|
||||||
super service_name
|
super
|
||||||
|
@server_fd = self.connect service_name
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: parse_message should raise exception if response not anticipated
|
# TODO: parse_message should raise exception if response not anticipated
|
||||||
|
@ -25,10 +26,10 @@ class FileStorage::Client < IPC::Client
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class FileStorage::Client < IPC::Client
|
class FileStorage::Client < IPC
|
||||||
def login
|
def login
|
||||||
request = FileStorage::Request::Login.new @auth_token
|
request = FileStorage::Request::Login.new @auth_token
|
||||||
send_now @server_fd.not_nil!, request
|
write @server_fd.not_nil!, request
|
||||||
parse_message [ FileStorage::Response::Login ], read
|
parse_message [ FileStorage::Response::Login ], read
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -57,7 +58,7 @@ class FileStorage::Client < IPC::Client
|
||||||
counter,
|
counter,
|
||||||
buffer[0 ... size]
|
buffer[0 ... size]
|
||||||
|
|
||||||
send_now @server_fd.not_nil!, transfer_message
|
write @server_fd.not_nil!, transfer_message
|
||||||
counter += 1
|
counter += 1
|
||||||
|
|
||||||
buffer = Bytes.new buffer_size
|
buffer = Bytes.new buffer_size
|
||||||
|
@ -80,7 +81,7 @@ class FileStorage::Client < IPC::Client
|
||||||
|
|
||||||
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
|
||||||
send_now @server_fd.not_nil!, request
|
write @server_fd.not_nil!, request
|
||||||
parse_message [ FileStorage::Response::Download ], read
|
parse_message [ FileStorage::Response::Download ], read
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -96,7 +97,7 @@ class FileStorage::Client < IPC::Client
|
||||||
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
|
||||||
send_now @server_fd.not_nil!, get_chunk_message
|
write @server_fd.not_nil!, get_chunk_message
|
||||||
response = parse_message [ FileStorage::Response::GetChunk ], read
|
response = parse_message [ FileStorage::Response::GetChunk ], read
|
||||||
|
|
||||||
case response
|
case response
|
||||||
|
@ -134,7 +135,7 @@ class FileStorage::Client < IPC::Client
|
||||||
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
|
||||||
send_now @server_fd.not_nil!, request
|
write @server_fd.not_nil!, request
|
||||||
end
|
end
|
||||||
|
|
||||||
parse_message [ FileStorage::Response::Upload ], read
|
parse_message [ FileStorage::Response::Upload ], read
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
# # If already in users_status:
|
# # If already in users_status:
|
||||||
# # check if the requests are the same
|
# # check if the requests are the same
|
||||||
# # if not: add them to the user structure in users_status
|
# # if not: add them to the user structure in users_status
|
||||||
# def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
|
# def handle(filestoraged : FileStorage::Service, event : IPC::Event)
|
||||||
#
|
#
|
||||||
# token = filestoraged.auth.decode_token @token
|
# token = filestoraged.auth.decode_token @token
|
||||||
# userid = token.uid
|
# userid = token.uid
|
||||||
|
|
|
@ -9,7 +9,7 @@ class FileStorage::Request
|
||||||
@mid = UUID.random.to_s
|
@mid = UUID.random.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
|
def handle(filestoraged : FileStorage::Service, event : IPC::Event)
|
||||||
user = filestoraged.get_logged_user event
|
user = filestoraged.get_logged_user event
|
||||||
|
|
||||||
raise NotLoggedException.new if user.nil?
|
raise NotLoggedException.new if user.nil?
|
||||||
|
|
|
@ -13,7 +13,7 @@ class FileStorage::Request
|
||||||
@mid = UUID.random.to_s
|
@mid = UUID.random.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
|
def handle(filestoraged : FileStorage::Service, event : IPC::Event)
|
||||||
logged_users = filestoraged.logged_users
|
logged_users = filestoraged.logged_users
|
||||||
|
|
||||||
user, _ = filestoraged.decode_token token
|
user, _ = filestoraged.decode_token token
|
||||||
|
|
|
@ -17,7 +17,7 @@ class FileStorage::Request
|
||||||
@mid = UUID.random.to_s
|
@mid = UUID.random.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
|
def handle(filestoraged : FileStorage::Service, event : IPC::Event)
|
||||||
user = filestoraged.get_logged_user event
|
user = filestoraged.get_logged_user event
|
||||||
|
|
||||||
raise NotLoggedException.new if user.nil?
|
raise NotLoggedException.new if user.nil?
|
||||||
|
@ -51,7 +51,7 @@ class FileStorage::Request
|
||||||
@mid = UUID.random.to_s
|
@mid = UUID.random.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
|
def handle(filestoraged : FileStorage::Service, event : IPC::Event)
|
||||||
user = filestoraged.get_logged_user event
|
user = filestoraged.get_logged_user event
|
||||||
|
|
||||||
raise NotLoggedException.new if user.nil?
|
raise NotLoggedException.new if user.nil?
|
||||||
|
|
|
@ -7,7 +7,7 @@ class FileStorage::Request
|
||||||
@mid = UUID.random.to_s
|
@mid = UUID.random.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
|
def handle(filestoraged : FileStorage::Service, event : IPC::Event)
|
||||||
user = filestoraged.get_logged_user event
|
user = filestoraged.get_logged_user event
|
||||||
|
|
||||||
raise NotLoggedException.new if user.nil?
|
raise NotLoggedException.new if user.nil?
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
|
require "ipc"
|
||||||
require "ipc/json"
|
require "ipc/json"
|
||||||
require "authd"
|
require "authd"
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ require "./network.cr"
|
||||||
require "dodb"
|
require "dodb"
|
||||||
|
|
||||||
class IPC::JSON
|
class IPC::JSON
|
||||||
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
|
def handle(filestoraged : FileStorage::Service, event : IPC::Event)
|
||||||
raise "unknown request"
|
raise "unknown request"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -67,7 +68,7 @@ end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FileStorage::Service < IPC::Server
|
class FileStorage::Service < IPC
|
||||||
# List of connected users (fd => uid).
|
# List of connected users (fd => uid).
|
||||||
property connected_users = Hash(Int32, Int32).new
|
property connected_users = Hash(Int32, Int32).new
|
||||||
|
|
||||||
|
@ -92,6 +93,7 @@ class FileStorage::Service < IPC::Server
|
||||||
@auth_key : String
|
@auth_key : String
|
||||||
|
|
||||||
def initialize(storage_directory, @auth_key, reindex : Bool)
|
def initialize(storage_directory, @auth_key, reindex : Bool)
|
||||||
|
super()
|
||||||
# Data and metadata storage directory.
|
# Data and metadata storage directory.
|
||||||
@storage = FileStorage::Storage.new storage_directory, reindex
|
@storage = FileStorage::Storage.new storage_directory, reindex
|
||||||
|
|
||||||
|
@ -101,10 +103,10 @@ class FileStorage::Service < IPC::Server
|
||||||
@auth = AuthD::Client.new
|
@auth = AuthD::Client.new
|
||||||
@auth.key = @auth_key
|
@auth.key = @auth_key
|
||||||
|
|
||||||
super "filestorage"
|
self.service_init "filestorage"
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_logged_user(event : IPC::Event::Events)
|
def get_logged_user(event : IPC::Event)
|
||||||
fd = event.fd
|
fd = event.fd
|
||||||
|
|
||||||
@logged_users[fd]?
|
@logged_users[fd]?
|
||||||
|
@ -129,9 +131,13 @@ class FileStorage::Service < IPC::Server
|
||||||
@storage.user_data_per_user.update_or_create user_data.uid.to_s, user_data
|
@storage.user_data_per_user.update_or_create user_data.uid.to_s, user_data
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_request(event : IPC::Event::MessageReceived)
|
def handle_request(event : IPC::Event)
|
||||||
|
|
||||||
|
array = event.message.not_nil!
|
||||||
|
slice = Slice.new array.to_unsafe, array.size
|
||||||
|
message = IPCMessage::TypedMessage.deserialize slice
|
||||||
|
request = FileStorage.requests.parse_ipc_json message.not_nil!
|
||||||
|
|
||||||
request = FileStorage.requests.parse_ipc_json event.message
|
|
||||||
if request.nil?
|
if request.nil?
|
||||||
raise "unknown request type"
|
raise "unknown request type"
|
||||||
end
|
end
|
||||||
|
@ -166,7 +172,7 @@ class FileStorage::Service < IPC::Server
|
||||||
# in the responses. Allows identifying responses easily.
|
# in the responses. Allows identifying responses easily.
|
||||||
response.id = request.id
|
response.id = request.id
|
||||||
|
|
||||||
send event.fd, response
|
schedule event.fd, response
|
||||||
|
|
||||||
response_name = response.class.name.sub /^FileStorage::(Response|Errors)::/, ""
|
response_name = response.class.name.sub /^FileStorage::(Response|Errors)::/, ""
|
||||||
if response.responds_to?(:reason)
|
if response.responds_to?(:reason)
|
||||||
|
@ -183,15 +189,15 @@ class FileStorage::Service < IPC::Server
|
||||||
begin
|
begin
|
||||||
|
|
||||||
case event
|
case event
|
||||||
when IPC::Event::Timer
|
when LibIPC::EventType::Timer
|
||||||
Baguette::Log.debug "IPC::Event::Timer" if @print_timer
|
Baguette::Log.debug "LibIPC::EventType::Timer" if @print_timer
|
||||||
|
|
||||||
when IPC::Event::Connection
|
when LibIPC::EventType::Connection
|
||||||
Baguette::Log.debug "IPC::Event::Connection: #{event.fd}"
|
Baguette::Log.debug "LibIPC::EventType::Connection: #{event.fd}"
|
||||||
@all_connections << event.fd
|
@all_connections << event.fd
|
||||||
|
|
||||||
when IPC::Event::Disconnection
|
when LibIPC::EventType::Disconnection
|
||||||
Baguette::Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
Baguette::Log.debug "LibIPC::EventType::Disconnection: #{event.fd}"
|
||||||
fd = event.fd
|
fd = event.fd
|
||||||
|
|
||||||
@logged_users.delete fd
|
@logged_users.delete fd
|
||||||
|
@ -201,26 +207,29 @@ class FileStorage::Service < IPC::Server
|
||||||
fd != event.fd
|
fd != event.fd
|
||||||
end
|
end
|
||||||
|
|
||||||
when IPC::Event::ExtraSocket
|
when LibIPC::EventType::External
|
||||||
Baguette::Log.warning "IPC::Event::ExtraSocket: should not happen in this service"
|
Baguette::Log.warning "LibIPC::EventType::ExtraSocket: should not happen in this service"
|
||||||
|
|
||||||
when IPC::Event::Switch
|
when LibIPC::EventType::SwitchTx
|
||||||
Baguette::Log.warning "IPC::Event::Switch: should not happen in this service"
|
Baguette::Log.warning "LibIPC::EventType::SwitchTx: should not happen in this service"
|
||||||
|
|
||||||
# IPC::Event::Message has to be the last entry
|
when LibIPC::EventType::SwitchRx
|
||||||
|
Baguette::Log.warning "LibIPC::EventType::SwitchRx: should not happen in this service"
|
||||||
|
|
||||||
|
# LibIPC::EventType::Message has to be the last entry
|
||||||
# because ExtraSocket and Switch inherit from Message class
|
# because ExtraSocket and Switch inherit from Message class
|
||||||
when IPC::Event::MessageReceived
|
when LibIPC::EventType::MessageRx
|
||||||
Baguette::Log.debug "IPC::Event::Message: #{event.fd}"
|
Baguette::Log.debug "LibIPC::EventType::MessageRx: #{event.fd}"
|
||||||
|
|
||||||
request_start = Time.utc
|
request_start = Time.utc
|
||||||
handle_request event
|
handle_request event
|
||||||
duration = Time.utc - request_start
|
duration = Time.utc - request_start
|
||||||
Baguette::Log.debug "request took: #{duration}"
|
Baguette::Log.debug "request took: #{duration}"
|
||||||
|
|
||||||
when IPC::Event::MessageSent
|
when LibIPC::EventType::MessageTx
|
||||||
Baguette::Log.debug "IPC::Event::MessageSent: #{event.fd}"
|
Baguette::Log.debug "LibIPC::EventType::MessageTx: #{event.fd}"
|
||||||
else
|
else
|
||||||
Baguette::Log.warning "unhandled IPC event: #{event.class}"
|
Baguette::Log.warning "unhandled LibIPC event: #{event.class}"
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue exception
|
rescue exception
|
||||||
|
@ -315,8 +324,7 @@ class FileStorage::Service < IPC::Server
|
||||||
authd_configuration.shared_key,
|
authd_configuration.shared_key,
|
||||||
configuration.db_reindex).tap do |service|
|
configuration.db_reindex).tap do |service|
|
||||||
|
|
||||||
service.base_timer = configuration.ipc_timer
|
service.timer configuration.ipc_timer
|
||||||
service.timer = configuration.ipc_timer
|
|
||||||
service.max_file_size = configuration.max_file_size
|
service.max_file_size = configuration.max_file_size
|
||||||
service.print_timer = configuration.print_ipc_timer
|
service.print_timer = configuration.print_ipc_timer
|
||||||
service.faulty = configuration.faulty
|
service.faulty = configuration.faulty
|
||||||
|
|
|
@ -2,9 +2,10 @@ require "ipc"
|
||||||
require "ipc/json"
|
require "ipc/json"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
class IPC::Context
|
class IPC
|
||||||
def send(fd : Int32, request : IPC::JSON)
|
def schedule(fd : Int32, request : IPC::JSON)
|
||||||
send fd, request.type.to_u8, request.to_json
|
m = IPCMessage::TypedMessage.new request.type.to_u8, request.to_json
|
||||||
|
schedule fd, m
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -14,9 +15,14 @@ module FileStorage
|
||||||
class_getter errors = [] of IPC::JSON.class
|
class_getter errors = [] of IPC::JSON.class
|
||||||
end
|
end
|
||||||
|
|
||||||
class FileStorage::Client < IPC::Client
|
class FileStorage::Client < IPC
|
||||||
def initialize
|
def initialize
|
||||||
initialize "filestorage"
|
super
|
||||||
|
fd = self.connect "filestorage"
|
||||||
|
if fd.nil?
|
||||||
|
raise "couldn't connect to 'auth' IPC service"
|
||||||
|
end
|
||||||
|
@server_fd = fd
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -25,5 +31,3 @@ require "../requests/transfer.cr"
|
||||||
require "../requests/upload.cr"
|
require "../requests/upload.cr"
|
||||||
require "../requests/errors.cr"
|
require "../requests/errors.cr"
|
||||||
require "../requests/download.cr"
|
require "../requests/download.cr"
|
||||||
|
|
||||||
# require "../requests/*"
|
|
||||||
|
|
|
@ -24,13 +24,9 @@ module FileStorage
|
||||||
|
|
||||||
# private function
|
# private function
|
||||||
def self.data_digest(data : Bytes)
|
def self.data_digest(data : Bytes)
|
||||||
iodata = IO::Memory.new data, false
|
digest = OpenSSL::Digest.new "sha256"
|
||||||
buffer = Bytes.new FileStorage.file_reading_buffer_size
|
digest.update data
|
||||||
|
digest.hexfinal
|
||||||
io = OpenSSL::DigestIO.new(iodata, "SHA256")
|
|
||||||
while io.read(buffer) > 0; end
|
|
||||||
|
|
||||||
io.digest.hexstring
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# private function
|
# private function
|
||||||
|
|
Loading…
Reference in New Issue