@@ -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- }
0 commit comments