Skip to content

Commit 33fd50b

Browse files
committed
Fix file upload
1 parent f291ae8 commit 33fd50b

File tree

5 files changed

+75
-45
lines changed

5 files changed

+75
-45
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: seatrackR
22
Title: Interface functions for the Seatrack database
3-
Version: 0.0.3.8
3+
Version: 0.0.3.92
44
Authors@R: c(person(given = "Jens",
55
family = "Åström",
66
email = "jens.astrom@nina.no",

R/getPositions.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' The available choices can be seen in the column `species_name_eng` in the result from the function `getSpecies()`.
1010
#' @param colony Character string. Option to limit selection to one or a set of colonies. Default is NULL. The available
1111
#' choices can be seen in the column `colony_int_name` in the result from the function `getColonies()`.
12+
#' @param age_deployment_class Character string. Option to limit selection to one or a set of age classes. Default is NULL.
1213
#' @param dataResponsible Character string. Option to limit selection to one or a set of names of data responsible persons. Note that this
1314
#' must conform to the name nomenclature used in the postable. Default is NULL. The available
1415
#' choices can be seen in the column `name` in the result from the function `getNames()`.
@@ -53,6 +54,7 @@
5354
getPositions <- function(datatype = "GLS",
5455
species = NULL,
5556
colony = NULL,
57+
age_deployment_class = NULL,
5658
dataResponsible = NULL,
5759
ringnumber = NULL,
5860
year = NULL,
@@ -65,6 +67,7 @@ getPositions <- function(datatype = "GLS",
6567

6668
selectSpecies <- species
6769
selectColony <- colony
70+
selectAge <- age_deployment_class
6871

6972
datatype <- match.arg(datatype,
7073
choices = c("GLS", "IRMA", "GPS")
@@ -91,6 +94,15 @@ getPositions <- function(datatype = "GLS",
9194
res <- res |> filter(colony %in% selectColony)
9295
}
9396

97+
if (!is.null(selectAge)) {
98+
res <- res |>
99+
mutate(
100+
age_deployment_class = ifelse(!is.na(age_deployment) & tolower(age_deployment) %in% c("pullus", "chick", "pull", "juvenile"), "C", "A")
101+
) %>%
102+
filter(age_deployment_class %in% selectAge) %>%
103+
select(-age_deployment_class)
104+
}
105+
94106
if (!is.null(dataResponsible)) {
95107
res <- res |> filter(data_responsible %in% dataResponsible)
96108
}
@@ -111,7 +123,7 @@ getPositions <- function(datatype = "GLS",
111123
res <- res |> filter(individ_id %in% individId)
112124
}
113125

114-
if (!limit == F) {
126+
if (!limit == FALSE) {
115127
res <- res |> head(limit)
116128
}
117129

R/uploadFiles.R

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,57 +32,70 @@ uploadFiles <- function(files = NULL,
3232
current_roles <- current_roles[, 1]
3333

3434

35-
if (!("admin" %in% current_roles || "seatrack_writer" %in% current_roles)) stop("Connected user needs to be part of seatrack_writer or admin group")
36-
37-
if (!tibble::is_tibble(files)) files <- tibble::as_tibble(files)
35+
if (!("admin" %in% current_roles || "seatrack_writer" %in% current_roles)) {
36+
stop("Connected user needs to be part of seatrack_writer or admin group")
37+
}
3838

3939
fileArchive <- listFileArchive()
40-
41-
if (any(files$value %in% fileArchive$filesInArchive$filename) & overwrite == F) {
42-
stop(paste("At least one file already exists in the file archive, use overwrite = True to overwrite"))
40+
if(overwrite == FALSE){
41+
to_upload <- files[!basename(files) %in% fileArchive$filesInArchive$filename]
42+
print(paste(length(files) - length(to_upload) , "files already exist in the file archive and will not be uploaded, use overwrite = TRUE to overwrite"))
4343
} else {
44-
url <- .getFtpUrl()
44+
to_upload <- files
45+
}
46+
print(paste(length(to_upload), "files to upload"))
47+
if(length(to_upload) == 0) {
48+
return(invisible())
49+
}
50+
url <- .getFtpUrl()
4551

46-
writeFile <- function(x,
52+
for(x in to_upload) {
53+
result <- writeFile(x = x, url = url, originFolder = originFolder)
54+
if(result == TRUE) {
55+
print(paste("Successfully uploaded file: ", x))
56+
} else {
57+
print(paste("Failed to upload file: ", x))
58+
}
59+
}
60+
}
61+
62+
63+
writeFile <- function(x,
4764
url,
4865
originFolder = originFolder,
4966
...) {
5067
if (!is.null(originFolder)) {
51-
filename <- paste0(originFolder, "/", x)
68+
filename <- file.path(originFolder, x)
5269
} else {
5370
filename <- paste(x)
5471
}
5572

5673
if (!file.exists(filename)) {
5774
warning(paste("Cannot find file: ", filename))
58-
return(paste0("File not uploaded: ", filename))
59-
} else {
60-
tmp <- strsplit(url$url, "//")
61-
getUrl <- paste0(tmp[[1]][1], "//", url$pwd, "@", tmp[[1]][2], "/", x)
75+
return(FALSE)
76+
}
77+
78+
tmp <- strsplit(url$url, "//")
79+
getUrl <- paste0(tmp[[1]][1], "//", url$pwd, "@", tmp[[1]][2], "/", basename(x))
6280

63-
getHandle <- httr::handle(getUrl)
64-
filePkg <- httr::upload_file(filename)
81+
getHandle <- httr::handle(getUrl)
82+
filePkg <- httr::upload_file(filename)
6583

66-
mess <- lapply(getUrl, factory(function(x) {
67-
RCurl::ftpUpload(
68-
what = filename,
69-
to = getUrl,
70-
asText = FALSE,
71-
use.ssl = TRUE,
72-
ssl.verifypeer = FALSE,
73-
sslversion = 6L,
74-
...
75-
)
76-
}))
84+
mess <- lapply(getUrl, factory(function(x) {
85+
RCurl::ftpUpload(
86+
what = filename,
87+
to = getUrl,
88+
asText = FALSE,
89+
use.ssl = TRUE,
90+
ssl.verifypeer = FALSE,
91+
sslversion = 6L
92+
)
93+
}))
7794

78-
if (any(grepl("OK", attr(mess[[1]][[1]], "names")))) {
79-
return(paste0("File uploaded: ", x))
80-
}
95+
if (any(grepl("OK", attr(mess[[1]][[1]], "names")))) {
96+
return(TRUE)
97+
}else{
98+
return(FALSE)
8199
}
100+
82101
}
83-
84-
apply(files, 1, function(x) writeFile(x = x, url = url, originFolder = originFolder))
85-
86-
## Handle messages like in download, not finished
87-
}
88-
}

R/writeRecordings.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
#' @param activityData A named vector or data frame that fits the activity_raw table in schema recordings
77
#' @param temperatureData A named vector or data frame that fits the temperature_raw table in schema recordings
88
#' @param accelerationData A named vector or data frame that fits the acceleration_raw table in schema recordings
9-
#' @param append Logical, default True. If True, the line(s) is appended to the end of the table.
10-
#' @param overwrite Logical, default False. WARNING!! If True, the function overwrites the current content of the logger_info table.
11-
#'
12-
#' @return Data frame.
9+
1310
#' @export
1411
#' @examples
1512
#' \dontrun{
@@ -20,11 +17,12 @@
2017
writeRecordings <- function(lightData = NULL,
2118
activityData = NULL,
2219
temperatureData = NULL,
23-
accelerationData = NULL,
24-
append = T,
25-
overwrite = FALSE) {
20+
accelerationData = NULL
21+
) {
2622
checkCon()
27-
23+
# Hardcoding these, we don't want to accidentally overwrite data, and we want to make sure that we append to the tables instead of overwriting them
24+
append <- TRUE
25+
overwrite <- FALSE
2826
if (!is.null(lightData)) {
2927
DBI::dbWithTransaction(
3028
con,

scaffolding.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ library(formatR)
33
library(badger)
44
library(styler)
55
library(NinaR)
6+
install_cellar <- function(path = ".") {
7+
cellar <- renv:::renv_paths_cellar()
8+
if (!dir.exists(cellar)) {
9+
dir.create(cellar)
10+
}
11+
pkgbuild::build(path, dest_path = cellar)
12+
}

0 commit comments

Comments
 (0)