Skip to content

Commit

Permalink
Use the mime package. Closes #148
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 12, 2014
1 parent 397fd08 commit d7f38bf
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 38 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Imports:
digest,
tools,
methods,
jsonlite
jsonlite,
mime
Suggests:
XML,
testthat (>= 0.8.0),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
correctly implement case insensitivity in header names. (#142, #146) thanks
to Håkon Malmedal (@hmalmedal) and Jim Hester (@jimhester).

* Deprecate `guess_media()`, and instead use `mime::guess_type()` (#148).

# httr 0.5

Expand Down
3 changes: 2 additions & 1 deletion R/body.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ body_config <- function(body = NULL, encode = "form") {
if (inherits(body, "FileUploadInfo")) {
con <- file(body$filename, "rb")
# FIXME: also need to close when done
mime_type <- body$contentType %||% guess_media(body$filename)
mime_type <- body$contentType %||%
mime::guess_type(body$filename, empty = NULL)
size <- file.info(body$filename)$size

return(body_httr(
Expand Down
3 changes: 2 additions & 1 deletion R/content.r
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
content <- function(x, as = NULL, type = NULL, encoding = NULL, ...) {
stopifnot(is.response(x))

type <- type %||% x$headers[["Content-Type"]] %||% guess_media_url(x$url)
type <- type %||% x$headers[["Content-Type"]] %||%
mime::guess_type(x$url, empty = NULL)

as <- as %||% parseability(type)
as <- match.arg(as, c("raw", "text", "parsed"))
Expand Down
6 changes: 3 additions & 3 deletions R/headers.r
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ add_headers <- function(..., .headers = character()) {
#' ask for json or xml responses or tell the server you are sending json/xml.
#'
#' @param type A mime type or a file extension. If a file extension (i.e. starts
#' with \code{.}) will guess the mime type using \code{\link{guess_media}}.
#' with \code{.}) will guess the mime type using \code{\link[mime]{guess_type}}.
#' @export
#' @examples
#' GET("http://httpbin.org/headers")
Expand All @@ -73,7 +73,7 @@ add_headers <- function(..., .headers = character()) {
#' GET("http://httpbin.org/headers", content_type(".xml"))
content_type <- function(type) {
if (substr(type, 1, 1) == ".") {
type <- guess_media(type)
type <- mime::guess_type(type, empty = NULL)
}

add_headers("Content-Type" = type)
Expand All @@ -93,7 +93,7 @@ content_type_xml <- function() content_type("application/xml")
#' @rdname content_type
accept <- function(type) {
if (substr(type, 1, 1) == ".") {
type <- guess_media(type)
type <- mime::guess_type(type, empty = NULL)
}
add_headers("Accept" = type)

Expand Down
29 changes: 5 additions & 24 deletions R/media-guess.r
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
#' Guess the media type of a path from its extension.
#'
#' DEPRECATED: please use \code{mime::guess_type} instead.
#'
#' @param x path to file
#' @keywords internal
#' @export
#' @examples
#' guess_media("report.doc")
#' guess_media("owl.png")
guess_media <- function(x) {
ext <- tools::file_ext(x)

if (ext %in% names(ext_media)) {
ext_media[[ext]]
} else {
NULL
}
}

guess_media_url <- function(x) {
guess_media(parse_url(x)$path)
}

cache_media <- function() {
url <- "http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types"
tbl <- scan(url, character(1), comment.char = "#", sep = "\n", quiet = TRUE)
pieces <- strsplit(tbl, "\\s+")

make_lookup <- function(x) setNames(rep(x[1], length(x) - 1), x[-1])
ext_media <- unlist(lapply(pieces, make_lookup), recursive = FALSE)
save(ext_media, file = "R/sysdata.rda")
.Deprecated("mime::guess_type")
mime::guess_type(x, empty = NULL)
}
Binary file removed R/sysdata.rda
Binary file not shown.
3 changes: 2 additions & 1 deletion R/upload-file.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @param path path to file
#' @param type mime type of path. If not supplied, will be guess by
#' \code{\link{guess_media}} when needed.
#' \code{\link[mime]{guess_type}} when needed.
#' @export
#' @examples
#' POST("http://httpbin.org/post",
Expand All @@ -13,3 +13,4 @@ upload_file <- function(path, type = NULL) {
stopifnot(is.character(path), length(path) == 1)
RCurl::fileUpload(path, contentType = type)
}

2 changes: 1 addition & 1 deletion man/content_type.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ accept_xml()
}
\arguments{
\item{type}{A mime type or a file extension. If a file extension (i.e. starts
with \code{.}) will guess the mime type using \code{\link{guess_media}}.}
with \code{.}) will guess the mime type using \code{\link[mime]{guess_type}}.}
}
\description{
These are convenient wrappers aroud \code{\link{add_headers}}.
Expand Down
7 changes: 2 additions & 5 deletions man/guess_media.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ guess_media(x)
\item{x}{path to file}
}
\description{
Guess the media type of a path from its extension.
}
\examples{
guess_media("report.doc")
guess_media("owl.png")
DEPRECATED: please use \code{mime::guess_type} instead.
}
\keyword{internal}

2 changes: 1 addition & 1 deletion man/upload_file.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ upload_file(path, type = NULL)
\item{path}{path to file}

\item{type}{mime type of path. If not supplied, will be guess by
\code{\link{guess_media}} when needed.}
\code{\link[mime]{guess_type}} when needed.}
}
\description{
This is a tiny wrapper for \pkg{RCurl}'s \code{\link[RCurl]{fileUpload}}.
Expand Down

0 comments on commit d7f38bf

Please sign in to comment.