Bob Rudis
8 years ago
19 changed files with 162 additions and 80 deletions
@ -1,28 +1,29 @@ |
|||||
Package: wand |
Package: wand |
||||
Type: Package |
Type: Package |
||||
Title: Retrieve 'Magic' Attributes from Files and Directories |
Title: Retrieve 'Magic' Attributes from Files and Directories |
||||
Version: 0.1.0 |
Version: 0.2.0 |
||||
Date: 2016-08-12 |
Date: 2016-08-13 |
||||
Author: Bob Rudis (@hrbrmstr), Christos Zoulas [libmagic] |
Author: Bob Rudis (@hrbrmstr), Christos Zoulas [libmagic] |
||||
Maintainer: Bob Rudis <bob@rudis.net> |
Maintainer: Bob Rudis <bob@rud.is> |
||||
Description: The 'libmagic' library provides functions to determine |
Description: The 'libmagic' library provides functions to determine |
||||
mime type and other metadata from files through their "magic" |
mime type and other metadata from files through their "magic" |
||||
attributes. |
attributes. |
||||
URL: http://github.com/hrbrmstr/wand |
URL: http://github.com/hrbrmstr/wand |
||||
BugReports: https://github.com/hrbrmstr/wand/issues |
BugReports: https://github.com/hrbrmstr/wand/issues |
||||
SystemRequirements: libmagic |
SystemRequirements: libmagic (>= 5.14) |
||||
License: AGPL |
License: AGPL |
||||
Suggests: |
Suggests: |
||||
testthat, |
testthat |
||||
tibble, |
|
||||
magrittr, |
|
||||
dplyr |
|
||||
Depends: |
Depends: |
||||
R (>= 3.0.0) |
R (>= 3.0.0) |
||||
Imports: |
Imports: |
||||
purrr, |
purrr, |
||||
Rcpp, |
Rcpp, |
||||
rappdirs, |
rappdirs, |
||||
utils |
tibble, |
||||
|
utils, |
||||
|
tidyr, |
||||
|
stringi, |
||||
|
dplyr |
||||
LinkingTo: Rcpp |
LinkingTo: Rcpp |
||||
RoxygenNote: 5.0.1 |
RoxygenNote: 5.0.1 |
||||
|
@ -1,2 +1,5 @@ |
|||||
0.1.0 |
# 0.2.0 |
||||
|
* Works on Windows |
||||
|
|
||||
|
# 0.1.0 |
||||
* Initial release |
* Initial release |
||||
|
@ -0,0 +1,76 @@ |
|||||
|
#' Retrieve 'magic' attributes from files and directories |
||||
|
#' |
||||
|
#' @param path character vector of files to use magic on |
||||
|
#' @param magic_db either "\code{system}" (the default) to use the system |
||||
|
#' \code{magic} database or an atomic character vector with a |
||||
|
#' colon-separated list of full paths to custom \code{magic} database(s). |
||||
|
#' @return a \code{tibble} / \code{data.frame} of file magic attributes. |
||||
|
#' Specifically, mime type, encoding, possible file extensions and |
||||
|
#' type description are returned as colums in the data frame along |
||||
|
#' with \code{path}. |
||||
|
#' @note Various fields might not be available depending on the version |
||||
|
#' of \code{libmagic} you have installed. |
||||
|
#' @references See \url{http://openpreservation.org/blog/2012/08/09/magic-editing-and-creation-primer/} |
||||
|
#' for information on how to create your own \code{magic} database |
||||
|
#' @export |
||||
|
#' @examples |
||||
|
#' library(magrittr) |
||||
|
#' library(dplyr) |
||||
|
#' |
||||
|
#' system.file("img", package="filemagic") %>% |
||||
|
#' list.files(full.names=TRUE) %>% |
||||
|
#' incant() %>% |
||||
|
#' glimpse() |
||||
|
incant <- function(path, magic_db="system") { |
||||
|
|
||||
|
if (get_os() == "win") { |
||||
|
|
||||
|
file_exe <- system.file("exec/file.exe", package="wand") |
||||
|
|
||||
|
magic_db <- normalizePath(magic_wand_file()) |
||||
|
|
||||
|
tf <- tempfile() |
||||
|
writeLines(path, tf) |
||||
|
|
||||
|
system2(file_exe, |
||||
|
c("--mime-type", "--mime-encoding", "--no-buffer", "--preserve-date", |
||||
|
'--separator "||"', sprintf('--magic-file "%s"', magic_db), |
||||
|
sprintf('--files-from "%s"', tf)), |
||||
|
stdout=TRUE) -> output_1 |
||||
|
|
||||
|
system2(file_exe, |
||||
|
c("--no-buffer", "--preserve-date", '--separator "||"', |
||||
|
sprintf('--magic-file "%s"', magic_db), |
||||
|
sprintf('--files-from "%s"', tf)), |
||||
|
stdout=TRUE) -> output_2 |
||||
|
|
||||
|
unlink(tf) |
||||
|
|
||||
|
stri_split_fixed(output_1, "||", n=2, simplify=TRUE) %>% |
||||
|
as_data_frame() %>% |
||||
|
setNames(c("file", "response")) %>% |
||||
|
separate(response, c("mime_type", "encoding"), sep=";", extra="drop", fill="right") %>% |
||||
|
mutate(encoding=stri_replace_first_regex(encoding, "charset=", "")) -> df1 |
||||
|
|
||||
|
stri_split_fixed(output_2, "||", n=2, simplify=TRUE) %>% |
||||
|
as_data_frame() %>% |
||||
|
setNames(c("file", "description")) -> df2 |
||||
|
|
||||
|
left_join(df1, df2, by="file") |
||||
|
|
||||
|
} else { |
||||
|
incant_(path, magic_db) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
get_os <- function () { |
||||
|
if (.Platform$OS.type == "windows") { |
||||
|
"win" |
||||
|
} else if (Sys.info()["sysname"] == "Darwin") { |
||||
|
"mac" |
||||
|
} else if (.Platform$OS.type == "unix") { |
||||
|
"unix" |
||||
|
} else { |
||||
|
stop("Unknown OS") |
||||
|
} |
||||
|
} |
Binary file not shown.
Binary file not shown.
@ -1 +1,8 @@ |
|||||
PKG_LIBS = -lmagic |
PKG_LIBS = -lmagic |
||||
|
|
||||
|
all: clean |
||||
|
|
||||
|
clean: |
||||
|
rm -f $(OBJECTS) wand.dll |
||||
|
|
||||
|
.PHONY: all clean |
@ -0,0 +1,9 @@ |
|||||
|
all: winlibs |
||||
|
|
||||
|
clean: |
||||
|
rm -f $(OBJECTS) wand.dll |
||||
|
|
||||
|
winlibs: |
||||
|
"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" "../tools/getfile.R" |
||||
|
|
||||
|
.PHONY: all winlibs clean |
@ -0,0 +1,5 @@ |
|||||
|
if(getRversion() < "3.3.0") setInternet2() |
||||
|
download.file("https://www.stats.ox.ac.uk/pub/Rtools/goodies/file-5.03.zip", "file.zip", quiet = TRUE) |
||||
|
dir.create("../inst/exec", showWarnings = FALSE) |
||||
|
unzip("file.zip", "file.exe", exdir = "../inst/exec") |
||||
|
unlink("file.zip") |
Loading…
Reference in new issue