@@ -42,9 +42,11 @@ content_type_for <- function(path) {
4242 switch (ext ,
4343 pdf = " application/pdf" ,
4444 png = " image/png" ,
45- jpg = " image/jpeg" , jpeg = " image/jpeg" ,
45+ jpg = " image/jpeg" ,
46+ jpeg = " image/jpeg" ,
4647 gif = " image/gif" ,
47- tif = " image/tiff" , tiff = " image/tiff" ,
48+ tif = " image/tiff" ,
49+ tiff = " image/tiff" ,
4850 bmp = " image/bmp" ,
4951 webp = " image/webp" ,
5052 doc = " application/msword" ,
@@ -55,77 +57,80 @@ content_type_for <- function(path) {
5557 xlsx = " application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
5658 txt = " text/plain" ,
5759 rtf = " application/rtf" ,
58- html = " text/html" , htm = " text/html" ,
60+ html = " text/html" ,
61+ htm = " text/html" ,
5962 " application/octet-stream"
6063 )
6164}
6265
63- tryCatch({
64- collected_ids <- character (0 )
66+ tryCatch(
67+ {
68+ collected_ids <- character (0 )
6569
66- for (i in seq_along(args )) {
67- p <- args [[i ]]
68- ext <- tolower(tools :: file_ext(p ))
69- if (ext == " pdf" ) {
70- # Upload PDF to get id
71- upload_url <- paste0(api_base , " /upload" )
72- upload_resp <- httr :: POST(
73- upload_url ,
74- httr :: add_headers(
75- " api-key" = api_key ,
76- " content-filename" = basename(p ),
77- " Content-Type" = " application/octet-stream"
78- ),
79- body = readBin(p , what = " raw" , n = file.info(p )$ size )
80- )
81- txt <- httr :: content(upload_resp , as = " text" , encoding = " UTF-8" )
82- message(txt )
83- if (httr :: http_error(upload_resp )) {
84- stop(sprintf(" Upload failed for input #%d with status %s" , i , httr :: status_code(upload_resp )))
70+ for (i in seq_along(args )) {
71+ p <- args [[i ]]
72+ ext <- tolower(tools :: file_ext(p ))
73+ if (ext == " pdf" ) {
74+ # Upload PDF to get id
75+ upload_url <- paste0(api_base , " /upload" )
76+ upload_resp <- httr :: POST(
77+ upload_url ,
78+ httr :: add_headers(
79+ " api-key" = api_key ,
80+ " content-filename" = basename(p ),
81+ " Content-Type" = " application/octet-stream"
82+ ),
83+ body = readBin(p , what = " raw" , n = file.info(p )$ size )
84+ )
85+ txt <- httr :: content(upload_resp , as = " text" , encoding = " UTF-8" )
86+ message(txt )
87+ if (httr :: http_error(upload_resp )) {
88+ stop(sprintf(" Upload failed for input #%d with status %s" , i , httr :: status_code(upload_resp )))
89+ }
90+ up_json <- jsonlite :: fromJSON(txt )
91+ collected_ids <- c(collected_ids , if (is.data.frame(up_json $ files )) up_json $ files $ id [[1 ]] else up_json $ files [[1 ]]$ id )
92+ message(sprintf(" Uploaded PDF (#%d); id=%s" , i , tail(collected_ids , 1 )))
93+ } else {
94+ # Convert to PDF via /pdf to get outputId
95+ conv_url <- paste0(api_base , " /pdf" )
96+ body <- list (file = httr :: upload_file(p , type = content_type_for(p )))
97+ conv_resp <- httr :: POST(conv_url , httr :: add_headers(" api-key" = api_key ), body = body , encode = " multipart" )
98+ txt <- httr :: content(conv_resp , as = " text" , encoding = " UTF-8" )
99+ message(txt )
100+ if (httr :: http_error(conv_resp )) {
101+ stop(sprintf(" Conversion failed for input #%d with status %s" , i , httr :: status_code(conv_resp )))
102+ }
103+ cv_json <- jsonlite :: fromJSON(txt )
104+ collected_ids <- c(collected_ids , cv_json $ outputId )
105+ message(sprintf(" Converted non-PDF (#%d); outputId=%s" , i , tail(collected_ids , 1 )))
85106 }
86- up_json <- jsonlite :: fromJSON(txt )
87- collected_ids <- c(collected_ids , if (is.data.frame(up_json $ files )) up_json $ files $ id [[1 ]] else up_json $ files [[1 ]]$ id )
88- message(sprintf(" Uploaded PDF (#%d); id=%s" , i , tail(collected_ids , 1 )))
89- } else {
90- # Convert to PDF via /pdf to get outputId
91- conv_url <- paste0(api_base , " /pdf" )
92- body <- list (file = httr :: upload_file(p , type = content_type_for(p )))
93- conv_resp <- httr :: POST(conv_url , httr :: add_headers(" api-key" = api_key ), body = body , encode = " multipart" )
94- txt <- httr :: content(conv_resp , as = " text" , encoding = " UTF-8" )
95- message(txt )
96- if (httr :: http_error(conv_resp )) {
97- stop(sprintf(" Conversion failed for input #%d with status %s" , i , httr :: status_code(conv_resp )))
98- }
99- cv_json <- jsonlite :: fromJSON(txt )
100- collected_ids <- c(collected_ids , cv_json $ outputId )
101- message(sprintf(" Converted non-PDF (#%d); outputId=%s" , i , tail(collected_ids , 1 )))
102107 }
103- }
104108
105- # Build x-www-form-urlencoded merge body
106- enc <- function (x ) utils :: URLencode(x , reserved = TRUE )
107- parts <- character (0 )
108- for (id in collected_ids ) {
109- parts <- c(parts , paste0(" id[]=" , enc(id )))
110- parts <- c(parts , paste0(" pages[]=" , enc(" 1-last" )))
111- parts <- c(parts , paste0(" type[]=" , enc(" id" )))
112- }
113- merge_body <- paste(parts , collapse = " &" )
109+ # Build x-www-form-urlencoded merge body
110+ enc <- function (x ) utils :: URLencode(x , reserved = TRUE )
111+ parts <- character (0 )
112+ for (id in collected_ids ) {
113+ parts <- c(parts , paste0(" id[]=" , enc(id )))
114+ parts <- c(parts , paste0(" pages[]=" , enc(" 1-last" )))
115+ parts <- c(parts , paste0(" type[]=" , enc(" id" )))
116+ }
117+ merge_body <- paste(parts , collapse = " &" )
114118
115- merge_url <- paste0(api_base , " /merged-pdf" )
116- merge_resp <- httr :: POST(
117- merge_url ,
118- httr :: add_headers(" api-key" = api_key , " Content-Type" = " application/x-www-form-urlencoded" ),
119- body = merge_body
120- )
119+ merge_url <- paste0(api_base , " /merged-pdf" )
120+ merge_resp <- httr :: POST(
121+ merge_url ,
122+ httr :: add_headers(" api-key" = api_key , " Content-Type" = " application/x-www-form-urlencoded" ),
123+ body = merge_body
124+ )
121125
122- merge_text <- httr :: content(merge_resp , as = " text" , encoding = " UTF-8" )
123- cat(merge_text )
124- if (httr :: http_error(merge_resp )) {
125- stop(sprintf(" Merge failed with status %s" , httr :: status_code(merge_resp )))
126+ merge_text <- httr :: content(merge_resp , as = " text" , encoding = " UTF-8" )
127+ cat(merge_text )
128+ if (httr :: http_error(merge_resp )) {
129+ stop(sprintf(" Merge failed with status %s" , httr :: status_code(merge_resp )))
130+ }
131+ },
132+ error = function (e ) {
133+ stderrf(" Error: %s: %s\n " , class(e )[1 ], conditionMessage(e ))
134+ quit(status = 1 )
126135 }
127-
128- }, error = function (e ) {
129- stderrf(" Error: %s: %s\n " , class(e )[1 ], conditionMessage(e ))
130- quit(status = 1 )
131- })
136+ )
0 commit comments