Skip to content

Commit

Permalink
[ADF-1653] [ADF-1651] - disabled download for external source and add…
Browse files Browse the repository at this point in the history
…ed download for same login content (#5450)

* ADF-1653 - disabled download for external content

* ADF-1653 - ADF-1651 - disabled download for external source and added download for same login content

* ADF-1653 - using downloadService instead of contentService

* improve solution

* improve solution

* fix unit

* add more unti test

* fix lint

Co-authored-by: Eugenio Romano <[email protected]>
  • Loading branch information
VitoAlbano and eromano authored Feb 6, 2020
1 parent 7d04297 commit 88e961d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@
</button>
<mat-menu #fileActionMenu="matMenu" xPosition="before">
<button id="{{'file-'+file.id+'-show-file'}}"
[disabled]="file.isExternal"
[disabled]="file.isExternal || !file.contentAvailable"
mat-menu-item (click)="onAttachFileClicked(file)">
<mat-icon>image</mat-icon>
<span>{{ 'FORM.FIELD.SHOW_FILE' | translate }}</span>
</button>
<button id="{{'file-'+file.id+'-download-file'}}"
[disabled]="file.isExternal"
mat-menu-item (click)="downloadContent(file)">
<mat-icon>file_download</mat-icon>
<span>{{ 'FORM.FIELD.DOWNLOAD_FILE' | translate }}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import {
ThumbnailService,
ProcessContentService,
ActivitiContentService,
ContentService,
AppConfigValues,
AppConfigService
AppConfigService,
DownloadService,
ContentService
} from '@alfresco/adf-core';
import { ContentNodeDialogService } from '@alfresco/adf-content-services';
import { Node, RelatedContentRepresentation } from '@alfresco/js-api';
import { Node, RelatedContentRepresentation, NodeChildAssociation } from '@alfresco/js-api';
import { from, zip, of, Subject } from 'rxjs';
import { mergeMap, takeUntil } from 'rxjs/operators';
import { AttachFileWidgetDialogService } from './attach-file-widget-dialog.service';
Expand Down Expand Up @@ -66,6 +67,7 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements
private contentService: ContentService,
private contentDialog: ContentNodeDialogService,
private appConfigService: AppConfigService,
private downloadService: DownloadService,
private attachDialogService: AttachFileWidgetDialogService) {
super(formService, logger, thumbnails, processContentService);
}
Expand Down Expand Up @@ -131,6 +133,10 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements
return this.tempFilesList.findIndex((elem) => elem.name === file.name) >= 0;
}

getNodeFromTempFile(file): NodeChildAssociation {
return this.tempFilesList.find((elem) => elem.name === file.name);
}

openSelectDialogFromFileSource() {
const params = this.field.params;
if (this.isDefinedSourceFolder()) {
Expand All @@ -157,7 +163,7 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements
}

onAttachFileClicked(file: any) {
if (file.isExternal) {
if (file.isExternal || !file.contentAvailable) {
this.logger.info(`The file ${file.name} comes from an external source and cannot be showed at this moment`);
return;
}
Expand All @@ -170,11 +176,22 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements

downloadContent(file: any | RelatedContentRepresentation): void {
if (this.isTemporaryFile(file)) {
this.contentService.downloadBlob((<RelatedContentRepresentation> file).contentBlob, file.name);
const fileBlob = (<RelatedContentRepresentation> file).contentBlob;
if (fileBlob) {
this.downloadService.downloadBlob(fileBlob, file.name);
} else {
const nodeUploaded: NodeChildAssociation = this.getNodeFromTempFile(file);
const nodeUrl = this.contentService.getContentUrl(nodeUploaded.id);
this.downloadService.downloadUrl(nodeUrl, file.name);
}
}
if (file.sourceId) {
const nodeUrl = this.contentService.getContentUrl(file.sourceId);
this.downloadService.downloadUrl(nodeUrl, file.name);
} else {
this.processContentService.getFileRawContent((<any> file).id).subscribe(
(blob: Blob) => {
this.contentService.downloadBlob(blob, (<any> file).name);
this.downloadService.downloadBlob(blob, (<any> file).name);
},
() => {
this.logger.error('Impossible retrieve content for download');
Expand Down Expand Up @@ -225,8 +242,8 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements
},
() => {
const previousFiles = this.field.value ? this.field.value : [];
this.field.value = [ ...previousFiles, ...filesSaved ];
this.field.json.value = [ ...previousFiles, ...filesSaved ];
this.field.value = [...previousFiles, ...filesSaved];
this.field.json.value = [...previousFiles, ...filesSaved];
this.hasFile = true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
ProcessContentService,
ActivitiContentService,
FormFieldMetadata,
ContentService,
setupTestBed
setupTestBed,
DownloadService
} from '@alfresco/adf-core';
import { ContentNodeDialogService, ContentModule } from '@alfresco/adf-content-services';
import { of } from 'rxjs';
Expand All @@ -40,30 +40,30 @@ const fakeRepositoryListAnswer = [
'serviceId': 'alfresco-9999-SHAREME',
'metaDataAllowed': true,
'name': 'SHAREME',
'repositoryUrl' : 'http://localhost:0000/SHAREME'
'repositoryUrl': 'http://localhost:0000/SHAREME'
},
{
'authorized': true,
'serviceId': 'alfresco-0000-GOKUSHARE',
'metaDataAllowed': true,
'name': 'GOKUSHARE',
'repositoryUrl' : 'http://localhost:0000/GOKUSHARE'
'repositoryUrl': 'http://localhost:0000/GOKUSHARE'
}];

const onlyLocalParams = {
fileSource : {
fileSource: {
serviceId: 'local-file'
}
};

const allSourceParams = {
fileSource : {
fileSource: {
serviceId: 'all-file-sources'
}
};

const definedSourceParams = {
fileSource : {
fileSource: {
serviceId: 'goku-sources',
name: 'pippo-baudo',
selectedFolder: {
Expand All @@ -84,10 +84,11 @@ const fakePngUpload = {
'id': 1166,
'name': 'fake-png.png',
'created': '2017-07-25T17:17:37.099Z',
'createdBy': {'id': 1001, 'firstName': 'Admin', 'lastName': 'admin', 'email': 'admin'},
'createdBy': { 'id': 1001, 'firstName': 'Admin', 'lastName': 'admin', 'email': 'admin' },
'relatedContent': false,
'contentAvailable': true,
'link': false,
'isExternal': false,
'mimeType': 'image/png',
'simpleType': 'image',
'previewStatus': 'queued',
Expand All @@ -98,9 +99,10 @@ const fakePngAnswer = {
'id': 1155,
'name': 'a_png_file.png',
'created': '2017-07-25T17:17:37.099Z',
'createdBy': {'id': 1001, 'firstName': 'Admin', 'lastName': 'admin', 'email': 'admin'},
'createdBy': { 'id': 1001, 'firstName': 'Admin', 'lastName': 'admin', 'email': 'admin' },
'relatedContent': false,
'contentAvailable': true,
'isExternal': false,
'link': false,
'mimeType': 'image/png',
'simpleType': 'image',
Expand All @@ -116,7 +118,7 @@ describe('AttachFileWidgetComponent', () => {
let activitiContentService: ActivitiContentService;
let contentNodeDialogService: ContentNodeDialogService;
let processContentService: ProcessContentService;
let contentService: ContentService;
let downloadService: DownloadService;
let formService: FormService;

setupTestBed({
Expand All @@ -133,7 +135,7 @@ describe('AttachFileWidgetComponent', () => {
activitiContentService = TestBed.get(ActivitiContentService);
contentNodeDialogService = TestBed.get(ContentNodeDialogService);
processContentService = TestBed.get(ProcessContentService);
contentService = TestBed.get(ContentService);
downloadService = TestBed.get(DownloadService);
formService = TestBed.get(FormService);
}));

Expand Down Expand Up @@ -310,7 +312,7 @@ describe('AttachFileWidgetComponent', () => {
fixture.detectChanges();
fixture.whenStable().then(() => {
const inputDebugElement = fixture.debugElement.query(By.css('#attach-file-attach'));
inputDebugElement.triggerEventHandler('change', {target: {files: [fakePngAnswer]}});
inputDebugElement.triggerEventHandler('change', { target: { files: [fakePngAnswer] } });
fixture.detectChanges();
expect(element.querySelector('#file-1155-icon')).not.toBeNull();
});
Expand Down Expand Up @@ -342,15 +344,15 @@ describe('AttachFileWidgetComponent', () => {
}));

it('should download file when download is clicked', async(() => {
spyOn(contentService, 'downloadBlob').and.stub();
spyOn(downloadService, 'downloadBlob').and.stub();
const menuButton: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#file-1155-option-menu');
expect(menuButton).not.toBeNull();
menuButton.click();
fixture.detectChanges();
const downloadOption: HTMLButtonElement = <HTMLButtonElement> fixture.debugElement.query(By.css('#file-1155-download-file')).nativeElement;
downloadOption.click();
fixture.whenStable().then(() => {
expect(contentService.downloadBlob).toHaveBeenCalled();
expect(downloadService.downloadBlob).toHaveBeenCalled();
});
}));

Expand All @@ -368,6 +370,55 @@ describe('AttachFileWidgetComponent', () => {
showOption.click();
}));

it('should not display the show button file when is an external file', async(() => {
fakePngAnswer.isExternal = true;
spyOn(processContentService, 'getFileRawContent').and.returnValue(of(fakePngAnswer));

const menuButton: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#file-1155-option-menu');
expect(menuButton).not.toBeNull();
menuButton.click();
fixture.detectChanges();
const showOption: HTMLButtonElement = <HTMLButtonElement> fixture.debugElement.query(By.css('#file-1155-show-file')).nativeElement;
expect(showOption.disabled).toBeTruthy();
}));

it('should not display the download button file when is an external file', async(() => {
fakePngAnswer.isExternal = true;
spyOn(processContentService, 'getFileRawContent').and.returnValue(of(fakePngAnswer));

const menuButton: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#file-1155-option-menu');
expect(menuButton).not.toBeNull();
menuButton.click();
fixture.detectChanges();
const downloadOption: HTMLButtonElement = <HTMLButtonElement> fixture.debugElement.query(By.css('#file-1155-download-file')).nativeElement;
expect(downloadOption.disabled).toBeTruthy();
}));

it('should display the download button file when is an internal file', async(() => {
fakePngAnswer.isExternal = false;
spyOn(processContentService, 'getFileRawContent').and.returnValue(of(fakePngAnswer));

const menuButton: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#file-1155-option-menu');
expect(menuButton).not.toBeNull();
menuButton.click();
fixture.detectChanges();
const downloadOption: HTMLButtonElement = <HTMLButtonElement> fixture.debugElement.query(By.css('#file-1155-download-file')).nativeElement;
expect(downloadOption.disabled).toBeFalsy();

}));

it('should not display the show button file when there is no contentAvailable', async(() => {
fakePngAnswer.contentAvailable = false;
spyOn(processContentService, 'getFileRawContent').and.returnValue(of(fakePngAnswer));

const menuButton: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#file-1155-option-menu');
expect(menuButton).not.toBeNull();
menuButton.click();
fixture.detectChanges();
const showOption: HTMLButtonElement = <HTMLButtonElement> fixture.debugElement.query(By.css('#file-1155-show-file')).nativeElement;
expect(showOption.disabled).toBeTruthy();
}));

});

});

0 comments on commit 88e961d

Please sign in to comment.