-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
FileSystemScreen.tsx
209 lines (192 loc) · 6.05 KB
/
FileSystemScreen.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
import React from 'react';
import {
Alert,
AsyncStorage,
Platform,
ProgressBarAndroid,
ProgressViewIOS,
ScrollView,
StyleSheet,
} from 'react-native';
import * as FileSystem from 'expo-file-system';
import { Asset } from 'expo-asset';
import ListButton from '../components/ListButton';
interface State {
downloadProgress: number;
}
export default class FileSystemScreen extends React.Component<{}, State> {
static navigationOptions = {
title: 'FileSystem',
};
readonly state: State = {
downloadProgress: 0,
};
download?: FileSystem.DownloadResumable;
_download = async () => {
const url = 'http://ipv4.download.thinkbroadband.com/256KB.zip';
await FileSystem.downloadAsync(url, FileSystem.documentDirectory + '256KB.zip');
alert('Download complete!');
}
_startDownloading = async () => {
const url = 'http://ipv4.download.thinkbroadband.com/5MB.zip';
const fileUri = FileSystem.documentDirectory + '5MB.zip';
const callback: FileSystem.DownloadProgressCallback = downloadProgress => {
const progress =
downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
this.setState({
downloadProgress: progress,
});
};
const options = { md5: true };
this.download = FileSystem.createDownloadResumable(url, fileUri, options, callback);
try {
await this.download.downloadAsync();
if (this.state.downloadProgress === 1) {
alert('Download complete!');
}
} catch (e) {
console.log(e);
}
}
_pause = async () => {
if (!this.download) {
alert('Initiate a download first!');
return;
}
try {
const downloadSnapshot = await this.download.pauseAsync();
await AsyncStorage.setItem('pausedDownload', JSON.stringify(downloadSnapshot));
alert('Download paused...');
} catch (e) {
console.log(e);
}
}
_resume = async () => {
try {
if (this.download) {
await this.download.resumeAsync();
if (this.state.downloadProgress === 1) {
alert('Download complete!');
}
} else {
this._fetchDownload();
}
} catch (e) {
console.log(e);
}
}
_fetchDownload = async () => {
try {
const downloadJson = await AsyncStorage.getItem('pausedDownload');
if (downloadJson !== null) {
const downloadFromStore = JSON.parse(downloadJson);
const callback: FileSystem.DownloadProgressCallback = downloadProgress => {
const progress =
downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
this.setState({
downloadProgress: progress,
});
};
this.download = new FileSystem.DownloadResumable(
downloadFromStore.url,
downloadFromStore.fileUri,
downloadFromStore.options,
callback,
downloadFromStore.resumeData
);
await this.download.resumeAsync();
if (this.state.downloadProgress === 1) {
alert('Download complete!');
}
} else {
alert('Initiate a download first!');
return;
}
} catch (e) {
console.log(e);
}
}
_getInfo = async () => {
if (!this.download) {
alert('Initiate a download first!');
return;
}
try {
const info = await FileSystem.getInfoAsync(this.download._fileUri);
Alert.alert('File Info:', JSON.stringify(info), [{ text: 'OK', onPress: () => {} }]);
} catch (e) {
console.log(e);
}
}
_readAsset = async () => {
const asset = Asset.fromModule(require('../../assets/index.html'));
await asset.downloadAsync();
try {
const result = await FileSystem.readAsStringAsync(asset.localUri!);
Alert.alert('Result', result);
} catch (e) {
Alert.alert('Error', e.message);
}
}
_getInfoAsset = async () => {
const asset = Asset.fromModule(require('../../assets/index.html'));
await asset.downloadAsync();
try {
const result = await FileSystem.getInfoAsync(asset.localUri!);
Alert.alert('Result', JSON.stringify(result, null, 2));
} catch (e) {
Alert.alert('Error', e.message);
}
}
_copyAndReadAsset = async () => {
const asset = Asset.fromModule(require('../../assets/index.html'));
await asset.downloadAsync();
const tmpFile = FileSystem.cacheDirectory + 'test.html';
try {
await FileSystem.copyAsync({ from: asset.localUri!, to: tmpFile });
const result = await FileSystem.readAsStringAsync(tmpFile);
Alert.alert('Result', result);
} catch (e) {
Alert.alert('Error', e.message);
}
}
_alertFreeSpace = async () => {
const freeBytes = await FileSystem.getFreeDiskStorageAsync();
alert(`${Math.round(freeBytes / 1024 / 1024)} MB available`);
}
render() {
let progress = null;
if (Platform.OS === 'ios') {
progress = <ProgressViewIOS style={styles.progress} progress={this.state.downloadProgress} />;
} else {
progress = (
<ProgressBarAndroid
style={styles.progress}
styleAttr="Horizontal"
indeterminate={false}
progress={this.state.downloadProgress}
/>
);
}
return (
<ScrollView style={{ padding: 10 }}>
<ListButton onPress={this._download} title="Download file (512KB)" />
<ListButton onPress={this._startDownloading} title="Start Downloading file (5MB)" />
<ListButton onPress={this._pause} title="Pause Download" />
<ListButton onPress={this._resume} title="Resume Download" />
<ListButton onPress={this._getInfo} title="Get Info" />
{progress}
<ListButton onPress={this._readAsset} title="Read Asset" />
<ListButton onPress={this._getInfoAsset} title="Get Info Asset" />
<ListButton onPress={this._copyAndReadAsset} title="Copy and Read Asset" />
<ListButton onPress={this._alertFreeSpace} title="Alert free space" />
</ScrollView>
);
}
}
const styles = StyleSheet.create({
progress: {
marginHorizontal: 10,
marginVertical: 32,
},
});