Skip to content

Commit

Permalink
refactor in favor of withr::local_tempfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
ijlyttle committed Jan 6, 2024
1 parent 7dee645 commit 2eb0612
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions R/boxr_read.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ box_read <- function(file_id, type = NULL, version_id = NULL,
req <- boxGet(file_id, local_file = temp_file, version_id = version_id,
version_no = version_no, download = TRUE)

# Extract the filename
# Extract the filename and extension
filename <- gsub(
'filename=\"|\"', '',
stringr::str_extract(
req$headers["content-disposition"][[1]],
'filename=\"(.*?)\"'
)
)
file_ext <- glue::glue(".{fs::path_ext(filename)}")

Check warning on line 71 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L71

Added line #L71 was not covered by tests

# Give the file it's original name back, so that you can preserve the file
# extension
new_name <- paste0(tempdir(), "/", filename)
file.rename(temp_file, new_name)
# Give the file its original file-extension back
temp_file_new <- withr::local_tempfile(fileext = file_ext)
file.rename(temp_file, temp_file_new)

Check warning on line 75 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L74-L75

Added lines #L74 - L75 were not covered by tests

# If the file doesn't have an obvious file extension, try and do the right
# thing by considering the mime-type from the request
if (!grepl("\\.[[:alnum:]]+$", new_name)) {
if (!grepl("\\.[[:alnum:]]+$", temp_file_new)) {

Check warning on line 79 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L79

Added line #L79 was not covered by tests
message("Cannot read file extension from name.\n",
"Inferring from mime-type...\n")
mime <- req$headers$`content-type`
Expand All @@ -88,13 +88,13 @@ box_read <- function(file_id, type = NULL, version_id = NULL,
# Supply the file format to read_fun, if it seems to accept them (the
# default, rio::import, does)
if ("format" %in% names(formals(read_fun))) {
cont <- read_fun(new_name, format = ext, ...)
cont <- read_fun(temp_file_new, format = ext, ...)

Check warning on line 91 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L91

Added line #L91 was not covered by tests
} else {
# Otherwise, just try and read it with a user-supplied function
cont <- read_fun(new_name, ...)
cont <- read_fun(temp_file_new, ...)

Check warning on line 94 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L94

Added line #L94 was not covered by tests
}
} else {
cont <- read_fun(new_name, ...)
cont <- read_fun(temp_file_new, ...)

Check warning on line 97 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L97

Added line #L97 was not covered by tests
}

# this code comment is old (i think) and maybe worth revisiting was rio goes to CRAN (NCD 2019-11-01)
Expand All @@ -107,7 +107,7 @@ box_read <- function(file_id, type = NULL, version_id = NULL,
}

message(
"Remote file '", new_name, "' read into memory as an object of class ",
"Remote file '", filename, "' read into memory as an object of class ",

Check warning on line 110 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L110

Added line #L110 was not covered by tests
paste(class(cont), collapse = ", "),
"\n"
)
Expand Down

0 comments on commit 2eb0612

Please sign in to comment.