From 0c14d8a32e9a46d7ba68998ba644efe49f4c3b10 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Sat, 4 Jan 2020 15:42:03 +0100
Subject: [PATCH] File transfer: ok.
---
src/server/handlers.cr | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/src/server/handlers.cr b/src/server/handlers.cr
index 62cb455..d707431 100644
--- a/src/server/handlers.cr
+++ b/src/server/handlers.cr
@@ -11,7 +11,7 @@ def hdl_transfer(message : FileStorage::Transfer,
mid = message.mid
mid ||= "no message id"
- # pp! transfer_message
+ # pp! message
file_info = user.uploads.select do |v|
v.file.digest == message.filedigest
@@ -20,11 +20,27 @@ def hdl_transfer(message : FileStorage::Transfer,
pp! file_info
# TODO: verify the digest
- # TODO: store the file
- # TODO: register the file, with its tags
- # puts "chunk: #{transfer_message.chunk}"
- # puts "data: #{Base64.decode transfer_message.data}"
+ # storage: Context.storage_directory/userid/fileuuid.bin
+ dir = "#{Context.storage_directory}/#{user.uid}"
+
+ 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|
+ # TODO: store the file
+ offset = (message.chunk.n - 1) * FileStorage.message_buffer_size
+ file.seek(offset, IO::Seek::Set)
+ data = Base64.decode message.data
+ file.write data
+ end
+
+ # TODO: register the file with dodb, with its tags
FileStorage::Response.new mid, "Ok"