mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-788] Support for max file size constraints in Upload button component (#2546)
* add maxFile size in upload Button * test button file size in demo shell
This commit is contained in:
committed by
Popovics András
parent
3aa2724d25
commit
1cfe43e5d9
@@ -118,7 +118,7 @@ describe('UploadButtonComponent', () => {
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
component.permissionEvent.subscribe( permission => {
|
||||
component.permissionEvent.subscribe(permission => {
|
||||
expect(permission).toBeDefined();
|
||||
expect(permission.type).toEqual('content');
|
||||
expect(permission.action).toEqual('upload');
|
||||
@@ -148,7 +148,7 @@ describe('UploadButtonComponent', () => {
|
||||
|
||||
spyOn(component, 'getFolderNode').and.returnValue(Observable.of(fakeFolderNodeWithPermission));
|
||||
|
||||
component.ngOnChanges({ rootFolderId: new SimpleChange(null, component.rootFolderId, true) });
|
||||
component.ngOnChanges({rootFolderId: new SimpleChange(null, component.rootFolderId, true)});
|
||||
component.onFilesAdded(fakeEvent);
|
||||
let compiled = fixture.debugElement.nativeElement;
|
||||
fixture.detectChanges();
|
||||
@@ -162,7 +162,7 @@ describe('UploadButtonComponent', () => {
|
||||
|
||||
spyOn(component, 'getFolderNode').and.returnValue(Observable.of(fakeFolderNodeWithPermission));
|
||||
|
||||
component.ngOnChanges({ rootFolderId: new SimpleChange(null, component.rootFolderId, true) });
|
||||
component.ngOnChanges({rootFolderId: new SimpleChange(null, component.rootFolderId, true)});
|
||||
component.onFilesAdded(fakeEvent);
|
||||
let compiled = fixture.debugElement.nativeElement;
|
||||
fixture.detectChanges();
|
||||
@@ -177,7 +177,7 @@ describe('UploadButtonComponent', () => {
|
||||
|
||||
spyOn(component, 'getFolderNode').and.returnValue(Observable.of(fakeFolderNodeWithPermission));
|
||||
|
||||
component.ngOnChanges({ rootFolderId: new SimpleChange(null, component.rootFolderId, true) });
|
||||
component.ngOnChanges({rootFolderId: new SimpleChange(null, component.rootFolderId, true)});
|
||||
uploadService.uploadFilesInTheQueue = jasmine.createSpy('uploadFilesInTheQueue');
|
||||
|
||||
fixture.detectChanges();
|
||||
@@ -192,7 +192,7 @@ describe('UploadButtonComponent', () => {
|
||||
component.success = null;
|
||||
|
||||
spyOn(component, 'getFolderNode').and.returnValue(Observable.of(fakeFolderNodeWithPermission));
|
||||
component.ngOnChanges({ rootFolderId: new SimpleChange(null, component.rootFolderId, true) });
|
||||
component.ngOnChanges({rootFolderId: new SimpleChange(null, component.rootFolderId, true)});
|
||||
|
||||
uploadService.uploadFilesInTheQueue = jasmine.createSpy('uploadFilesInTheQueue');
|
||||
|
||||
@@ -209,7 +209,7 @@ describe('UploadButtonComponent', () => {
|
||||
spyOn(contentService, 'createFolder').and.returnValue(Observable.of(true));
|
||||
spyOn(component, 'getFolderNode').and.returnValue(Observable.of(fakeFolderNodeWithPermission));
|
||||
|
||||
component.ngOnChanges({ rootFolderId: new SimpleChange(null, component.rootFolderId, true) });
|
||||
component.ngOnChanges({rootFolderId: new SimpleChange(null, component.rootFolderId, true)});
|
||||
fixture.detectChanges();
|
||||
|
||||
component.success.subscribe(e => {
|
||||
@@ -260,12 +260,45 @@ describe('UploadButtonComponent', () => {
|
||||
expect(compiled.querySelector('#uploadFolder-label-static').textContent).toEqual('test-text');
|
||||
});
|
||||
|
||||
describe('filesize', () => {
|
||||
|
||||
const files: File[] = [
|
||||
<File> {name: 'bigFile.png', size: 1000},
|
||||
<File> {name: 'smallFile.png', size: 10}
|
||||
];
|
||||
|
||||
let addToQueueSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
addToQueueSpy = spyOn(uploadService, 'addToQueue');
|
||||
});
|
||||
|
||||
it('should filter out file, which are too big if max file size is set', () => {
|
||||
component.maxFilesSize = 100;
|
||||
|
||||
component.uploadFiles(files);
|
||||
|
||||
const filesCalledWith = addToQueueSpy.calls.mostRecent().args;
|
||||
expect(filesCalledWith.length).toBe(1);
|
||||
expect(filesCalledWith[0].name).toBe('smallFile.png');
|
||||
});
|
||||
|
||||
it('should not filter out files if max file size is not set', () => {
|
||||
component.maxFilesSize = null;
|
||||
|
||||
component.uploadFiles(files);
|
||||
|
||||
const filesCalledWith = addToQueueSpy.calls.mostRecent().args;
|
||||
expect(filesCalledWith.length).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('uploadFiles', () => {
|
||||
|
||||
const files: File[] = [
|
||||
<File> { name: 'phobos.jpg' },
|
||||
<File> { name: 'deimos.png' },
|
||||
<File> { name: 'ganymede.bmp' }
|
||||
<File> {name: 'phobos.jpg'},
|
||||
<File> {name: 'deimos.png'},
|
||||
<File> {name: 'ganymede.bmp'}
|
||||
];
|
||||
|
||||
let addToQueueSpy;
|
||||
|
@@ -15,7 +15,17 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Component, EventEmitter, forwardRef, Input, OnChanges, OnInit, Output, SimpleChanges, ViewEncapsulation } from '@angular/core';
|
||||
import {
|
||||
Component,
|
||||
EventEmitter,
|
||||
forwardRef,
|
||||
Input,
|
||||
OnChanges,
|
||||
OnInit,
|
||||
Output,
|
||||
SimpleChanges,
|
||||
ViewEncapsulation
|
||||
} from '@angular/core';
|
||||
import { MinimalNodeEntryEntity } from 'alfresco-js-api';
|
||||
import {
|
||||
AlfrescoApiService,
|
||||
@@ -36,7 +46,7 @@ import { PermissionModel } from '../models/permissions.model';
|
||||
templateUrl: './upload-button.component.html',
|
||||
styleUrls: ['./upload-button.component.scss'],
|
||||
providers: [
|
||||
{ provide: EXTENDIBLE_COMPONENT, useExisting: forwardRef(() => UploadButtonComponent)}
|
||||
{provide: EXTENDIBLE_COMPONENT, useExisting: forwardRef(() => UploadButtonComponent)}
|
||||
],
|
||||
encapsulation: ViewEncapsulation.None
|
||||
})
|
||||
@@ -69,6 +79,9 @@ export class UploadButtonComponent implements OnInit, OnChanges, NodePermissionS
|
||||
@Input()
|
||||
acceptedFilesType: string = '*';
|
||||
|
||||
@Input()
|
||||
maxFilesSize: number;
|
||||
|
||||
@Input()
|
||||
staticTitle: string;
|
||||
|
||||
@@ -158,7 +171,8 @@ export class UploadButtonComponent implements OnInit, OnChanges, NodePermissionS
|
||||
uploadFiles(files: File[]): void {
|
||||
const latestFilesAdded: FileModel[] = files
|
||||
.map<FileModel>(this.createFileModel.bind(this))
|
||||
.filter(this.isFileAcceptable.bind(this));
|
||||
.filter(this.isFileAcceptable.bind(this))
|
||||
.filter(this.isFileSizeAcceptable.bind(this));
|
||||
|
||||
if (latestFilesAdded.length > 0) {
|
||||
this.uploadService.addToQueue(...latestFilesAdded);
|
||||
@@ -203,6 +217,15 @@ export class UploadButtonComponent implements OnInit, OnChanges, NodePermissionS
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given file is an acceptable size
|
||||
*
|
||||
* @param file FileModel
|
||||
*/
|
||||
private isFileSizeAcceptable(file: FileModel): boolean {
|
||||
return this.maxFilesSize ? file.size <= this.maxFilesSize : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show undo notification bar.
|
||||
*
|
||||
|
Reference in New Issue
Block a user