#Worker and temp picture for Twitter

1 messages · Page 1 of 1 (latest)

short finch
#

Hello, 'workers' is a fantastic thing, but I have a problem that has been bothering me for days, and I haven’t been able to solve it. I have a function that needs to send a tweet to a certain network. First, I need to send a picture in the standard way and it should return a mediaID. Since the picture is at a URL, I've been taking it into a buffer but the Twitter API 1.1 simply refuses to send it. What’s even worse, when I try the function on a system where I have the possibility to have the picture on the filesystem, it works without a problem. In desperation, I also sent it to R2 and tried from there, but without success. Does anyone have any solution to at least transfer it to some temporary place on the filesystem before sending??? Thanks

livid hamlet
short finch
#

The last thing I did was this, however, there were hundreds of ideas and versions before that... and error
(log) Starting media upload to Twitter...
(log) Preparing FormData...
(log) Making fetch call for media upload...
(log) Fetch call completed.
(log) HTTP Status: 400
(log) Received data: {"request":"/1.1/media/upload.json","error":"media type unrecognized."}
(error) Failed to upload media to Twitter

livid hamlet
# short finch The last thing I did was this, however, there were hundreds of ideas and version...

Is there a reason why you're putting it into r2, and taking it out?
the mdn is a good source for JS Docs:
https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
form.append's third argument is just the file name, not an object or anything. It also only accepts a string or Blob (or any subclass of blob, like File), anything else is stringified
Unless you want to keep the r2 logic as a backup, you could get rid of all that and just pass the arrayBuffer through. You can create a Blob using it:
https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob

   let imageBlob = new Blob([imageBuffer], {
      type: "image/jpeg",
    });

and then we can use that blob to append to the form data

    form.append("media", imageBlob, "image.jpg");

Should hopefully clear up the media type unrecognized error, since you're properly giving it a type

short finch
#

My idea was to leave the picture in R2 as a backup, before this code I have a part of the code that writes the URL into my D1 database and then after that, we go with this and set images in R2. I changed the function call a bit but still without success. I think I will give up on the Worker and switch to Vercel but I really wanted to keep the server on Cloudflare because I really like the service 😦