()
| 137 | }; |
| 138 | |
| 139 | export const showFilePickerAndReadFile = async function (): Promise<{ data: string | false; uri: string | false }> { |
| 140 | try { |
| 141 | const [pickedFile] = await pick({ |
| 142 | type: [types.allFiles], |
| 143 | }); |
| 144 | |
| 145 | const [localCopy] = await keepLocalCopy({ |
| 146 | files: [ |
| 147 | { |
| 148 | uri: pickedFile.uri, |
| 149 | fileName: pickedFile.name ?? 'unnamed', |
| 150 | }, |
| 151 | ], |
| 152 | destination: 'cachesDirectory', |
| 153 | }); |
| 154 | |
| 155 | if (localCopy.status !== 'success') { |
| 156 | // to make ts happy, should not need this check here |
| 157 | presentAlert({ message: 'Picking and caching a file failed: ' + localCopy.copyError }); |
| 158 | return { data: false, uri: false }; |
| 159 | } |
| 160 | |
| 161 | const fileCopyUri = decodeURI(localCopy.localUri); |
| 162 | |
| 163 | if (localCopy.localUri.toLowerCase().endsWith('.psbt')) { |
| 164 | // this is either binary file from ElectrumDesktop OR string file with base64 string in there |
| 165 | const file = await _readPsbtFileIntoBase64(fileCopyUri); |
| 166 | return { data: file, uri: fileCopyUri }; |
| 167 | } |
| 168 | |
| 169 | if (localCopy.localUri.endsWith('.png') || localCopy.localUri.endsWith('.jpg') || localCopy.localUri.endsWith('.jpeg')) { |
| 170 | return await handleImageFile(fileCopyUri); |
| 171 | } |
| 172 | |
| 173 | const file = await RNFS.readFile(fileCopyUri); |
| 174 | return { data: file, uri: fileCopyUri }; |
| 175 | } catch (err: any) { |
| 176 | if (!isCancel(err)) { |
| 177 | presentAlert({ message: err.message }); |
| 178 | } |
| 179 | return { data: false, uri: false }; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | const readFileAsBase64 = async (uri: string): Promise<string> => { |
| 184 | try { |
no test coverage detected