Skip to content

Commit

Permalink
Update code to use new content hash API
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Mar 20, 2019
1 parent 34bd284 commit 8902f39
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
26 changes: 8 additions & 18 deletions src/DropboxSDK.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ include("types.jl")

################################################################################

# """
# struct Error
# dict::Dict{String, Any}
# end
#
# Return value if a request failed. The content is a dictionary
# containing the parsed JSON error response.
# """
# struct Error
# dict::Dict{String, Any}
# end



export Authorization
Expand Down Expand Up @@ -559,7 +547,7 @@ function files_upload(auth::Authorization,
content::ContentIterator)::FileMetadata
session_id = nothing
offset = Int64(0)
cstate = calc_content_hash_init()
cdata, chash = content_hasher()
while !isempty(content.iterator)
chunk = popfirst!(content.iterator)::Vector{UInt8}
isempty(chunk) && continue
Expand All @@ -582,7 +570,7 @@ function files_upload(auth::Authorization,
args, chunk)
end
offset = offset + length(chunk)
calc_content_hash_add!(cstate, chunk)
put!(cdata, chunk)
end
if session_id === nothing
# The file was empty
Expand Down Expand Up @@ -611,7 +599,8 @@ function files_upload(auth::Authorization,
end

# Check content hash
content_hash = calc_content_hash_get(cstate)
close(cdata)
content_hash = take!(chash)
if metadata.content_hash != content_hash
throw(DropboxError(
Dict("error" => "LocalContentHashMismatch",
Expand Down Expand Up @@ -659,7 +648,7 @@ function files_upload(auth::Authorization,

session_id = nothing
offset = Int64(0)
cstate = calc_content_hash_init()
cdata, chash = content_hasher()
while !isempty(content.iterator)
chunk = popfirst!(content.iterator)::Vector{UInt8}
isempty(chunk) && continue
Expand All @@ -683,7 +672,7 @@ function files_upload(auth::Authorization,
args, chunk)
end
offset = offset + length(chunk)
calc_content_hash_add!(cstate, chunk)
put!(cdata, chunk)
end
# TODO: We need to close only the last session. But what does
# "last" mean? Is it "last session id passed to
Expand All @@ -710,7 +699,8 @@ function files_upload(auth::Authorization,
args, UInt8[])
end

content_hash = calc_content_hash_get(cstate)
close(cdata)
content_hash = take!(chash)
push!(upload_states,
UploadState(path, session_id, offset, content_hash, nothing))
end
Expand Down
8 changes: 0 additions & 8 deletions test/testsdk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ end
@assert sum(length.(contents)) == length(content)
# @assert vcat(contents...) == content

cstate = calc_content_hash_init()
for content in contents
calc_content_hash_add!(cstate, content)
end
content_hash = calc_content_hash_get(cstate)
@test (content_hash ==
"8a6ebea6983dc68be1575676d3a8ec0d664cfee69b2dbcdf44087cf5d455fe12")

cdata, chash = content_hasher()
for content in contents
put!(cdata, content)
Expand Down

0 comments on commit 8902f39

Please sign in to comment.