@@ -36,58 +36,74 @@ osf_mkdir <- function(x, path, verbose = FALSE) {
36
36
37
37
# ' @export
38
38
osf_mkdir.osf_tbl_node <- function (x , path , verbose = FALSE ) {
39
-
40
39
x <- make_single(x )
41
- id <- as_id(x )
40
+ recurse_path(x , path , missing_action = " create" , verbose )
41
+ }
42
42
43
- # does path root already exist?
44
- path_root <- fs :: path_split(path )[[1 ]][1 ]
45
- items <- osf_ls_files(x , type = " folder" , pattern = path_root )
46
- dir_root <- items [which(items $ name == path_root ), ]
43
+ # ' @export
44
+ osf_mkdir.osf_tbl_file <- function (x , path , verbose = FALSE ) {
45
+ if (is_osf_file(x )) abort(" Can't create directories within a file." )
46
+ x <- make_single(x )
47
+ recurse_path(x , path , missing_action = " create" , verbose )
48
+ }
47
49
48
- if (nrow(dir_root ) == 0 ) {
49
- res <- .wb_create_folder(id = id , name = path_root )
50
- raise_error(res )
51
- dir_id <- gsub(" /" , " " , res $ data $ attributes $ path , fixed = TRUE )
52
- dir_root <- osf_retrieve_file(dir_id )
53
- msg <- sprintf(" Created directory '%s/' in node %s" , path_root , id )
54
- } else {
55
- msg <- sprintf(" Directory '%s/' already exists in node %s" , path_root , id )
56
- }
57
50
58
- if (verbose ) message(msg )
51
+ # ' Create a single folder on OSF
52
+ # '
53
+ # ' This wraps the create folder endpoint on Waterbutler but retrieves the newly
54
+ # ' created directory from OSF because Waterbutler returns only a subset of the
55
+ # ' information provided by OSF.
56
+ # '
57
+ # ' @param id GUID for an OSF project or component
58
+ # ' @param name Name of the new directory (note: this must be the name of a
59
+ # ' single directory, not a path)
60
+ # ' @param fid Optional, provide a Waterbutler folder ID to create the new folder
61
+ # ' within the specified existing folder.
62
+ # ' @noRd
59
63
60
- # recurse to the next-level if there is a subfolder
61
- path_next <- fs :: path_rel(path , path_root )
62
- if (path_next == " ." ) {
63
- out <- dir_root
64
- } else {
65
- out <- osf_mkdir(dir_root , path_next , verbose )
66
- }
67
- out
64
+ .osf_mkdir <- function (id , name , fid = NULL ) {
65
+ res <- .wb_create_folder(id , name , fid )
66
+ raise_error(res )
67
+ dir_id <- gsub(" /" , " " , res $ data $ attributes $ path , fixed = TRUE )
68
+ osf_retrieve_file(dir_id )
68
69
}
69
70
70
- # ' @export
71
- osf_mkdir.osf_tbl_file <- function (x , path , verbose = FALSE ) {
72
- x <- make_single(x )
73
- id <- as_id(x )
74
- if (is_osf_file(x )) abort(" Can't create directories within a file." )
75
71
76
- # does path root already exist?
72
+ # ' Recurse a directory path
73
+ # '
74
+ # ' Given a path like 'root/subdir1/subdir2', this will retrieve each directory
75
+ # ' level from OSF and return the leaf directory. The `missing_action` argument
76
+ # ' determines what happens if an intermediate directory does not exist.
77
+ # '
78
+ # ' @param x An `osf_tbl_node` or an `osf_tbl_file` with a directory.
79
+ # ' @param path A path of directories.
80
+ # ' @param missing_action Either `"error"` or `"create"` to create the missing
81
+ # ' directory.
82
+ # ' @noRd
83
+ recurse_path <- function (x , path , missing_action = " error" , verbose = FALSE ) {
84
+ missing_action <- match.arg(missing_action , c(" error" , " create" ))
85
+
77
86
path_root <- fs :: path_split(path )[[1 ]][1 ]
78
- items <- osf_ls_files(x , type = " folder" , pattern = path_root )
79
- dir_root <- items [which(items $ name == path_root ), ]
87
+ root_dir <- osf_ls_files(x , type = " folder" , pattern = path_root )
80
88
81
- if (nrow(dir_root ) == 0 ) {
82
- res <- .wb_create_folder(id = get_parent_id(x ), name = path_root , fid = id )
83
- raise_error(res )
89
+ if (nrow(root_dir ) == 0 ) {
90
+ if (missing_action == " error" ) {
91
+ abort(sprintf(" Can't find directory '%s' in `%s`" , path_root , x $ name ))
92
+ }
84
93
85
- dir_id <- gsub(" /" , " " , res $ data $ attributes $ path , fixed = TRUE )
86
- dir_root <- osf_retrieve_file(dir_id )
87
- msg <- sprintf(" Created sub-directory '%s/' in directory '%s/'" ,
94
+ # create the missing directory
95
+ if (inherits(x , " osf_tbl_node" )) {
96
+ root_dir <- .osf_mkdir(as_id(x ), name = path_root )
97
+ msg <- sprintf(" Created directory '%s/' in node %s" ,
98
+ path_root , as_id(x ))
99
+ } else {
100
+ root_dir <- .osf_mkdir(get_parent_id(x ), name = path_root , fid = as_id(x ))
101
+ msg <- sprintf(" Created sub-directory '%s/' in directory '%s/'" ,
88
102
path_root , x $ name )
103
+ }
104
+
89
105
} else {
90
- msg <- sprintf(" Sub -directory '%s/' already exists in directory '%s/ '" ,
106
+ msg <- sprintf(" Navigating to sub -directory '%s/' in '%s'" ,
91
107
path_root , x $ name )
92
108
}
93
109
@@ -96,9 +112,9 @@ osf_mkdir.osf_tbl_file <- function(x, path, verbose = FALSE) {
96
112
# recurse to the next-level if there is a subfolder
97
113
path_next <- fs :: path_rel(path , path_root )
98
114
if (path_next == " ." ) {
99
- out <- dir_root
115
+ out <- root_dir
100
116
} else {
101
- out <- osf_mkdir( dir_root , path_next , verbose )
117
+ out <- Recall( root_dir , path_next , missing_action , verbose )
102
118
}
103
119
out
104
120
}
0 commit comments