Download files in Client Mode Ricoh Theta X

I am using React Native and Expo to develop an app to control the camera via Client Mode. So with this Client Mode, every call needs to have digest authentication, otherwise, the call will fail.

When I fetch the data using react-native-digest-fetch, I can get the data from the camera.

const response = await digestFetch(url, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
    username: 'THETAYR10105339',
    password: '10105339',
  })

However, the problem is I cannot save the data into a file. The app crashed when I tried to write the data into the file

if (response.ok) {
  // Fetch the image data
  const imageBlob = await response.blob()

  // Write the image data to the file
  await FileSystem.writeAsStringAsync(fileUri, imageBlob, {
    encoding: FileSystem.EncodingType.Base64,
  })
  const { uri: downloadedUri } = await FileSystem.downloadAsync(
    url,
    fileUri,
  )

  console.log('Image downloaded and saved:', fileUri)
  return filleUri
}

Please help me to solve this problem. Thank you very much.

Is the library returning the response as base64? Or, is the library returning an stream of bytes?

The thumbnail is base64 encoded. However, you’re saving the full-size image, right?

If you use the same library without digest auth, does it work? Meaning, if you connect in Access Point (AP) mode and then save the file, does it work?

Can you write a file from a test image server like this one?

https://picsum.photos/

is there writeAsByteAsync??

  await FileSystem.writeAsStringAsync(fileUri, imageBlob, {

Is there something like writeAsByteAsync?

I don’t normally use JavaScript.

This is an example using Dart.

void saveImage(imageData, String imageName) async {
  var picturesPath = await getPicturesPath();
  var thetaImage = await File(join(picturesPath, 'theta_images', imageName))
      .create(recursive: true);
  await thetaImage.writeAsBytes(imageData.bodyBytes);
}

The thumbnail in camera.listFiles is base64. However, the image is normally a stream of bytes. Though, it depends on your library that you’re using for the HTTP request and response.

1 Like

I can download using Download Async in AP mode.

In Client Mode, I haven’t found a way to write the data into a file. Is there a way to shut down the digest authentication with Ricoh Theta X? Fetching without the digest would be much more easier. Then I can use the Download Async function to get the job done.

I finally managed to fetch with Digest Auth and write the data into a file. Thanks a lot for your help.

The data is actually not base64 so it requires some extra steps using FileReader to parse the data and delete some unnecessary part of the data before can actually write into the file.

You can set security to none. As mentioned, data is not base64.

Is the image file data from the camera different when using Client Mode versus Access Point mode? Are you using react-native-digest-fetch for both the AP and Client Mode HTTP requests?

I thought that both are just a URL endpoint that can download the image using an HTTP GET command. I thought that in both cases, the data is a stream of bytes.

Though, it may depend on how you are making the HTTP request. In my example with dart, there is an option on the response to treat the response as bytes, not as a string. The default for the library I am using is to return the response as a string.

1 Like

Hello this might be a bit late but, here is an example that shows downloading an image as bytes.

It uses the http library and fs library in JavaScript.

1 Like