Skip to content

Commit

Permalink
Unpack zips only response content type suggests it's a zip
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Apr 29, 2019
1 parent 8856367 commit 6d87d53
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/S2_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ S2_download = function(url, destfile, zip = TRUE, skipExisting = 'samesize', pro
resp = curl::curl_fetch_memory(url[i], chHead)$headers
headers = curl::parse_headers(resp)
contentLength = c(as.integer(sub('^.* ', '', grep('^content-length: [0-9]+$', headers, value = TRUE, ignore.case = TRUE))), -1L)[1]
contentType = c(sub('^.* ', '', grep('^content-type: [-_./a-zA-Z0-9]+$', headers, value = TRUE, ignore.case = TRUE)), '')[1]

# when needed, perform a download
if (!file.exists(destfile[i]) | skipExisting == 'never' | file.size(destfile[i]) != contentLength) {
Expand All @@ -125,7 +126,7 @@ S2_download = function(url, destfile, zip = TRUE, skipExisting = 'samesize', pro

# unpacking zip files
signature = readBin(destfile[i], 'raw', 4)
if (all(signature == as.raw(c(80L, 75L, 3L, 4L))) & zip) {
if (all(signature == as.raw(c(80L, 75L, 3L, 4L))) & zip & contentType == 'application/zip') {
destfile[i] = sub('[.]zip$', '', destfile[i])
zipfile = paste0(destfile[i], '.zip')
file.rename(destfile[i], zipfile)
Expand Down

0 comments on commit 6d87d53

Please sign in to comment.