Debian Codegraph @

R/download.R

raw
1 #' Download file to disk
2 #'
3 #' Libcurl implementation of `C_download` (the "internal" download method)
4 #' with added support for https, ftps, gzip, etc. Default behavior is identical
5 #' to [download.file()], but request can be fully configured by passing
6 #' a custom [handle()].
7 #'
8 #' The main difference between `curl_download` and `curl_fetch_disk`
9 #' is that `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 #' `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 #' The `curl_download` function does support resuming and removes the temporary
15 #' file if the download did not complete successfully.
16 #' For a more advanced download interface which supports concurrent requests and
17 #' resuming large files, have a look at the [multi_download] function.
18 #'
19 #' @useDynLib curl R_download_curl
20 #' @seealso Advanced download interface: [multi_download]
21 #' @param url A character string naming the URL of a resource to be downloaded.
22 #' @param destfile A character string with the name where the downloaded file
23 #' is saved. Tilde-expansion is performed.
24 #' @param quiet If `TRUE`, suppress status messages (if any), and the
25 #' progress bar.
26 #' @param mode A character string specifying the mode with which to write the file.
27 #' Useful values are `"w"`, `"wb"` (binary), `"a"` (append)
28 #' and `"ab"`.
29 #' @param handle a curl handle object
30 #' @return Path of downloaded file (invisibly).
31 #' @export
32 #' @examples
33 #' # Download large file
34 #' \dontrun{
35 #' url <- "http://www2.census.gov/acs2011_5yr/pums/csv_pus.zip"
36 #' tmp <- tempfile()
37 #' curl_download(url, tmp)
38 #' }
39 curl_download <- function(url, destfile, quiet = TRUE, mode = "wb", handle = new_handle()){
40 destfile <- enc2native(normalizePath(destfile, mustWork = FALSE))
41 nonblocking <- isTRUE(getOption("curl_interrupt", TRUE))
42 tmp <- enc2native(paste0(destfile, ".curltmp"))
43 on.exit(unlink(tmp))
44 .Call(R_download_curl, url, tmp, quiet, mode, handle, nonblocking)
45 file.rename(tmp, destfile)
46 invisible(destfile)
47 }