FarmMapsLib/projects/common/src/fm/components/resumable-file-upload/resumable-file-upload.service.ts

150 lines
4.1 KiB
TypeScript
Raw Normal View History

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";
2019-07-19 12:28:54 +00:00
import { UploadxService, UploadState,UploadxOptions} from 'ngx-uploadx';
2019-07-18 16:59:42 +00:00
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;
2019-07-19 07:36:05 +00:00
public totalProgress = 0;
2019-07-15 14:54:19 +00:00
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-19 07:36:05 +00:00
updatetotalprogress() {
var totalProgress =0;
2019-07-19 08:01:39 +00:00
var n=0;
2019-07-19 07:36:05 +00:00
for(var i =0;i<this.files.length;i++) {
2019-07-19 08:01:39 +00:00
if(!this.files[i].error) {
totalProgress+=this.files[i].progress;
n++;
}
2019-07-19 07:36:05 +00:00
}
this.totalProgress=totalProgress/this.files.length;
2019-07-19 08:23:30 +00:00
if(this.totalProgress==100) this.isUploading=false;
2019-07-19 07:36:05 +00:00
}
2019-07-18 16:59:42 +00:00
handleState(state:UploadState) {
switch(state.status) {
2019-09-16 14:58:57 +00:00
case "queue": {
2019-07-18 16:59:42 +00:00
this.files.push(new File(state));
2019-07-19 07:36:05 +00:00
this.isClosed=false;
};break;
case "uploading": {
2019-07-19 08:07:01 +00:00
this.isUploading = true;
2019-07-19 07:36:05 +00:00
var file =this.files.find((f) => f.identifier == state.uploadId )
if(file) {
file.progress = (state.progress?state.progress:0);
}
2019-07-18 16:59:42 +00:00
};break;
case "complete": {
var file =this.files.find((f) => f.identifier == state.uploadId )
if(file) {
2019-08-16 08:07:53 +00:00
var parts = state.url.split("/");
file.itemCode = parts[parts.length-1];
2019-07-19 07:36:05 +00:00
file.progress = (state.progress?state.progress:0);
2019-09-16 14:58:57 +00:00
file.success=true;
2019-07-18 16:59:42 +00:00
}
2019-07-19 07:36:05 +00:00
};break;
case "error": {
var file =this.files.find((f) => f.identifier == state.uploadId )
if(file) {
file.error=true;
file.errorMessage = state.response;
}
2019-07-18 16:59:42 +00:00
};break;
}
2019-07-19 08:01:39 +00:00
this.updatetotalprogress();
2019-07-19 07:36:05 +00:00
this.refresh.next({});
2019-07-15 14:54:19 +00:00
}
2019-07-19 12:28:54 +00:00
addFiles = (files: any[], event: any, metadata:any) => {
2019-07-18 16:59:42 +00:00
for (let f of files) {
2019-07-19 12:28:54 +00:00
var options:UploadxOptions = {metadata:metadata};
this.uploadService.handleFile(f,options);
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;
2019-07-19 07:36:05 +00:00
public progress: number;
2019-07-15 14:54:19 +00:00
public identifier: string;
2019-08-16 08:07:53 +00:00
public itemCode: string;
2019-07-15 14:54:19 +00:00
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;
2019-07-19 07:36:05 +00:00
this.progress = state.progress?state.progress:0;
2019-07-18 16:59:42 +00:00
this.identifier = state.uploadId;
2019-07-15 14:54:19 +00:00
this.success = false;
this.error = false;
this.errorMessage = "";
2019-08-16 08:07:53 +00:00
this.itemCode = null;
2019-07-15 14:54:19 +00:00
}
}