contentful-management.js icon indicating copy to clipboard operation
contentful-management.js copied to clipboard

createAssetFromFiles in React Native

Open harrywrok opened this issue 3 years ago • 4 comments
trafficstars

I m trying to create asset but can't see the image i uploaded(Image is broken). For "file" value, i used RNFetchBlob to "wrap" or "readStream".

Code

const stream = await RNFetchBlob.fs.readStream(uri.replace('file://', ''), 'base64')
const selectedImg = await RNFetchBlob.wrap(uri.replace('file://', ''))

const image = {
    fields: {
      title: {
        'en-US': testFile
      },
      file: {
        'en-GB': {
          contentType: 'image/jpeg',
          fileName: testFileName,
          file: selectedImg, // or stream
        }
      }
    }
  }

contentful.createAssetFromFiles(image)

SDK Response

{
   "fields":{
      "file":{
         "en-US":["Object" ]
      },
      "title":{
         "en-US":"testFile"
      }
   },
   "metadata":{
      "tags":[  ]
   },
   "sys":{
      "createdAt":"2021-12-22T20:49:41.405Z",
      "createdBy":{
         "sys":[ "Object" ]
      },
      "environment":{
         "sys":[ "Object" ]
      },
      "id":"6jQR6q4SrOFbiukeeeee4R",
      "publishedCounter":0,
      "space":{
         "sys":[ "Object" ]
      },
      "type":"Asset",
      "updatedAt":"2021-12-22T20:49:41.405Z",
      "updatedBy":{
         "sys":["Object" ]
      },
      "version":1
   }
}

There is no error in SDK's response but i cant see the image on Contentful Screen Shot 2021-12-22 at 6 14 08 PM

Test Device

  • Ios -> Simulator(14.5)
  • Android -> real device(Android 10)

Can you help me what is wrong here?

harrywrok avatar Dec 23 '21 00:12 harrywrok

@harrywrok I'm not 100% sure, but you probably need to call contentful.processForAllLocales -- as in, you need to process the upload data, as seen in this test: https://github.com/contentful/contentful-management.js/blob/22f1e5ecfa98146426a6e9c43ab7adf4eccab78a/test/integration/asset-integration.js#L126-L147

luizfonseca avatar Jan 03 '22 09:01 luizfonseca

@luizfonseca after calling await asset.processForAllLocales({ processingCheckWait: 5000 }) processedAsset.fields.file['en-US'] -> {"contentType": "image/jpeg", "details": {"size": 129}, "fileName": "img.jpg", "url": "//images.ctfassets.net/vi16v6goahak/63vC7xvOxVlmNvgg6JzurN/a0be483480e50d27631b6fc49e05ed3e/img.jpg"}

There is no error in response and i cant see images again.

Screen Shot 2022-01-03 at 11 48 21 AM Screen Shot 2022-01-03 at 11 48 33 AM

harrywrok avatar Jan 03 '22 17:01 harrywrok

@harrywrok Not sure if you've solved this, but can you double check if the blob/stream is being passed correctly? If you access that image, you will find in your inspector it's just a partial encoded string instead.

I'm not familiar with the library you are using, but I'd imagine that you need to consumer the stream with .onData: https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#readstreampath-encoding-buffersize-interval-promisernfbreadstream

A file stream will NOT open automatically, invoke this method to start read chunks from the file. https://github.com/joltup/rn-fetch-blob/wiki/Classes#open

luizfonseca avatar Jan 06 '22 15:01 luizfonseca

@harrywrok otherwise, maybe readFile is what you want https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#readfilepath-encodingpromise

luizfonseca avatar Jan 06 '22 15:01 luizfonseca

Closing for inactivity

Spring3 avatar Oct 12 '22 19:10 Spring3