-
Notifications
You must be signed in to change notification settings - Fork 7
/
MediaFileMgr.js
executable file
·196 lines (192 loc) · 6.97 KB
/
MediaFileMgr.js
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
'use strict';
const React = require('react');const ReactNative = require('react-native');
const {
AsyncStorage,
Platform,
} = ReactNative;
const moment = require('moment');
const EventEmitter = require('EventEmitter');
const fs = require('react-native-fs');
const DocumentPath = Platform.OS === 'android' ? fs.ExternalStorageDirectoryPath + '/mediaRecodes/' : fs.DocumentDirectoryPath + '/mediaRecodes/';
console.log(DocumentPath);
const ITEM_NAME = 'mediaRecordeFiles';
const ITEM_NAME_INDEX = 'mediaRecordeFilesIndex';
class Manager extends EventEmitter {
constructor () {
super();
this.get();
}
get () {
return new Promise(async(resolve, reject) => {
let data = {};
let index = 0;
try {
const nameStr = await AsyncStorage.getItem(ITEM_NAME);
data = JSON.parse(nameStr || '{}');
} catch (e) {
data = {};
}
this.data = data;
try {
const indexStr = await AsyncStorage.getItem(ITEM_NAME_INDEX);
index = JSON.parse(indexStr || '0');
} catch (e) {
index = 0;
}
this.index = index;
this.tempItem = { images:[], videos:[] };
});
}
resetItem () {
this.tempItem = { images:[], videos:[] };
}
checkRootDir () {
return new Promise(async(resolve, reject) => {
const exist = await fs.exists(DocumentPath);
if (!exist) {
await fs.mkdir(DocumentPath);
}
await fs.mkdir(DocumentPath);
resolve(true);
});
}
addMediaFile (path, isImage) {
return new Promise(async(resolve, reject) => {
this.index++;
const expandName = path.replace(/^.*(\..*)$/, '$1');
await AsyncStorage.setItem(ITEM_NAME_INDEX, JSON.stringify(this.index));
const item = {
mediaId: this.index,
url: DocumentPath + this.index + expandName,
local: true,
};
await fs.copyFile(path, item.url);
const taskItem = this.tempItem;
const list = isImage ? taskItem.images : taskItem.videos;
list.push(item);
resolve(list);
});
}
removeMediaFile (accusationId, url, isImage, noNeedChecked) {
return new Promise(async(resolve, reject) => {
if (noNeedChecked ? false : await fs.exists(url)) {
await fs.unlink(url);
}
const taskItem = this.data[accusationId];
if (taskItem) {
let list = isImage ? taskItem.images : taskItem.videos;
if (list) {
list = _.reject(list, (item) => item.url === url);
if (list.length) {
if (isImage) {
taskItem.images = list;
} else {
taskItem.videos = list;
}
} else {
if (isImage) {
if (taskItem.videos.length) {
taskItem.images = [];
} else {
delete this.data[accusationId];
this.emit('MEDIA_UPLOAD_ALL_EVENT', { accusationId });
}
} else {
if (taskItem.images.length) {
taskItem.videos = [];
} else {
delete this.data[accusationId];
this.emit('MEDIA_UPLOAD_ALL_EVENT', { accusationId });
}
}
}
await AsyncStorage.setItem(ITEM_NAME, JSON.stringify(this.data));
}
}
resolve();
});
}
async startTryUploadTasks (accusationId) {
this.data[accusationId] = this.tempItem;
await AsyncStorage.setItem(ITEM_NAME, JSON.stringify(this.data));
setTimeout(() => this.tryUploadTasks(), 1000);
}
tryUploadTasks () {
if (!this.isUpLoading) {
const accusationId = _.keys(this.data)[0];
if (accusationId) {
const { connect, fee } = app.net.info;
const { onlyWifiUpload } = app.setting.data;
if (connect) {
if (!(fee && onlyWifiUpload)) {
this.isUpLoading = true;
this.tryUploadMediaFile(accusationId);
}
}
}
}
}
tryUploadMediaFile (accusationId) {
const task = this.data[accusationId];
if (task) {
const { images, videos } = task;
if (images.length) {
this.uploadMediaFile(accusationId, images[0], true);
} else if (videos.length) {
this.uploadMediaFile(accusationId, videos[0], false);
}
} else {
const accusationId = _.keys(this.data)[0];
if (accusationId) {
this.tryUploadMediaFile(accusationId);
} else {
this.isUpLoading = false;
}
}
}
uploadMediaFile (accusationId, media, isImage) {
const { url, mediaId } = media;
const options = {
fileKey: 'file',
fileName: url.substr(url.lastIndexOf('/') + 1),
mimeType: isImage ? 'image/jpeg' : 'm4a',
params: {
type: isImage ? 0 : 1,
},
};
UPLOAD(url, app.route.ROUTE_UPLOAD_MEDIA_FILE, options, (progress) => {
this.emit('MEDIA_UPLOAD_PROGREES_EVENT', { accusationId, isImage, mediaId, progress:parseInt(progress.loaded * 100 / progress.total) });
}, (data) => {
this.removeMediaFile(accusationId, url, isImage).then(() => {
this.emit('MEDIA_UPLOAD_UPLOADED_EVENT', { accusationId, isImage, url: data.context.url, mediaId });
this.tryUploadMediaFile(accusationId);
});
}, async (error) => {
const exist = await fs.exists(url);
if (!exist) {
this.removeMediaFile(accusationId, url, isImage, true).then(() => {
this.tryUploadMediaFile(accusationId);
});
} else {
this.isUpLoading = false;
Toast('上传失败, 需要在设置里面启动上传');
}
});
}
onNetChange (info) {
if (info.connect) {
this.tryUploadTasks();
} else {
this.isUpLoading = false;
}
}
getMediaFile (accusationId) {
return this.data[accusationId] | { images:[], videos:[] };
}
clear () {
this.data = {};
AsyncStorage.removeItem(ITEM_NAME);
}
}
module.exports = new Manager();