2019-11-05 13:40:01 +09:00
|
|
|
import { Observable, Subject } from 'rxjs';
|
|
|
|
import { share } from 'rxjs/operators';
|
|
|
|
|
|
|
|
export class FileUploadItem {
|
|
|
|
file: File;
|
|
|
|
uploadTime: number;
|
|
|
|
uploadingProgress$: Observable<number>;
|
|
|
|
|
|
|
|
private uploadingProgress: Subject<number>;
|
|
|
|
private uploadStartTime: number;
|
|
|
|
|
2019-12-04 17:58:59 +09:00
|
|
|
private constructor(file?: File) {
|
2019-11-05 13:40:01 +09:00
|
|
|
this.file = file;
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:55:17 +09:00
|
|
|
static fromFiles(files: FileList): FileUploadItem[] {
|
2019-11-05 13:40:01 +09:00
|
|
|
const fileItems: FileUploadItem[] = [];
|
2019-11-05 14:55:17 +09:00
|
|
|
|
|
|
|
// tslint:disable-next-line: prefer-for-of
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
fileItems.push(new FileUploadItem(files[i]));
|
2019-11-05 13:40:01 +09:00
|
|
|
}
|
2019-11-05 14:55:17 +09:00
|
|
|
|
2019-11-05 13:40:01 +09:00
|
|
|
return fileItems;
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:58:59 +09:00
|
|
|
static from(): FileUploadItem {
|
|
|
|
return new FileUploadItem();
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:40:01 +09:00
|
|
|
uploadStart(): Subject<number> {
|
|
|
|
this.uploadStartTime = new Date().getTime();
|
|
|
|
this.uploadingProgress = new Subject<number>();
|
|
|
|
this.uploadingProgress$ = this.uploadingProgress
|
|
|
|
.asObservable()
|
|
|
|
.pipe(share());
|
|
|
|
return this.uploadingProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadComplete() {
|
|
|
|
const endTime = new Date().getTime();
|
|
|
|
this.uploadTime = endTime - this.uploadStartTime;
|
|
|
|
this.uploadingProgress.complete();
|
|
|
|
}
|
|
|
|
}
|