R/download_file.R

Defines functions download_file

Documented in download_file

#' Download a file from VirusTotal
#'
#' @param hash File hash (MD5, SHA1, or SHA256)
#' @param output_path Local path to save the downloaded file. Optional.
#' @param \dots Additional arguments passed to \code{\link{virustotal_GET}}.
#'
#' @return Raw file content or saves file to specified path
#'
#' @export
#'
#' @references \url{https://docs.virustotal.com/reference}
#'
#' @seealso \code{\link{set_key}} for setting the API key
#'
#' @examples \dontrun{
#'
#' # Before calling the function, set the API key using set_key('api_key_here')
#'
#' download_file(hash='99017f6eebbac24f351415dd410d522d',
#'               output_path='/tmp/downloaded_file')
#' }

download_file <- function(hash = NULL, output_path = NULL, ...) {

  assert_character(hash, len = 1, any.missing = FALSE, min.chars = 1)

  if (!is.null(output_path)) {
    assert_character(output_path, len = 1, any.missing = FALSE, min.chars = 1)
  }

  res <- GET("https://www.virustotal.com/",
             path = paste0("api/v3/files/", hash, "/download"),
             add_headers("x-apikey" = Sys.getenv("VirustotalToken")), ...)

  virustotal_check(res)

  if (!is.null(output_path)) {
    writeBin(content(res, "raw"), output_path)
    return(paste("File downloaded to:", output_path))
  } else {
    return(content(res, "raw"))
  }
}

Try the virustotal package in your browser

Any scripts or data that you put into this service are public.

virustotal documentation built on April 13, 2026, 9:07 a.m.