@@ -9,6 +9,7 @@ import { useWindowDimensions } from '../util'
9
9
import LoadingOutlined from '@ant-design/icons/LoadingOutlined'
10
10
import UploadOutlined from '@ant-design/icons/UploadOutlined'
11
11
import jsQR from 'jsqr'
12
+ import { getDataURLFromFile , getTextFromFile } from './Common'
12
13
13
14
const QrCodeScanner = ( { onScan, shouldInit, style } ) => {
14
15
const ref = useRef ( )
@@ -78,37 +79,45 @@ const QrCodeScanner = ({ onScan, shouldInit, style }) => {
78
79
image . src = uri
79
80
} )
80
81
81
- const getBase64 = ( img ) => new Promise ( ( resolve ) => {
82
- const reader = new FileReader ( )
83
- reader . addEventListener ( 'load' , ( ) => resolve ( reader . result ) )
84
- reader . readAsDataURL ( img )
85
- } )
86
-
87
82
const onQrcodeChange = async ( info ) => {
88
83
if ( info . file . status === 'uploading' ) {
89
84
setQrCodeImageUploading ( true )
90
85
}
91
86
92
87
if ( info . file . status === 'done' ) {
93
- const imageUri = await getBase64 ( info . file . originFileObj )
94
- const imageData = await convertURIToImageData ( imageUri )
95
- const qrCode = jsQR ( imageData . data , imageData . width , imageData . height )
96
- if ( ! qrCode ) {
97
- message . error ( 'Fail to read the uploaded image.' , 15 )
88
+ try {
89
+ if ( info . file ?. name ?. endsWith ( '.json' ) ) {
90
+ const jsonData = await getTextFromFile ( info . file . originFileObj )
91
+ message . debug ( jsonData )
92
+ const parsed = JSON . parse ( jsonData )
93
+ onScan ( parsed , true )
94
+ return
95
+ }
96
+ const imageUri = await getDataURLFromFile ( info . file . originFileObj )
97
+ const imageData = await convertURIToImageData ( imageUri )
98
+ const qrCode = jsQR ( imageData . data , imageData . width , imageData . height )
99
+ if ( ! qrCode ) {
100
+ message . error ( 'Fail to read the uploaded image.' , 15 )
101
+ return
102
+ }
103
+ onScan ( qrCode . data )
104
+ } catch ( ex ) {
105
+ console . error ( ex )
106
+ message . error ( 'An error occurred while parsing the QR Code image. Please contact 1wallet developer' )
107
+ } finally {
98
108
setQrCodeImageUploading ( false )
99
- return
100
109
}
101
- onScan ( qrCode . data )
102
- setQrCodeImageUploading ( false )
103
110
}
104
111
}
105
112
106
113
const beforeUpload = ( file ) => {
107
114
const isJpgOrPng = file . type === 'image/jpeg' || file . type === 'image/png'
108
- if ( ! isJpgOrPng ) {
109
- message . error ( 'You can only upload JPG/PNG file' )
115
+ const isJson = file . type === 'application/json'
116
+ // message.debug(`File type: ${file.type}`)
117
+ if ( ! isJpgOrPng && ! isJson ) {
118
+ message . error ( 'You can only upload JSON or JPG/PNG file' )
110
119
}
111
- return isJpgOrPng
120
+ return isJpgOrPng || isJson
112
121
}
113
122
114
123
return (
@@ -151,7 +160,7 @@ const QrCodeScanner = ({ onScan, shouldInit, style }) => {
151
160
beforeUpload = { beforeUpload }
152
161
onChange = { onQrcodeChange }
153
162
>
154
- < Button shape = 'round' icon = { qrCodeImageUploading ? < LoadingOutlined /> : < UploadOutlined /> } > Use Image Instead</ Button >
163
+ < Button shape = 'round' icon = { qrCodeImageUploading ? < LoadingOutlined /> : < UploadOutlined /> } > Use Image or JSON Instead</ Button >
155
164
</ Upload >
156
165
</ Row >
157
166
</ >
0 commit comments