-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
256 lines (233 loc) · 6.53 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, {useState} from 'react';
import {
SafeAreaView,
StatusBar,
View,
StyleSheet,
TouchableHighlight,
} from 'react-native';
import {WebView} from 'react-native-webview';
import {
downloadFile,
unlink,
DocumentDirectoryPath,
writeFile,
mkdir,
} from 'react-native-fs';
import {
zipWithPassword,
unzip,
unzipWithPassword,
isPasswordProtected,
subscribe as subscribeToZipArchive,
} from 'react-native-zip-archive';
import {
Provider as PaperProvider,
FAB,
ProgressBar,
Modal,
Portal,
MD3Colors,
} from 'react-native-paper';
console.log(`working dir is ${DocumentDirectoryPath}`);
const App = () => {
const [progress, setProgress] = useState(0);
const [isShowingProgress, toggleProgress] = useState(false);
const [isShowingWebview, toggleWebview] = useState(false);
const [webviewUrl, setWebviewUrl] = useState(
'https://github.com/mockingbot/react-native-zip-archive/',
);
function getFilename(path: string) {
const filenameWithExt = path.split('/').pop();
const filenameAsList = filenameWithExt!.split('.');
filenameAsList.pop();
return filenameAsList.join('.');
}
const remoteZipFileUrl =
'https://rnza-test-app-assets.firebaseapp.com/static_password.zip';
async function downloadArchive() {
const zipFilePath = `${DocumentDirectoryPath}/${remoteZipFileUrl
.split('/')
.pop()}`;
try {
await unlink(zipFilePath);
} catch (error) {}
showProgress();
const downloadPromise = downloadFile({
fromUrl: remoteZipFileUrl,
toFile: zipFilePath,
progress: function ({contentLength, jobId, bytesWritten}) {
if (jobId === downloadPromise.jobId) {
setProgressAndPrint(bytesWritten / contentLength);
}
},
});
const downloadResult = await downloadPromise.promise;
if (downloadResult.statusCode !== 200) {
throw new Error(downloadResult.statusCode.toString());
}
return zipFilePath;
}
async function startArchiveTest() {
const folder = `${DocumentDirectoryPath}/test`;
const file = `${DocumentDirectoryPath}/test.zip`;
setProgressAndPrint(0);
showProgress();
try {
await unlink(folder);
} catch (error) {}
try {
await unlink(file);
} catch (error) {}
try {
await mkdir(folder);
await writeFile(`${folder}/test1.txt`, 'this is a test1', 'utf8');
await writeFile(`${folder}/test2.txt`, 'this is a test2', 'utf8');
const subscription = subscribeToZipArchive(function ({
progress: zipProgress,
filePath,
}) {
console.log(`zipping to ${filePath}`);
setProgressAndPrint(zipProgress);
});
await zipWithPassword(
[`${folder}/test1.txt`],
`${DocumentDirectoryPath}/test.zip`,
'password',
);
subscription.remove();
setProgressAndPrint(1);
hideProgress();
await unlink(folder);
console.log(`zipped ${folder}`);
} catch (error) {
console.error(error);
}
}
async function startUnzipTest() {
setProgressAndPrint(0);
showProgress();
const unzippedDir = `${DocumentDirectoryPath}/${getFilename(
remoteZipFileUrl,
)}`;
try {
await unlink(unzippedDir);
} catch (error) {}
try {
let zipFilePath = await downloadArchive();
zipFilePath = `file://${zipFilePath}`;
const subscription = subscribeToZipArchive(function ({
progress: unzipProgress,
filePath,
}) {
console.log(`unzipping to ${filePath}`);
setProgressAndPrint(unzipProgress);
});
const isPasswordProtectedZip = await isPasswordProtected(zipFilePath);
if (isPasswordProtectedZip) {
// TODO: prompt password dialog to user
const password = 'helloworld';
await unzipWithPassword(zipFilePath, DocumentDirectoryPath, password);
} else {
await unzip(zipFilePath, DocumentDirectoryPath);
}
subscription.remove();
showWebview();
setWebviewUrl(`file://${unzippedDir}/index.html`);
await unlink(zipFilePath);
} catch (error) {
console.log(error);
}
setProgressAndPrint(1);
hideProgress();
}
function setProgressAndPrint(progress: number) {
console.log(`progress: ${progress}`);
setProgress(progress);
}
function showProgress() {
toggleProgress(true);
}
function hideProgress() {
toggleProgress(false);
}
function showWebview() {
toggleWebview(true);
}
function hideWebview() {
toggleWebview(false);
}
return (
<>
<PaperProvider>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.full}>
<View style={[styles.full, styles.center]}>
<View style={styles.buttons}>
<TouchableHighlight>
<FAB
onPress={startUnzipTest}
icon="folder-open"
label="Unzip"
/>
</TouchableHighlight>
<TouchableHighlight>
<FAB
onPress={startArchiveTest}
icon="zip-box"
label="Archive"
color={MD3Colors.neutralVariant100}
style={{backgroundColor: MD3Colors.neutral10}}
/>
</TouchableHighlight>
</View>
</View>
<Portal>
<Modal
visible={isShowingProgress}
contentContainerStyle={styles.modal}>
<ProgressBar progress={progress} color={MD3Colors.secondary40} />
</Modal>
</Portal>
<Portal>
<Modal
visible={isShowingWebview}
onDismiss={hideWebview}
contentContainerStyle={styles.modal}>
<WebView
source={{uri: webviewUrl}}
originWhitelist={['http://*', 'https://*', 'file://*']}
startInLoadingState
allowFileAccess
onError={syntheticEvent => {
const {nativeEvent} = syntheticEvent;
console.warn('WebView error: ', nativeEvent);
}}
/>
</Modal>
</Portal>
</SafeAreaView>
</PaperProvider>
</>
);
};
const styles = StyleSheet.create({
full: {
flex: 1,
},
center: {
justifyContent: 'center',
alignItems: 'center',
},
modal: {
flex: 0,
alignSelf: 'stretch',
height: 400,
paddingHorizontal: 40,
},
buttons: {
height: 200,
justifyContent: 'space-around',
},
});
export default App;