diff --git a/src/common/requests/errors.cr b/src/common/requests/errors.cr index 65171f6..a4e349e 100644 --- a/src/common/requests/errors.cr +++ b/src/common/requests/errors.cr @@ -43,8 +43,9 @@ class FileStorage::Errors property mid : String property reason = "file already present" property filedigest : String + property next_chunk : Int32 - def initialize(@mid, @filedigest) + def initialize(@mid, @filedigest, @next_chunk) end end FileStorage.errors << FileExists @@ -59,4 +60,15 @@ class FileStorage::Errors end end 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 diff --git a/src/server/storage.cr b/src/server/storage.cr index 5ad8a05..5305400 100644 --- a/src/server/storage.cr +++ b/src/server/storage.cr @@ -105,9 +105,14 @@ class FileStorage::Storage if transfer_info.chunks.select do |v| v == chunk_number end.size == 1 write_a_chunk file_digest, chunk_size, chunk_number, data else - # Send the next remaining chunk to upload. - next_chunk = transfer_info.chunks.sort.first - return FileStorage::Errors::ChunkAlreadyUploaded.new mid, file_digest, next_chunk + begin + # Send the next remaining chunk to upload. + 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 remove_chunk_from_db transfer_info, chunk_number @@ -185,7 +190,13 @@ class FileStorage::Storage else # File information already exists, request may be duplicated, # 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 # TODO: store upload request in UserData?