alpha0010 / react-native-file-access

Filesystem access for React Native
MIT License
298 stars 18 forks source link

cpExternal to 'downloads' works in Android but not in iOS #41

Open afilp opened 2 years ago

afilp commented 2 years ago

Hello,

While it works in iOS, the cpExternal does not work in iOS. Note that I have both UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace enabled. Testing in emulator. Shouldn't the destination ('downloads') be a "common" folder and not an application-specific folder?

Is this a bug or a non-proper implementation?

Thanks a lot!

image

const fetchPayment = async (paymentId, createdDate) => {
  const token = await getAuthToken();
  console.log('token', token);

  const destPath = `${Dirs.CacheDir}/${createdDate}-Invoice.pdf`;

  // const date = new Date();
  const { data } = await new Promise((resolve, reject) => {
    try {
      FileSystem.fetch(
        `${window.our.BASE_URL}/payment?paymentId=${paymentId}`,
        {
          headers: {
            Authorization: `Bearer ${token}`,
            Accept: 'application/pdf',
            'Content-Type': 'application/pdf',
          },
          path: destPath,
        },
      )
        .then(async res => {
          console.log('res', res);
          await FileSystem.cpExternal(
            destPath,
            `${createdDate}-Invoice.pdf`,
            'downloads',
          );
          Alert.alert(
            t('invoiceDownloaded'),
            t('invoiceDownloadedAndroidFolder'),
          );
          resolve(res);
        })
        // Something went wrong:
        .catch((errorMessage, statusCode) => {
          console.log(errorMessage);
          reject(errorMessage);
        });
    } catch (err) {
      console.log(err);
      reject(err);
    }
  });
  return data;
};
alpha0010 commented 2 years ago

File is put where os says it should be. Best as I can tell, that is always sandboxed within the app (might not be true for macos, but I do not use that). Apple procedure for sharing files between apps is by marking the files as sharable (the settings you have enabled). With that, the iOS system file browser should be able to select files from this app for other apps to open.

If you find native api to allow otherwise, let me know.

afilp commented 2 years ago

Thanks @alpha0010, I am OK to leave the file within the app's sandbox. The problem is that cpExternal throws an error while I am using the expected code. As you see from the message, it just tries to copy from "CacheDir" to "downloads" (which is the app's downloads) and it fails. Am I doing something wrong in the code? I believe I am using just the standard code.

I do not see any app specific folder in the Files app, could this be the case?

alpha0010 commented 2 years ago

Oops, sorry, my vision just skipped over the non-English text (or maybe the screenshot did not load for me before?). That is probably a bug.

However, try using cp(), and putting files in Dirs.DocumentDir. I think that should show up in the Files app.