Compiling again.

This commit is contained in:
Karchnu 2020-05-16 01:40:09 +02:00
parent 0e4396c0c6
commit ff74b4f217
8 changed files with 94 additions and 33 deletions

View File

@ -14,6 +14,10 @@ dependencies:
git: https://git.baguette.netlib.re/Baguette/dodb.cr git: https://git.baguette.netlib.re/Baguette/dodb.cr
authd: authd:
git: https://github.com/Lukc/authd git: https://github.com/Lukc/authd
weird-crystal-base:
git: https://git.baguette.netlib.re/Baguette/weird-crystal-base
branch: master
targets: targets:
common-tests: common-tests:

View File

@ -10,7 +10,7 @@ class FileStorage::Request
end end
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
user = altideald.get_logged_user event user = filestoraged.get_logged_user event
raise Exception.new "unauthorized" if user.nil? raise Exception.new "unauthorized" if user.nil?
@ -19,7 +19,7 @@ class FileStorage::Request
user_data = filestoraged.get_user_data user.uid user_data = filestoraged.get_user_data user.uid
filestoraged.storage.download self, event filestoraged.storage.download self, user_data
rescue e rescue e
return Response::Error.new @mid, "unauthorized" return Response::Error.new @mid, "unauthorized"
end end

View File

@ -26,7 +26,7 @@ class FileStorage::Request
return Response::Login.new @mid return Response::Login.new @mid
rescue e rescue e
return Response::Error.new "unauthorized" return Response::Error.new @mid, "unauthorized"
end end
end end
FileStorage.requests << Login FileStorage.requests << Login

View File

@ -2,6 +2,10 @@ class FileStorage::Request
JSONIPC.request Transfer, 40 do JSONIPC.request Transfer, 40 do
property mid : String # autogenerated property mid : String # autogenerated
property filedigest : String # SHA256 digest of the entire file property filedigest : String # SHA256 digest of the entire file
# Chunk:
# - n : Int32 => chunk number
# - on : Int32 => number of chunks
# - digest : String => 1024-byte data in base64 format
property chunk : Chunk # For now, just the counter in a string property chunk : Chunk # For now, just the counter in a string
property data : String # base64 slice property data : String # base64 slice
def initialize(file_info : FileInfo, count, bindata) def initialize(file_info : FileInfo, count, bindata)
@ -14,7 +18,7 @@ class FileStorage::Request
end end
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
user = altideald.get_logged_user event user = filestoraged.get_logged_user event
raise Exception.new "unauthorized" if user.nil? raise Exception.new "unauthorized" if user.nil?
@ -23,7 +27,7 @@ class FileStorage::Request
user_data = filestoraged.get_user_data user.uid user_data = filestoraged.get_user_data user.uid
filestoraged.storage.transfer self, event filestoraged.storage.transfer self, user_data
rescue e rescue e
return Response::Error.new @mid, "unauthorized" return Response::Error.new @mid, "unauthorized"
end end

View File

@ -8,7 +8,7 @@ class FileStorage::Request
end end
def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events)
user = altideald.get_logged_user event user = filestoraged.get_logged_user event
raise Exception.new "unauthorized" if user.nil? raise Exception.new "unauthorized" if user.nil?
@ -17,7 +17,7 @@ class FileStorage::Request
user_data = filestoraged.get_user_data user.uid user_data = filestoraged.get_user_data user.uid
filestoraged.storage.upload self, event filestoraged.storage.upload self, user_data
rescue e rescue e
return Response::Error.new @mid, "unauthorized" return Response::Error.new @mid, "unauthorized"
end end
@ -49,15 +49,13 @@ class FileStorage::Response
end end
end end
JSONIPC.request Responses, 100 do # JSONIPC.request Responses, 100 do
include JSON::Serializable # property mid : String
# property responses : Array(Response) # a response for each request
property mid : String # property response : String
property responses : Array(Response) # a response for each request # property reason : String?
property response : String #
property reason : String? # def initialize(@mid, @response, @responses, @reason = nil)
# end
def initialize(@mid, @response, @responses, @reason = nil) # end
end
end
end end

View File

@ -3,6 +3,10 @@ require "ipc"
require "json" require "json"
require "authd" require "authd"
require "colorize"
require "weird-crystal-base"
require "../common/colors" require "../common/colors"
# require "../common/filestorage.cr" # require "../common/filestorage.cr"
@ -42,7 +46,7 @@ class FileStorage::Service < IPC::Service
# users_status: keep track of the users' status even if they are disconnected, # users_status: keep track of the users' status even if they are disconnected,
# allowing the application to handle connection problems. # allowing the application to handle connection problems.
property users_status = Hash(Int32, User).new property users_status : Hash(Int32, UserData) = Hash(Int32, UserData).new
# Actual storage. # Actual storage.
getter storage : FileStorage::Storage getter storage : FileStorage::Storage

View File

@ -6,8 +6,6 @@ require "openssl"
require "dodb" require "dodb"
require "base64" require "base64"
require "../common/utils"
require "./storage/*" require "./storage/*"
# private function # private function
@ -35,17 +33,26 @@ class FileStorage::Storage
property db_by_owner : DODB::Partition(TransferInfo) property db_by_owner : DODB::Partition(TransferInfo)
property db_by_tags : DODB::Tags(TransferInfo) property db_by_tags : DODB::Tags(TransferInfo)
def initialize(storage_directory, file_info_directory) # Where to store the data.
@db = DODB::DataBase(TransferInfo).new @file_info_directory property root : String
getter user_data : DODB::DataBase(UserData)
getter user_data_per_user : DODB::Index(UserData)
def initialize(@root, file_info_directory)
@db = DODB::DataBase(TransferInfo).new file_info_directory
# Create indexes, partitions and tags objects. # Create indexes, partitions and tags objects.
@db_by_filedigest = @db.new_index "filedigest", &.file_info.digest @db_by_filedigest = @db.new_index "filedigest", &.file_info.digest
@db_by_owner = @db.new_partition "owner", &.owner.to_s @db_by_owner = @db.new_partition "owner", &.owner.to_s
@db_by_tags = @db.new_tags "tags", &.file_info.tags @db_by_tags = @db.new_tags "tags", &.file_info.tags
@user_data = DODB::DataBase(UserData).new "#{@root}/user-data"
@user_data_per_user = @user_data.new_index "uid", &.uid.to_s
end end
# Reception of a file chunk. # Reception of a file chunk.
def transfer(message : FileStorage::Transfer, user : User) : FileStorage::Response def transfer(message : FileStorage::Request::Transfer, user : UserData)
# We received a message containing a chunk of file. # We received a message containing a chunk of file.
mid = message.mid mid = message.mid
@ -78,11 +85,11 @@ class FileStorage::Storage
FileStorage::Response::Transfer.new mid FileStorage::Response::Transfer.new mid
rescue e rescue e
puts "Error handling transfer: #{e.message}" puts "Error handling transfer: #{e.message}"
FileStorage::Response.new mid.not_nil!, "Not Ok", "Unexpected error: #{e.message}" FileStorage::Response::Error.new mid.not_nil!, "Unexpected error: #{e.message}"
end end
# the client sent an upload request # the client sent an upload request
def upload(request : FileStorage::Request::Upload, user : User) : FileStorage::Response def upload(request : FileStorage::Request::Upload, user : UserData)
mid = request.mid mid = request.mid
mid ||= "no message id" mid ||= "no message id"
@ -108,12 +115,12 @@ class FileStorage::Storage
FileStorage::Response::Upload.new request.mid FileStorage::Response::Upload.new request.mid
rescue e rescue e
puts "Error handling transfer: #{e.message}" puts "Error handling transfer: #{e.message}"
FileStorage::Response.new mid.not_nil!, "Not Ok", "Unexpected error: #{e.message}" FileStorage::Response::Error.new mid.not_nil!, "Unexpected error: #{e.message}"
end end
# TODO # TODO
# The client sent a download request. # The client sent a download request.
def download(request : FileStorage::DownloadRequest, user : User) : FileStorage::Response def download(request : FileStorage::Request::Download, user : UserData)
puts "hdl download: mid=#{request.mid}" puts "hdl download: mid=#{request.mid}"
pp! request pp! request
@ -121,12 +128,11 @@ class FileStorage::Storage
FileStorage::Response::Download.new request.mid FileStorage::Response::Download.new request.mid
end end
# Entry point for request management # Entry point for request management
# Each request should have a response. # Each request should have a response.
# Then, responses are sent in a single message. # Then, responses are sent in a single message.
def requests(requests : Array(FileStorage::Request), def requests(requests : Array(FileStorage::Request),
user : User, user : UserData,
event : IPC::Event::Message) : Array(FileStorage::Response) event : IPC::Event::Message) : Array(FileStorage::Response)
puts "hdl request" puts "hdl request"
@ -147,4 +153,50 @@ class FileStorage::Storage
responses responses
end end
def remove_chunk_from_db(transfer_info : TransferInfo, chunk_number : Int32)
transfer_info.chunks.delete chunk_number
@db_by_filedigest.update transfer_info.file_info.digest, transfer_info
end end
def write_a_chunk(userid : String,
file_info : FileStorage::FileInfo,
chunk_number : Int32,
data : Bytes)
# storage: @root/files/userid/fileuuid.bin
dir = "#{@root}/files/#{userid}"
FileUtils.mkdir_p dir
path = "#{dir}/#{file_info.digest}.bin"
# Create file if non existant
File.open(path, "a+") do |file|
end
# Write in it
File.open(path, "ab") do |file|
offset = chunk_number * FileStorage.message_buffer_size
file.seek(offset, IO::Seek::Set)
file.write data
end
end
end
### # TODO:
### # why getting the file_info here? We could check for the transfer_info right away
### # it has more info, and we'll get it later eventually
###
### file_info = nil
### begin
### file_info = user.uploads.select do |v|
### v.file.digest == message.filedigest
### end.first.file
###
### pp! file_info
### rescue e : IndexError
### puts "No recorded upload request for file #{message.filedigest}"
###
### rescue e
### puts "Unexpected error: #{e}"
### end

View File

@ -9,13 +9,12 @@ class FileStorage::UserData
include JSON::Serializable include JSON::Serializable
property uid : Int32 property uid : Int32
property token : AuthD::User::Public # property token : AuthD::User::Public?
property uploads : Array(FileStorage::Request::Upload) property uploads : Array(FileStorage::Request::Upload)
property downloads : Array(FileStorage::Request::Download) property downloads : Array(FileStorage::Request::Download)
def initialize(@token, def initialize(@uid,
@uploads = Array(FileStorage::Request::Upload).new, @uploads = Array(FileStorage::Request::Upload).new,
@downloads = Array(FileStorage::Request::Download).new) @downloads = Array(FileStorage::Request::Download).new)
@uid = token.uid
end end
end end