next_chunk in errors (FileExists, ChunkAlreadyUploaded), FileFullyUploaded.

dev
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 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

View File

@ -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?