file-replicator.ts
4.46 KB
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
import {FileHandler} from './api/file-handler';
import {File} from './api/file';
import {Util} from './util';
import {EventEmitter} from 'events';
export class FileReplicator extends EventEmitter {
constructor() {
super();
}
protected _files:Array<File> = [];
protected _itemValidator: (item:any) => boolean = null;
protected _fileHandler:FileHandler = null;
protected _retryTimeout:number = 0;
protected _itemKey = "type";
protected _itemValue = "asset";
protected _itemSourceAttribute = "source";
protected _itemTargetAttribute = "target";
get files(): Array<File> {
return this._files;
}
set fileHandler (handler:FileHandler) {
this._fileHandler = handler;
}
set retryTimeout (timeout:number) {
this._retryTimeout = timeout;
}
set itemValidator(validator:(item:any) => boolean) {
this._itemValidator = validator;
}
set itemKey(key:string) {
this._itemKey = key;
}
set itemValue(value:string) {
this._itemValue = value;
}
set itemSourceAttribute(sourceAttribute:string) {
this._itemSourceAttribute = sourceAttribute;
}
set itemTargetAttribute(targetAttribute:string) {
this._itemTargetAttribute = targetAttribute;
}
get itemKey() {
return this._itemKey;
}
get itemValue() {
return this._itemValue;
}
get itemSourceAttribute() {
return this._itemSourceAttribute;
}
get itemTargetAttribute() {
return this._itemTargetAttribute;
}
init(files: Array<File> = []) {
this._files = files;
}
/**
* change from pouchdb replicate
*/
pushChanges(change:any) {
let items:Array<any> = [];
if (change && change.docs && change.docs.length > 0) {
for (let item of change.docs) {
if (item[this._itemKey] && item[this._itemKey] === this._itemValue) {
items.push(item);
}
}
}
let files = this.prepareFiles(items);
for (let file of files) {
this._files.push(file);
}
}
downloadFiles(files:Array<File>, fileHandler:FileHandler, index:number = 0) {
if (index >= files.length) {
return;
}
this.emit('start', { progress: 0, index : index, length : files.length });
fileHandler
.download(files[index].source, files[index].target)
.subscribe(
progress => {
this.emit('progress', { progress : progress, index : index, length : files.length })
} ,
error => {
this.emit('error', { progress : 0, index : index, length : files.length, error: error });
} ,
() => {
this.emit('complete', { progress : 100 , index : index, length : files.length });
this.downloadFiles(files, fileHandler, index+1);
}
);
}
prepareFiles(items: Array<any>) : Array<File> {
let output = [];
for (let item of items) {
if (item[this._itemSourceAttribute] && (!this._itemValidator || this._itemValidator(item))) {
let file = { source : item[this._itemSourceAttribute] , target : '' };
if (item[this._itemTargetAttribute]) {
file.target = item[this._itemTargetAttribute];
} else {
file.target = Util.getNameHash(file.source);
}
output.push(file);
}
}
return output;
}
start() {
this.on('complete', (event:any) => {
if ((event.index + 1) >= event.length) {
this.replicationFinalized(event.index);
}
});
this.on('error', (event:any) => {
this.replicationFinalized(event.index);
});
this.downloadFiles(this._files, this._fileHandler);
}
replicationFinalized(lastIndex:number) {
if (lastIndex+1 >= this._files.length) { // all finished
this._files = [];
this.emit('final');
} else if (this._retryTimeout > 0) { // restart after last success
this._files.splice(0,lastIndex);
setTimeout(() => {
this.downloadFiles(this._files, this._fileHandler);
}, this._retryTimeout);
}
}
}