next_chunk in errors (FileExists, ChunkAlreadyUploaded), FileFullyUploaded.

This commit is contained in:
Karchnu 2020-06-08 23:02:31 +02:00
parent 854f383172
commit c926e5334a
2 changed files with 28 additions and 5 deletions

View File

@ -43,8 +43,9 @@ class FileStorage::Errors
property mid : String property mid : String
property reason = "file already present" property reason = "file already present"
property filedigest : String property filedigest : String
property next_chunk : Int32
def initialize(@mid, @filedigest) def initialize(@mid, @filedigest, @next_chunk)
end end
end end
FileStorage.errors << FileExists FileStorage.errors << FileExists
@ -59,4 +60,15 @@ class FileStorage::Errors
end end
end end
FileStorage.errors << FileDoesNotExist FileStorage.errors << FileDoesNotExist
# When a file was already fully uploaded.
JSONIPC.request FileFullyUploaded, 206 do
property mid : String
property reason = "file already uploaded fully"
property filedigest : String
def initialize(@mid, @filedigest)
end
end
FileStorage.errors << FileFullyUploaded
end end

View File

@ -105,9 +105,14 @@ class FileStorage::Storage
if transfer_info.chunks.select do |v| v == chunk_number end.size == 1 if transfer_info.chunks.select do |v| v == chunk_number end.size == 1
write_a_chunk file_digest, chunk_size, chunk_number, data write_a_chunk file_digest, chunk_size, chunk_number, data
else else
# Send the next remaining chunk to upload. begin
next_chunk = transfer_info.chunks.sort.first # Send the next remaining chunk to upload.
return FileStorage::Errors::ChunkAlreadyUploaded.new mid, file_digest, next_chunk next_chunk = transfer_info.chunks.sort.first
return FileStorage::Errors::ChunkAlreadyUploaded.new mid, file_digest, next_chunk
rescue e : IndexError
# In case the file was completely uploaded already.
return FileStorage::Errors::FileFullyUploaded.new mid, file_digest
end
end end
remove_chunk_from_db transfer_info, chunk_number remove_chunk_from_db transfer_info, chunk_number
@ -185,7 +190,13 @@ class FileStorage::Storage
else else
# File information already exists, request may be duplicated, # File information already exists, request may be duplicated,
# in this case: ignore the upload request. # in this case: ignore the upload request.
return FileStorage::Errors::FileExists.new mid, file_digest begin
next_chunk = transfer_info.chunks.sort.first
return FileStorage::Errors::FileExists.new mid, file_digest, next_chunk
rescue e : IndexError
# In case the file was completely uploaded already.
return FileStorage::Errors::FileFullyUploaded.new mid, file_digest
end
end end
# TODO: store upload request in UserData? # TODO: store upload request in UserData?