2019-07-18 16:59:42 +00:00
|
|
|
import { Injectable, OnDestroy } from '@angular/core';
|
2019-07-15 14:54:19 +00:00
|
|
|
import { OAuthService } from 'angular-oauth2-oidc';
|
2019-07-18 16:59:42 +00:00
|
|
|
import { Subject , Subscription } from 'rxjs';
|
|
|
|
import { HttpClient } from "@angular/common/http";
|
|
|
|
import { UploadxService, UploadState} from 'ngx-uploadx';
|
|
|
|
import { AppConfig } from '../../shared/app.config';
|
2019-07-15 14:54:19 +00:00
|
|
|
|
|
|
|
|
2019-07-18 16:59:42 +00:00
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
export class ResumableFileUploadService implements OnDestroy{
|
2019-07-15 14:54:19 +00:00
|
|
|
public files: Array<File> = new Array<File>();
|
|
|
|
public isUploading = false;
|
|
|
|
public totalProgress = "0";
|
|
|
|
public isClosed = true;
|
|
|
|
public isMinimized = false;
|
|
|
|
public parentCode: string;
|
|
|
|
public refresh: Subject<any> = new Subject<any>();
|
2019-07-18 16:59:42 +00:00
|
|
|
private _eventSub:Subscription;
|
|
|
|
private initialized = false;
|
2019-07-15 14:54:19 +00:00
|
|
|
|
2019-07-18 16:59:42 +00:00
|
|
|
constructor(private httpClient: HttpClient,private oauthService: OAuthService,private uploadService: UploadxService,public appConfig: AppConfig) {
|
|
|
|
|
2019-07-15 14:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 16:59:42 +00:00
|
|
|
endPoint() {
|
|
|
|
return `${this.appConfig.getConfig("apiEndPoint")}/api/v1/file`;
|
2019-07-15 14:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 16:59:42 +00:00
|
|
|
init() {
|
|
|
|
if(!this.initialized) {
|
|
|
|
this._eventSub=this.uploadService.init({
|
|
|
|
endpoint:this.endPoint(),
|
|
|
|
token:() => this.oauthService.getAccessToken(),
|
|
|
|
chunkSize: 2097152}).subscribe((uploadState:UploadState) => {
|
|
|
|
this.handleState(uploadState);
|
|
|
|
} );
|
|
|
|
this.initialized=true;
|
2019-07-15 14:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 16:59:42 +00:00
|
|
|
handleState(state:UploadState) {
|
|
|
|
switch(state.status) {
|
|
|
|
case "added": {
|
|
|
|
this.files.push(new File(state));
|
|
|
|
};break;
|
|
|
|
case "uploading":
|
|
|
|
case "complete": {
|
|
|
|
var file =this.files.find((f) => f.identifier == state.uploadId )
|
|
|
|
if(file) {
|
|
|
|
file.progress = (state.progress * 100) + '%';
|
|
|
|
}
|
|
|
|
};break;
|
|
|
|
}
|
2019-07-15 14:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 16:59:42 +00:00
|
|
|
addFiles = (files: any[], event: any, geoRefJson?: string, attributes?: any) => {
|
|
|
|
for (let f of files) {
|
|
|
|
this.uploadService.handleFile(f);
|
|
|
|
// if (geoRefJson) f.geoRefJson = geoRefJson;
|
|
|
|
// if (attributes) f.attributes = JSON.stringify(attributes);
|
2019-07-15 14:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-18 16:59:42 +00:00
|
|
|
|
2019-07-15 14:54:19 +00:00
|
|
|
toggleMinimize = function () {
|
|
|
|
this.isMinimized = !this.isMinimized;
|
|
|
|
};
|
|
|
|
|
|
|
|
cancelFile = function (file) {
|
2019-07-18 16:59:42 +00:00
|
|
|
this.uploadService.control({action:'cancel',uploadId:file.identifier});
|
2019-07-15 14:54:19 +00:00
|
|
|
var index = this.files.indexOf(file, 0);
|
|
|
|
if (index > -1) {
|
|
|
|
this.files.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
doClose = function () {
|
2019-07-18 16:59:42 +00:00
|
|
|
this.uploadService.control({action:'cancelAll'});
|
2019-07-15 14:54:19 +00:00
|
|
|
this.files = new Array<File>();
|
|
|
|
this.isClosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
close = function () {
|
|
|
|
let close = true;
|
|
|
|
if (this.isUploading) {
|
|
|
|
close = false;
|
|
|
|
}
|
|
|
|
if (close) {
|
|
|
|
this.doClose();
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 16:59:42 +00:00
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
if(this._eventSub) this._eventSub.unsubscribe();
|
|
|
|
}
|
2019-07-15 14:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class File {
|
|
|
|
private file: any;
|
|
|
|
public fileName: string;
|
|
|
|
public progress: string;
|
|
|
|
public identifier: string;
|
|
|
|
public success: boolean;
|
|
|
|
public error: boolean;
|
|
|
|
public errorMessage: string;
|
|
|
|
|
|
|
|
|
2019-07-18 16:59:42 +00:00
|
|
|
constructor(state: UploadState) {
|
|
|
|
this.file = state;
|
|
|
|
this.fileName = state.file.name;
|
|
|
|
this.progress = (state.progress * 100) + '%';
|
|
|
|
this.identifier = state.uploadId;
|
2019-07-15 14:54:19 +00:00
|
|
|
this.success = false;
|
|
|
|
this.error = false;
|
|
|
|
this.errorMessage = "";
|
|
|
|
}
|
|
|
|
}
|