1#' Download file to disk
2#'
3#' Libcurl implementation of \code{C_download} (the "internal" download method)
4#' with added support for https, ftps, gzip, etc. Default behavior is identical
5#' to \code{\link{download.file}}, but request can be fully configured by passing
6#' a custom \code{\link{handle}}.
7#'
8#' The main difference between \code{curl_download} and \code{curl_fetch_disk}
9#' is that \code{curl_download} checks the http status code before starting the
10#' download, and raises an error when status is non-successful. The behavior of
11#' \code{curl_fetch_disk} on the other hand is to proceed as normal and write
12#' the error page to disk in case of a non success response.
13#'
14#' @useDynLib curl R_download_curl
15#' @param url A character string naming the URL of a resource to be downloaded.
16#' @param destfile A character string with the name where the downloaded file
17#'   is saved. Tilde-expansion is performed.
18#' @param quiet If \code{TRUE}, suppress status messages (if any), and the
19#'   progress bar.
20#' @param mode A character string specifying the mode with which to write the file.
21#'   Useful values are \code{"w"}, \code{"wb"} (binary), \code{"a"} (append)
22#'   and \code{"ab"}.
23#' @param handle a curl handle object
24#' @return Path of downloaded file (invisibly).
25#' @export
26#' @examples
27#' # Download large file
28#' \dontrun{
29#' url <- "http://www2.census.gov/acs2011_5yr/pums/csv_pus.zip"
30#' tmp <- tempfile()
31#' curl_download(url, tmp)
32#' }
33curl_download <- function(url, destfile, quiet = TRUE, mode = "wb", handle = new_handle()){
34  destfile <- enc2native(normalizePath(destfile, mustWork = FALSE))
35  nonblocking <- isTRUE(getOption("curl_interrupt", TRUE))
36  tmp <- enc2native(paste0(destfile, ".curltmp"))
37  on.exit(unlink(tmp))
38  .Call(R_download_curl, url, tmp, quiet, mode, handle, nonblocking)
39  file.rename(tmp, destfile)
40  invisible(destfile)
41}
42