Piroro-hs / react-native-twitter

A Twitter API client library for React Native. Currently not maintained as my main motivation of creating this library was to use the User Streams API.
MIT License
39 stars 20 forks source link

Please help with uploading images not in Camera Roll #3

Open pakicoder123 opened 7 years ago

pakicoder123 commented 7 years ago

Hi, firstly I would like to say I really like this library and appreciate how well made it is.

The issue I'm having is that I don't know how to post a status with an image without using the camera roll. Is there anyway to get an image from a url on the internet and post that image to a twitter status along with some text? Please point me in the right direction, I couldn't find anything in your documentation.

Thank you so much for your time

Piroro-hs commented 7 years ago

React Native currently lacks Blob implementation, so getting an image from a url on the internet and posting that to a twitter is a little bit complicate work.

First, we need a file downloading library such as react-native-fetch-blob, react-native-fs, etc to download an image and get the uri of it. Below sample uses react-native-fetch-blob.

let imageUri = '';

RNFetchBlob
  .config({
    fileCache: true,
  })
  .fetch('GET', 'image url')
  .then((resp) => {
    const imagePath = resp.path();
    imageUri = `file://${imagePath}`;
  });

Then we can use this imageUri to create Object with uri property and pass it to rest.post().

rest.post('media/upload', {media: {uri: imageUri}})
  .then(({media_id_string}) => rest.post('statuses/update', {status: 'test', media_ids: media_id_string}));

When React Native gets support of Blob, I'll add some helper methods for media uploading. (https://github.com/facebook/react-native/pull/11417, https://github.com/facebook/react-native/pull/11573)

pakicoder123 commented 7 years ago

Thank you so much! I've been working countless hours on this, day and night, I appreciate the reply very much. Will the code you wrote above work as is or would I need to make any changes to it? Please let me know and again I would like to say that I appreciate your help and the wonderful library you've made.