yihui / xfun

Miscellaneous R functions
https://yihui.org/xfun/
Other
135 stars 28 forks source link

Move `knitr::imgur_upload()` to this package #83

Closed yihui closed 7 months ago

yihui commented 7 months ago

so that packages using imgur_upload() don't have to depend on knitr.

The dependency on the R package curl could also be removed if the command curl exists.

BTW, imgur.com is blocked in certain countries. I'm not sure if there are other similar image hosting services. The one below doesn't really work:

https://freeimage.host/page/api

upload_freeimage = function(file, curl = Sys.which('curl')) {
  api = 'https://freeimage.host/api/1/upload/'
  key = '6d207e02198a847aa98d0a2a901485a5'
  # use the curl package if the curl command is not found
  if (curl == '') {
    h = curl::new_handle()
    curl::handle_setopt(h, customrequest = 'POST')
    curl::handle_setform(h, key = key, source = curl::form_file(file))
    res = rawToChar(curl::curl_fetch_memory(api, h)$content)
    return(res)
  }
  system2(c('curl', shQuote(c(
    '--form', paste0('key=', key), '--form', paste0('source=@', file), api
  ))))
}