Skip to content

Commit

Permalink
Do not require 'curl' in webR
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Dec 20, 2023
1 parent ff8a806 commit 1312aa3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: magick
Type: Package
Title: Advanced Graphics and Image-Processing in R
Version: 2.8.1
Version: 2.8.2
Authors@R: person("Jeroen", "Ooms", role = c("aut", "cre"), email = "jeroen@berkeley.edu",
comment = c(ORCID = "0000-0002-4035-0289"))
Description: Bindings to 'ImageMagick': the most comprehensive open-source image
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
2.8.2
- Fix image_draw() for small images: #317
- Change image_shadow() to transparent background by default
- Downloading from a URL no longer requires 'curl' (to work in webR)

2.8.1
- Windows: update to libmagick 6.9.12-98
Expand Down
28 changes: 16 additions & 12 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ replace_url <- function(path){
}
}

# Uses file extension from HTTP content-type if available to help IM guess type.
download_url <- function(url){
req <- curl::curl_fetch_memory(url)
if(req$status_code >= 400)
stop(sprintf("Failed to download %s (HTTP %d)", url, req$status))
headers <- curl::parse_headers_list(req$headers)
ctype <- headers[['content-type']]
matches <- match(ctype, mimetypes$type)
extension <- if(length(matches) && !is.na(matches) && !grepl("(text|octet)", ctype)){
sub("*.", ".", mimetypes$pattern[matches[1]], fixed = TRUE)
tmp <- tempfile(fileext = sub("\\?.*", "", basename(url)))
if(requireNamespace('curl', quietly = TRUE)){
h <- curl::new_handle()
curl::curl_download(url, tmp, handle = h)
headers <- curl::parse_headers_list(curl::handle_data(h)$headers)
ctype <- headers[['content-type']]
matches <- match(ctype, mimetypes$type)
if(length(matches) && !is.na(matches) && !grepl("(text|octet)", ctype)){
extension <- sub("*.", ".", mimetypes$pattern[matches[1]], fixed = TRUE)
outfile <- tempfile(fileext = extension)
file.rename(tmp, outfile)
return(outfile)
}
} else {
sub("\\?.*", "", basename(url))
utils::download.file(url, tmp, quiet = TRUE) #Fallback for webR
}
filename <- tempfile(fileext = extension)
writeBin(req$content, filename)
return(filename)
return(tmp)
}

assert_image <- function(image){
Expand Down
5 changes: 5 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ fi
# Write to Makevars
sed -e "s|@cflags@|$PKG_CFLAGS|" -e "s|@libs@|$PKG_LIBS|" src/Makevars.in > src/Makevars

# Dont use curl on webR
if [ `uname` = "Emscripten" ]; then
sed -i.bak 's/curl/utils/g' DESCRIPTION
fi

# Success
exit 0

0 comments on commit 1312aa3

Please sign in to comment.