From 178740bbde9adda25f67f0c4906dac83a17cbc0f Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Sun, 29 Sep 2019 13:48:01 +0100 Subject: [PATCH] [ADF-4921] upload widget fixes (#5109) * upload widget fixes * fix mocks --- lib/core/i18n/en.json | 3 +- .../attach-file-cloud-widget.component.html | 18 ++-- ...attach-file-cloud-widget.component.spec.ts | 2 + .../attach-file-cloud-widget.component.ts | 36 +++----- .../form/components/upload-cloud.widget.html | 8 +- .../form/components/upload-cloud.widget.ts | 78 +++++++++-------- .../services/process-cloud-content.service.ts | 85 +++++++++++-------- 7 files changed, 123 insertions(+), 107 deletions(-) diff --git a/lib/core/i18n/en.json b/lib/core/i18n/en.json index 684b2258b0..70479ed93f 100644 --- a/lib/core/i18n/en.json +++ b/lib/core/i18n/en.json @@ -33,7 +33,8 @@ "NOT_LESS_THAN": "Can't be less than {{ minValue }}", "AT_LEAST_LONG": "Enter at least {{ minLength }} characters", "NO_LONGER_THAN": "Enter no more than {{ maxLength }} characters" - } + }, + "FILE_ALREADY_UPLOADED": "A file with the same name is already uploaded." }, "FORM_RENDERER": { "NAMELESS_TASK": "Nameless task" diff --git a/lib/process-services-cloud/src/lib/form/components/attach-file-cloud-widget/attach-file-cloud-widget.component.html b/lib/process-services-cloud/src/lib/form/components/attach-file-cloud-widget/attach-file-cloud-widget.component.html index 40c5051fa1..9a4dee3a7e 100644 --- a/lib/process-services-cloud/src/lib/form/components/attach-file-cloud-widget/attach-file-cloud-widget.component.html +++ b/lib/process-services-cloud/src/lib/form/components/attach-file-cloud-widget/attach-file-cloud-widget.component.html @@ -39,32 +39,32 @@
- + - {{file.name}} - - - - diff --git a/lib/process-services-cloud/src/lib/form/components/upload-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/upload-cloud.widget.ts index 46dc171b70..95af89b081 100644 --- a/lib/process-services-cloud/src/lib/form/components/upload-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/upload-cloud.widget.ts @@ -18,9 +18,10 @@ /* tslint:disable:component-selector */ import { Component, ElementRef, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; +import { Node } from '@alfresco/js-api'; import { Observable, from } from 'rxjs'; -import { mergeMap, map, catchError } from 'rxjs/operators'; -import { WidgetComponent, baseHost, LogService, FormService, ThumbnailService, ContentLinkModel } from '@alfresco/adf-core'; +import { mergeMap } from 'rxjs/operators'; +import { WidgetComponent, baseHost, LogService, FormService, ThumbnailService, ContentLinkModel, NotificationService } from '@alfresco/adf-core'; import { ProcessCloudContentService } from '../services/process-cloud-content.service'; @Component({ @@ -44,6 +45,7 @@ export class UploadCloudWidgetComponent extends WidgetComponent implements OnIni public formService: FormService, private thumbnailService: ThumbnailService, public processCloudContentService: ProcessCloudContentService, + private notificationService: NotificationService, private logService: LogService) { super(formService); } @@ -53,6 +55,7 @@ export class UploadCloudWidgetComponent extends WidgetComponent implements OnIni this.field.value && this.field.value.length > 0) { this.hasFile = true; + this.fixIncompatibilityFromPreviousAndNewForm([]); } this.getMultipleFileParam(); } @@ -64,8 +67,16 @@ export class UploadCloudWidgetComponent extends WidgetComponent implements OnIni } onFileChanged(event: any) { - const files = event.target.files; - const filesSaved = []; + const files: File[] = []; + const filesSaved: Node[] = []; + + for (const file of Array.from(event.target.files)) { + if (!this.isUploaded(file)) { + files.push(file); + } else { + this.notificationService.showWarning('FORM.FIELD.FILE_ALREADY_UPLOADED'); + } + } if (files && files.length > 0) { from(files) @@ -83,34 +94,29 @@ export class UploadCloudWidgetComponent extends WidgetComponent implements OnIni } } - fixIncompatibilityFromPreviousAndNewForm(filesSaved) { - this.field.value = filesSaved; - this.field.form.values[this.field.id] = filesSaved; - this.hasFile = true; + private isUploaded(file: File): boolean { + const current: Node[] = this.field.value || []; + return current.some(entry => entry.name === file.name); } - getIcon(mimeType) { + protected fixIncompatibilityFromPreviousAndNewForm(filesSaved: Node[]) { + const value: Node[] = [...this.field.value || []]; + value.push(...filesSaved || []); + + this.field.value = value; + this.field.form.values[this.field.id] = value; + + this.hasFile = value.length > 0; + } + + getIcon(mimeType: string): string { return this.thumbnailService.getMimeTypeIcon(mimeType); } - private uploadRawContent(file): Observable { - return this.processCloudContentService.createTemporaryRawRelatedContent(file, this.field.form.nodeId, this.field.form.contentHost) - .pipe( - map((response: any) => { - this.logService.info(response); - return { - nodeId: response.id, - name: response.name, - content: response.content, - createdAt: response.createdAt - }; - }), - catchError((err) => this.handleError(err)) - ); - } - - private handleError(error: any): any { - return this.logService.error(error || 'Server error'); + private uploadRawContent(file: File): Observable { + return this.processCloudContentService.createTemporaryRawRelatedContent( + file, this.field.form.nodeId, this.field.form.contentHost + ); } getMultipleFileParam() { @@ -121,17 +127,17 @@ export class UploadCloudWidgetComponent extends WidgetComponent implements OnIni } } - private removeElementFromList(file) { - const savedValues = this.field.form.values[this.field.id] - ? this.field.form.values[this.field.id] : this.field.value; - const index = savedValues.indexOf(file); - if (index !== -1) { - const filteredValues = savedValues.filter((value: any) => value.nodeId !== file.nodeId); - this.resetFormValues(filteredValues); - } + get uploadedFiles(): Node[] { + const result = this.field.value || this.field.form.values[this.field.id]; + return result || []; } - private resetFormValues(values) { + private removeElementFromList(file: Node) { + const filteredValues = this.uploadedFiles.filter(value => value.id !== file.id); + this.resetFormValues(filteredValues); + } + + private resetFormValues(values: Node[]) { if (values && values.length > 0) { this.field.value = values; this.field.form.values[this.field.id] = values; diff --git a/lib/process-services-cloud/src/lib/form/services/process-cloud-content.service.ts b/lib/process-services-cloud/src/lib/form/services/process-cloud-content.service.ts index c54a2f2df9..dc0ef92402 100644 --- a/lib/process-services-cloud/src/lib/form/services/process-cloud-content.service.ts +++ b/lib/process-services-cloud/src/lib/form/services/process-cloud-content.service.ts @@ -18,47 +18,64 @@ import { Injectable } from '@angular/core'; import { throwError, Observable, from } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; -import { AlfrescoApiService, LogService, ContentService } from '@alfresco/adf-core'; +import { + AlfrescoApiService, + LogService, + ContentService +} from '@alfresco/adf-core'; +import { Node } from '@alfresco/js-api'; @Injectable({ - providedIn: 'root' + providedIn: 'root' }) export class ProcessCloudContentService { + constructor( + private apiService: AlfrescoApiService, + private logService: LogService, + public contentService: ContentService + ) {} - constructor( - private apiService: AlfrescoApiService, - private logService: LogService, - public contentService: ContentService - ) { } + createTemporaryRawRelatedContent( + file: File, + nodeId: string, + contentHost: string + ): Observable { + const changedConfig = this.apiService.lastConfig; - createTemporaryRawRelatedContent(file, nodeId, contentHost): Observable { - const changedConfig = this.apiService.lastConfig; - changedConfig.provider = 'ALL'; - changedConfig.hostEcm = contentHost.replace('/alfresco', ''); - this.apiService.getInstance().setConfig(changedConfig); - return from(this.apiService.getInstance().upload.uploadFile( - file, '', nodeId, '', { overwrite: true })).pipe( - map((res: any) => { - return (res.entry); - }), - catchError((err) => this.handleError(err)) - ); - } + changedConfig.provider = 'ALL'; + changedConfig.hostEcm = contentHost.replace('/alfresco', ''); - getRawContentNode(nodeId: string, contentHost: string): Observable { - const changedConfig = this.apiService.lastConfig; - changedConfig.provider = 'ALL'; - changedConfig.hostEcm = contentHost.replace('/alfresco', ''); - this.apiService.getInstance().setConfig(changedConfig); - return this.contentService.getNodeContent(nodeId); - } + this.apiService.getInstance().setConfig(changedConfig); - downloadNodeContent(blob: Blob, fileName: string): void { - this.contentService.downloadBlob(blob, fileName); - } + return from( + this.apiService + .getInstance() + .upload.uploadFile(file, '', nodeId, '', { overwrite: true }) + ).pipe( + map((res: any) => { + return { + ...res.entry, + nodeId: res.entry.id + }; + }), + catchError(err => this.handleError(err)) + ); + } - private handleError(error: any) { - this.logService.error(error); - return throwError(error || 'Server error'); - } + getRawContentNode(nodeId: string, contentHost: string): Observable { + const changedConfig = this.apiService.lastConfig; + changedConfig.provider = 'ALL'; + changedConfig.hostEcm = contentHost.replace('/alfresco', ''); + this.apiService.getInstance().setConfig(changedConfig); + return this.contentService.getNodeContent(nodeId); + } + + downloadNodeContent(blob: Blob, fileName: string): void { + this.contentService.downloadBlob(blob, fileName); + } + + private handleError(error: any) { + this.logService.error(error); + return throwError(error || 'Server error'); + } }