-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
126 lines (97 loc) · 4.01 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
library(shiny)
source('./infusionsoft.R')
# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {
#########################################################################################
#Table Getter
tableData <- reactive({
getTable(input$appname,input$apikey,input$tableSelect)
})
output$tableContent <- renderTable({
tableData()
})
output$tableDownload <- downloadHandler(
filename = function() {paste(input$tableSelect,".csv",sep="")},
content = function(file) {
write.csv(tableData(),file,row.names=F,na="")
}
)
#########################################################################################
#File Attachments
#TODO:
#Gate files uploaded by filesize
#Gate files uploaded by file extension
library(base64enc)
library(plyr)
#DIRECTORY
#opens the connection to the file directory picker
shinyDirChoose(input, 'dir', roots = c(home = '~'), filetypes = c(''))
#store the chosen directory
dir <- reactive(input$dir)
#displays directory on UI
output$dir <- renderPrint(dir())
#PATH
#returns the path breakdown of the chosen directory
path <- reactive({
home <- normalizePath("~")
file.path(home, paste(unlist(dir()$path[-1]), collapse = .Platform$file.sep))
})
#FILE UPLOAD
#displays all files in the chosen directory
output$files <- renderPrint(list.files(path()))
#File upload handler
fileUpload <- eventReactive(input$upload,{
#reads uploaded attachment csv
attachments <- read.csv(input$fileAttachmentsFile$datapath,header=T)
#add error reason column for exporting
attachments["errorReason"] <-
#create empty data frame to store skipped attachments
skippedattachments <- data.frame()
#foreach row in attachments file
for(x in 1:nrow(attachments)) {
#valid file checker (confirms if entire row has data, and that file exists)
containsNA <- anyNA(attachments[x,])
if (containsNA == T) {
attachments[x,]$errorReason <- "Insufficient Data"
skippedattachments <- rbind.fill(skippedattachments,attachments[x,])
next
}
validFile <- file.exists(paste(parseDirPath(c(home = '~'),dir()),attachments[x,]$filepath,sep="/"))
if (validFile == F) {
attachments[x,]$errorReason <- "File not found"
skippedattachments <- rbind.fill(skippedattachments,attachments[x,])
next
}
#check for the current rows file size
filesizecheck <- file.info(paste(parseDirPath(c(home = '~'),dir()),attachments[x,]$filepath,sep="/"))$size < 10 * 1024 * 1024
if (filesizecheck == F) {
attachments[x,]$errorReason <- "File size limit exceeded"
skippedattachments <- rbind.fill(skippedattachments,attachments[x,])
next
}
#check that the files extension is in the list of supported file types
extensioncheck <- attachments[x,]$extension %in% supportedFileTypes
if (extensioncheck == F) {
attachments[x,]$errorReason <- "Unsupported file type"
skippedattachments <- rbind.fill(skippedattachments,attachments[x,])
next
}
#API call
#https://developer.infusionsoft.com/docs/xml-rpc/#file-upload-a-file
xml.rpc(paste("https://",input$appnameAttachment,".infusionsoft.com/api/xmlrpc",sep=""),
"FileService.uploadFile",
input$apikeyAttachment,
attachments[x,]$id,
as.character(attachments[x,]$filename),
base64encode(paste(parseDirPath(c(home = '~'),dir()),attachments[x,]$filepath,sep="/"))
)
}
write.csv(skippedattachments,paste(parseDirPath(c(home = '~'),dir()),"Skipped Attachments.csv",sep="/"),row.names = F)
})
output$status <- renderPrint({
fileUpload()
})
session$onSessionEnded(function() {
stopApp()
})
})