christopherkenny/bskyr

http 400 when publishing image

Opened this issue · 6 comments

`
download.file("https://via.placeholder.com/300", "test_image.jpg")

image_blob <- bs_upload_blob("test_image.jpg")
image_for_post <- list(
blob = image_blob[["ref_$link"]],
alt = "Test image"
)

bs_post(
text = "Test image",
images = list(image_for_post)
)
`

What am I doing wrong? :)

Hi @mw0000. Thanks for using bskyr! There are a few ways to go about this:

  1. If you're passing a blob, it should be a list record (ie set clean = FALSE in bs_upload_blob, like so)
download.file("https://via.placeholder.com/300", "test_image.jpg")

image_blob <- bs_upload_blob("test_image.jpg", clean = FALSE)

bs_post(
  text = "Testing https://github.com/christopherkenny/bskyr/issues/16",
  images = image_blob,
  images_alt = "Test image"
)
  1. You could skip the upload step and let bs_post() handle it:
download.file("https://via.placeholder.com/300", "test_image.jpg")

bs_post(
  text = "Testing https://github.com/christopherkenny/bskyr/issues/16",
  images = "test_image.jpg",
  images_alt = "Test image"
)

This raises a nice point on if passing a clean = TRUE, the default, blob should work. I would think it should, so I'll implement that option for a future 3rd option.

Oh and alt should be specified within bs_post() unless you're manually creating a record. If you were creating the record manually, you would want to use bs_create_record() to upload it then, instead of bs_post().

Okay, and if you reinstall, the following should work now The alt property should still be passed to bs_post() instead though.

download.file("https://via.placeholder.com/300", "test_image.jpg")

image_blob <- bs_upload_blob("test_image.jpg")

bs_post(
  text = "Test image",
  images = image_blob,
  images_alt = 'Test image'
)
download.file("https://via.placeholder.com/300", "test_image.jpg")

image_blob <- bs_upload_blob("test_image.jpg", clean = FALSE)

bs_post(
  text = "Testing https://github.com/christopherkenny/bskyr/issues/16",
  images = image_blob,
  images_alt = "Test image"
)

this works, thank you! But only works, when image size is <= 1MB
https://docs.bsky.app/docs/tutorials/creating-a-post

I've been trying to use base64

library(base64enc)
file_path <- "image.jpg" # 4.9MB
raw_data <- readBin(file_path, "raw", file.info(file_path)$size)

# base64_encoded_image <- base64encode(raw_data)

image_blob <- bs_upload_blob(paste0("data:image/jpeg;base64,", base64_encoded_image), clean = FALSE)

bs_post(
  text = "Test",
  images = image_blob,
  images_alt = "Test image"
)

but got

> image_blob <- bs_upload_blob(paste0("data:image/jpeg;base64,", base64_encoded_image), clean = FALSE)
Error in file(con, "rb") : cannot open the connection
In addition: Warning messages:
1: In basename(path[!is_missing]) :
  expanded path length 6811687 would be too long for

Can you give it a try again, doing nothing fancy?

file_path <- "image.jpg" # 4.9MB
image_blob <- bs_upload_blob(file_path , clean = FALSE)

bs_post(
  text = "Testing https://github.com/christopherkenny/bskyr/issues/16",
  images = image_blob,
  images_alt = "Test image"
)

That should work now, as I've added a base64enc suggests and handle it within the blob upload if it's too big.

Hmm while that uploads, it is still too large. If anything, encoding it as base64 made the blob larger, if I'm reading this correctly.