Redeploying to same content (replacing uploaded content)

Hello,

I have a shiny app where people in my department can upload their documents on Posit connect. However, I've had the problem where some people upload the same document more than once. I'm am therefore trying to overcome this by updating the app to first check if a document with that name already exists on Connent, if the document exists the user gets a pop-up message alerting then that a document with the same title already exists and how they would like to proceed. The user is then given an option to replace the file already uploaded with a new one (eg: overwrite the existing document).
The first part of the code works perfectly however the problem is when the replace button is clicked on.

When I (the person who created the api key) click the replace button I get it to work, but when someone else tries it, it does not work and throws an error, error while replacing.

Here is my code:

observeEvent(input$upload, {
tryCatch({

if (!is.null(input$file)) {

file <- file.copy(from = input$file$datapath,
to = paste0(tempdir(),input$pubname,
" - ",
input$pubmonth,
" ",
input$pubyear,
".pdf"))

client <- connect()

# Define file name
file_name <- paste0(input$pubname, " - ", input$pubmonth, " ", input$pubyear)

list_files <- connectapi::get_content(client)

test <- function(client, file_name) {
if (file_name %in% list_files$title) {
showModal(modalDialog(
title = "Duplicate File Found",
paste("A file with the name", file_name, "already exists on ESD Connect. How would you like to proceed?"),
easyClose = TRUE,
footer = tagList(
modalButton("Cancel"),
actionButton("replace_file", "Replace File")
)
))
return(FALSE)
} else {
return(TRUE)
}
}

if (test(client, file_name)) {

bnd <- bundle_static(path = input$file$datapath)

content_1 <- deploy(
client,
bnd,
name = connectapi:::create_random_name(length = 50),
title = file_name,
access_type = "all"
)

all_users <- get_users(client, page_size = 25, prefix = NULL, limit = 350)

# session_user <- "p526851"

one_user <- all_users %>% filter(username == toupper(session$user))

one_user_guid <- one_user %>% pull(guid)

# content_1 %>%
# content_update_owner(
# owner_guid = one_user_guid
# )
#

content_1 %>%
content_add_user(
one_user_guid
)

myapp <- set_content_tag_tree(content_1, "Flash Reports", input$unitname, input$pubname, input$pubyear)

content_1$update(owner_guid = one_user_guid)

urls <- content_1$get_url()

showModal(modalDialog(
title = "Flash Report Uploaded",
HTML(paste0(
"Dear Flash Report Author,",
"<br><br>",
"Direct link to report: ",
"<a href='", urls, "'>Click here</a>",
"<br><br>",
"To access historical reports as well as other data products, please visit ",
"<a href='[http://srv'>ESDConnect</a](http://srv'%3Eesdconnect%3C/a)>. ",
"This will require entering your P number and system password.",
"<br><br>",
"Sincerely,",
"<br><br>",
"ESD Data Analytics"
))
))
}

} else {
showModal(modalDialog(
title = "Error",
"No file selected for upload."
))
}

}, error = function(e) {
showModal(modalDialog(
title = "Error",
paste0("An error occurred: ", e$message)
))
})
})

observeEvent(input$replace_file, {
tryCatch({

if (!is.null(input$file)) {

file <- file.copy(from = input$file$datapath,
to = paste0(tempdir(),input$pubname,
" - ",
input$pubmonth,
" ",
input$pubyear,
".pdf"))

client <- connect()

file_name <- paste0(input$pubname, " - ", input$pubmonth, " ", input$pubyear)

list_files <- connectapi::get_content(client)

content_name <- list_files$name[list_files$title == file_name]

content_guids <- list_files$guid[list_files$title == file_name]

content_guid <- content_guids[1]

if (length(content_guid) == 1) {

bnd <- bundle_static(path = input$file$datapath)

content_1 <- deploy(
client,
bnd,
guid = content_guid,
name = content_name,
title = file_name,
access_type = "all"
)

all_users <- get_users(client, page_size = 25, prefix = NULL, limit = 350)

# session_user <- "p526851"

one_user <- all_users %>% filter(username == toupper(session$user))

one_user_guid <- one_user %>% pull(guid)

# content_1 %>%
# content_update_owner(
# owner_guid = one_user_guid
# )

content_1 %>%
content_add_user(
one_user_guid, role = "owner"
)

myapp <- set_content_tag_tree(content_1, "Flash Reports", input$unitname, input$pubname, input$pubyear)

content_1$update(owner_guid = one_user_guid)

urls <- content_1$get_url()

showModal(modalDialog(
title = "Flash Report Uploaded",
HTML(paste0(
"Dear Flash Report Author,",
"<br><br>",
"Direct link to report: ",
"<a href='", urls, "'>Click here</a>",
"<br><br>",
"To access historical reports as well as other data products, please visit ",
"<a href='[http://srv'>ESDConnect</a](http://srv'%3Eesdconnect%3C/a)>. ",
"This will require entering your P number and system password.",
"<br><br>",
"Sincerely,",
"<br><br>",
"ESD Data Analytics"
))
))

removeModal()

} else {
showModal(modalDialog(
title = "Error",
"File could not be replaced. Please try again."
))
}

}
}, error = function(e) {
showModal(modalDialog(
title = "Error",
paste0("An error occurred while replacing: ", e$message)
))
})
})

}

Would appreciate any assistance

It's a bit difficult to follow your example outside the context of the application it's running in--a good reproducible example is also minimal: https://reprex.tidyverse.org/articles/reprex-dos-and-donts.html#main-requirements

If you're able to reproduce the problem in a self-contained shiny app that will make it a bit easier to follow what's going on.

That said,

When I (the person who created the api key) click the replace button I get it to work, but when someone else tries it, it does not work and throws an error, error while replacing.

Versions of Connect >= 2025.01 support Visitor API Keys, which are intended to enable content to execute with the viewer's credentials rather than as the application author, which may be helpful in this case.