Skip to content

Commit

Permalink
moved to reading chunks directly in R and uploading those. Problem wi…
Browse files Browse the repository at this point in the history
…th signature on the subsequent uploads after the first. Signature issues
  • Loading branch information
duncantl committed Apr 7, 2013
1 parent e891a54 commit e594b7d
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions R/dropbox_chunked.r
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,65 @@
dropbox_chunked <-
function(cred, what, filename = basename(what), chunkSize = 4*10^6, ..., curl = getCurlHandle())
{
# raw and files
# totalSize = length(z)
input = CFILE(what, "rb")
totalSize = file.info(what)[1, "size"]
# source of data can be raw vectors, files and connections
if(is.character(what)) {
input = file(what, "rb")
totalSize = file.info(what)[1, "size"]
} else if(is.raw(what)) {
# raw buffer or a connection
input = rawConnection(what, "r")
totalSize = length(input)
} else if(is(what, "connection")) {
input = what
totalSize = NA
}

targetURL = "https://api-content.dropbox.com/1/chunked_upload"

# Need to reset the Content-Length, etc. in the final request
# so just clone the handle now.
dupCurl = dupCurlHandle(curl)

chunkSize = min(totalSize, chunkSize)

# Send the first block to obtain the upload_id token
block = readBin(input, raw(), chunkSize)
ans = OAuthRequest(cred, targetURL, , "PUT", upload = TRUE, verbose = TRUE,
readdata = input@ref, infilesize = chunkSize,
readfunction = block, infilesize = length(block),
httpheader = c('Content-Type' = "application/octet-stream"),
..., curl = curl)
tmp = fromJSON(ans)

offset = chunkSize + 1
tmp = fromJSON(ans)
offset = length(block) + 1
id = tmp[["upload_id"]]
while(tmp$offset < totalSize) {

while(is.na(totalSize) || offset < totalSize) {
browser()
tmp.url = sprintf("%s?upload_id=%s&offset=%d", targetURL, id, offset)
nbytes = min(totalSize - offset, chunkSize)
tmp.url = sprintf("%s?uploapd_id=%s&offset=%d", targetURL, id, offset)
block = readBin(input, raw(), chunkSize)
nbytes = length(block)
cat("sending", nbytes, "\n")
tmp = fromJSON(OAuthRequest(cred, tmp.url, , "PUT", upload = TRUE,
readdata = input@ref, infilesize = nbytes,
..., curl = curl))
#XXXX
# There is a problem with the signature on this request.
# Potential issue is the upload_id and offset arguments
# are in the URL but not in the request
# We may need to sign it as if they are parameters, but
# then put the parameters in the URL. ROAuth currently
# doesn't have a mechanism to specify this.
tmp = OAuthRequest(cred, tmp.url
#targetURL, list(upload_id = id, offset = offset),
"PUT", upload = TRUE,
readfunction = block, infilesize = nbytes,
..., curl = curl)
tmp = fromJSON(tmp)
if("error" %in% tmp)
stop("problem uploading chunk: ", tmp$error)

offset = offset + nbytes + 1
}

cat("committing upload", offset, "\n")
cat("committing upload to the file\n")
url = rDrop:::getPath(filename, "https://api-content.dropbox.com/1/commit_chunked_upload/dropbox", cred)

fromJSON(OAuthRequest(cred, url, list(overwrite = "true", upload_id = id), "POST", ..., curl = dupCurl, .opts = list(customrequest = "POST", httpheader = character())))
fromJSON(OAuthRequest(cred, url, list(overwrite = "true", upload_id = id),
"POST", ..., curl = dupCurl))

}

0 comments on commit e594b7d

Please sign in to comment.