update file upload service

This commit is contained in:
Dan Percic 2021-10-14 22:22:29 +03:00
parent e386c6a28f
commit ea44a716ab
2 changed files with 26 additions and 15 deletions

View File

@ -1,4 +1,4 @@
import { ApplicationRef, Injectable } from '@angular/core'; import { ApplicationRef, Injectable, Injector } from '@angular/core';
import { FileUploadModel } from '../model/file-upload.model'; import { FileUploadModel } from '../model/file-upload.model';
import { AppStateService } from '@state/app-state.service'; import { AppStateService } from '@state/app-state.service';
import { HttpErrorResponse, HttpEventType } from '@angular/common/http'; import { HttpErrorResponse, HttpEventType } from '@angular/common/http';
@ -6,10 +6,11 @@ import { interval, Subscription } from 'rxjs';
import { ConfigService } from '@services/config.service'; import { ConfigService } from '@services/config.service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { UploadDownloadDialogService } from './upload-download-dialog.service'; import { UploadDownloadDialogService } from './upload-download-dialog.service';
import { UploadControllerService } from '@redaction/red-ui-http'; import { FileUploadResult } from '@redaction/red-ui-http';
import { isCsv } from '@utils/file-drop-utils'; import { isCsv } from '@utils/file-drop-utils';
import { ErrorMessageService } from '@iqser/common-ui'; import { ErrorMessageService, GenericService, RequiredParam, Validate } from '@iqser/common-ui';
import { DossiersService } from '@services/entity-services/dossiers.service'; import { DossiersService } from '@services/entity-services/dossiers.service';
import { HeadersConfiguration } from '../../../../../../../libs/common-ui/src/lib/utils/headers-configuration';
export interface ActiveUpload { export interface ActiveUpload {
subscription: Subscription; subscription: Subscription;
@ -17,7 +18,7 @@ export interface ActiveUpload {
} }
@Injectable() @Injectable()
export class FileUploadService { export class FileUploadService extends GenericService<FileUploadResult> {
static readonly MAX_PARALLEL_UPLOADS = 5; static readonly MAX_PARALLEL_UPLOADS = 5;
files: FileUploadModel[] = []; files: FileUploadModel[] = [];
groupedFiles: { [key: string]: FileUploadModel[] } = {}; groupedFiles: { [key: string]: FileUploadModel[] } = {};
@ -32,11 +33,11 @@ export class FileUploadService {
private readonly _applicationRef: ApplicationRef, private readonly _applicationRef: ApplicationRef,
private readonly _translateService: TranslateService, private readonly _translateService: TranslateService,
private readonly _configService: ConfigService, private readonly _configService: ConfigService,
private readonly _uploadControllerService: UploadControllerService,
private readonly _dialogService: UploadDownloadDialogService, private readonly _dialogService: UploadDownloadDialogService,
private readonly _errorMessageService: ErrorMessageService, private readonly _errorMessageService: ErrorMessageService,
protected readonly _injector: Injector,
) { ) {
this._uploadControllerService.defaultHeaders = this._uploadControllerService.defaultHeaders.append('ngsw-bypass', 'true'); super(_injector, 'upload');
interval(2500).subscribe(() => { interval(2500).subscribe(() => {
this._handleUploads(); this._handleUploads();
}); });
@ -120,6 +121,23 @@ export class FileUploadService {
} }
} }
@Validate()
uploadFileForm(@RequiredParam() dossierId: string, file?: Blob) {
const formParams = new FormData();
if (file !== undefined) {
formParams.append('file', file);
}
const headers = HeadersConfiguration.getHeaders({ contentType: false }).append('ngsw-bypass', 'true');
return this._http.post<FileUploadResult>(`/${this._defaultModelPath}/${dossierId}`, formParams, {
headers,
observe: 'events',
reportProgress: true,
});
}
private _addFileToGroup(file: FileUploadModel) { private _addFileToGroup(file: FileUploadModel) {
if (!this.groupedFiles[file.dossierId]) { if (!this.groupedFiles[file.dossierId]) {
this.groupedFiles[file.dossierId] = []; this.groupedFiles[file.dossierId] = [];
@ -155,7 +173,7 @@ export class FileUploadService {
private _createSubscription(uploadFile: FileUploadModel) { private _createSubscription(uploadFile: FileUploadModel) {
this.activeUploads++; this.activeUploads++;
const obs = this._uploadControllerService.uploadFileForm(uploadFile.dossierId, uploadFile.file, 'events', true); const obs = this.uploadFileForm(uploadFile.dossierId, uploadFile.file);
return obs.subscribe( return obs.subscribe(
async event => { async event => {
if (event.type === HttpEventType.UploadProgress) { if (event.type === HttpEventType.UploadProgress) {

View File

@ -1,7 +1,6 @@
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core'; import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
import { Configuration } from './configuration'; import { Configuration } from './configuration';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { UploadControllerService } from './api/uploadController.service';
import { ViewedPagesControllerService } from './api/viewedPagesController.service'; import { ViewedPagesControllerService } from './api/viewedPagesController.service';
import { WatermarkControllerService } from './api/watermarkController.service'; import { WatermarkControllerService } from './api/watermarkController.service';
import { SearchControllerService } from './api/searchController.service'; import { SearchControllerService } from './api/searchController.service';
@ -11,13 +10,7 @@ import { NotificationControllerService } from './api/notificationController.serv
imports: [], imports: [],
declarations: [], declarations: [],
exports: [], exports: [],
providers: [ providers: [ViewedPagesControllerService, WatermarkControllerService, SearchControllerService, NotificationControllerService],
UploadControllerService,
ViewedPagesControllerService,
WatermarkControllerService,
SearchControllerService,
NotificationControllerService,
],
}) })
export class ApiModule { export class ApiModule {
constructor(@Optional() @SkipSelf() parentModule: ApiModule, @Optional() http: HttpClient) { constructor(@Optional() @SkipSelf() parentModule: ApiModule, @Optional() http: HttpClient) {