Skip to content

Commit

Permalink
log output and add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
NewGraphEnvironment committed Nov 1, 2024
1 parent 5a8957a commit 0b2175e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ importFrom(bcdata,bcdc_describe_feature)
importFrom(bcdata,bcdc_query_geodata)
importFrom(bcdata,bcdc_search)
importFrom(chk,chk_character)
importFrom(chk,chk_ext)
importFrom(chk,chk_file)
importFrom(chk,chk_string)
importFrom(cli,cli_abort)
Expand Down
48 changes: 41 additions & 7 deletions R/rfp_photo_metadata_rm.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#' @param overwrite_original A logical value indicating whether to overwrite
#' the original photo file (default is `TRUE`). If `FALSE`, a backup
#' file with the suffix `_original` will be created.
#' @param log_output Optionally produce tibble of log output (default is `TRUE`)
#' @importFrom exifr exiftool_call
#' @importFrom chk chk_string chk_file
#' @importFrom chk chk_string chk_file chk_ext
#' @importFrom tibble tibble
#'
#' @return None. The function modifies the specified file by removing its metadata.
#' @family photos
Expand All @@ -17,22 +19,54 @@
#' \dontrun{
#' rfp_photo_metadata_rm("path/to/photo.jpg")
#'
#' or many at the same time
#' # many at the same time with no logging
#'
#' list_of_photo_paths |> purrr::walk(rfp_photo_metadata_rm)
#' list_of_photo_paths |> purrr::walk(rfp_photo_metadata_rm, log_output = FALSE)
#'
#' # many at the same time with logging
#'
#' log_df <- list_of_photo_paths |> purrr::map_dfr(rfp_photo_metadata_rm)
#' }
rfp_photo_metadata_rm <- function(path_photo, overwrite_original = TRUE) {
rfp_photo_metadata_rm <- function(
path_photo,
overwrite_original = TRUE,
log_output = TRUE) {
# Check that `photo` is a valid string
chk::chk_string(path_photo)
chk::chk_file(path_photo)
chk::chk_ext(path_photo, c("jpeg", "JPEG", "JPG", "jpg", "png", "PNG"))

# Set the arguments for exiftool
# Set up exiftool command and arguments
exiftool_path <- system.file("exiftool/exiftool.pl", package = "exifr")
args <- c("-all=")
if (overwrite_original) {
args <- c(args, "-overwrite_original")
}

# Remove metadata from the photo
exifr::exiftool_call(args = args, fnames = path_photo)
# Execute exiftool command with system2 and capture output
console_output <- system2(
command = "perl",
args = c(exiftool_path, args, path_photo),
stdout = TRUE,
stderr = TRUE
)

# Initialize log tibble if logging is enabled
if (log_output) {
# Parse the output to create a log entry
log_entry <- tibble::tibble(
photo_path = path_photo,
command = paste("perl", exiftool_path, paste(args, collapse = " "), path_photo), # The command used by exiftool
status_message = paste(console_output, collapse = "; ") # Concatenate status lines
)

# Return the log entry as output (could be appended outside the function)
return(log_entry)
} else {
# If logging is disabled, return NULL
# Remove metadata from the photo with regular call to function
exifr::exiftool_call(args = args, fnames = path_photo)
}
}


12 changes: 9 additions & 3 deletions man/rfp_photo_metadata_rm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b2175e

Please sign in to comment.