add autorename as default behaviour and the possibility to use the versioning

This commit is contained in:
Eugenio Romano 2016-08-24 16:21:31 +01:00
parent e00896da4e
commit 23da361622
7 changed files with 118 additions and 47 deletions

View File

@ -3,6 +3,7 @@
[showUploadDialog]="true"
[currentFolderPath]="currentPath"
[uploaddirectory]=""
[versioning] = "versioning"
(onSuccess)="documentList.reload()">
<alfresco-document-list-breadcrumb
[currentFolderPath]="currentPath"
@ -147,6 +148,13 @@
</label>
</p>
<p style="width:250px;margin: 20px;">
<label for="switch-versioning" class="mdl-switch mdl-js-switch mdl-js-ripple-effect">
<input type="checkbox" id="switch-versioning" class="mdl-switch__input" (change)="toggleVersioning()">
<span class="mdl-switch__label">Versioning</span>
</label>
</p>
<h5>Upload</h5>
<br>
<div *ngIf="acceptedFilesTypeShow">
@ -160,6 +168,7 @@
[currentFolderPath]="currentPath"
[multipleFiles]="multipleFileUpload"
[uploadFolders]="folderUpload"
[versioning] = "versioning"
(onSuccess)="documentList.reload()">
<div class="mdl-spinner mdl-js-spinner is-active"></div>
</alfresco-upload-button>
@ -171,6 +180,7 @@
acceptedFilesType="{{acceptedFilesType}}"
[multipleFiles]="multipleFileUpload"
[uploadFolders]="folderUpload"
[versioning] = "versioning"
(onSuccess)="documentList.reload()">
<div class="mdl-spinner mdl-js-spinner is-active"></div>
</alfresco-upload-button>

View File

@ -63,6 +63,7 @@ export class FilesComponent implements OnInit {
multipleFileUpload: boolean = false;
folderUpload: boolean = false;
acceptedFilesTypeShow: boolean = false;
versioning: boolean = false;
acceptedFilesType: string = '.jpg,.pdf,.js';
@ -119,6 +120,11 @@ export class FilesComponent implements OnInit {
return this.acceptedFilesTypeShow;
}
toggleVersioning(){
this.versioning = !this.versioning;
return this.versioning;
}
ngOnInit() {
this.formService.getProcessDefinitions().subscribe(
defs => this.setupBpmActions(defs || []),

View File

@ -137,14 +137,21 @@ bootstrap(MyDemoApp, [
```
#### Events
**onSuccess**: The event is emitted when the file is uploaded<br />
Attribute | Description
--- | ---
`onSuccess` | The event is emitted when the file is uploaded
#### Options
**showUdoNotificationBar**: {boolean} (optional) default true. Hide/show notification bar.<br />
**uploadFolders**: {boolean} (optional) default false. Allow/disallow upload folders (only for chrome).<br />
**multipleFiles**: {boolean} (optional) default false. Allow/disallow multiple files.<br />
**acceptedFilesType**: {string} (optional) default "*". array of allowed file extensions , example: ".jpg,.gif,.png,.svg" .<br />
Attribute | Options | Default | Description | Mandatory
--- | --- | --- | --- | ---
`showUdoNotificationBar` | *boolean* | true | Hide/show notification bar |
`uploadFolders` | *boolean* | false | Allow/disallow upload folders (only for chrome) |
`multipleFiles` | *boolean* | false | Allow/disallow multiple files |
`acceptedFilesType` | *string* | * | array of allowed file extensions , example: ".jpg,.gif,.png,.svg" |
`versioning` | *boolean* | false | Versioning false is the default uploader behaviour and it rename using an integer suffix if there is a name clash. Versioning true to indicate that a major version should be created |
### Drag and drop
This component, provide a drag and drop are to upload files to alfresco.
@ -195,8 +202,11 @@ bootstrap(MyDemoApp, [
]);
```
#### Events
**onSuccess**: The event is emitted when the file is uploaded<br />
Attribute | Description
--- | ---
`onSuccess` | The event is emitted when the file is uploaded
### Files Dialog
This component provides a dialog that shows all the files uploaded

View File

@ -38,10 +38,11 @@ const ERROR_FOLDER_ALREADY_EXIST = 409;
* This component, provide a set of buttons to upload files to alfresco.
*
* @InputParam {boolean} [true] showUdoNotificationBar - hide/show notification bar.
* @InputParam {boolean} [false] versioning - true to indicate that a major version should be created
* @InputParam {boolean} [false] uploadFolders - allow/disallow upload folders (only for chrome).
* @InputParam {boolean} [false] multipleFiles - allow/disallow multiple files.
* @InputParam {string} [*] acceptedFilesType - array of allowed file extensions.
*
* @InputParam {boolean} [false] versioning - true to indicate that a major version should be created
* @Output - onSuccess - The event is emitted when the file is uploaded
*
* @returns {UploadDragAreaComponent} .
@ -67,6 +68,9 @@ export class UploadButtonComponent {
@Input()
multipleFiles: boolean = false;
@Input()
versioning: boolean = false;
@Input()
acceptedFilesType: string = '*';
@ -85,16 +89,16 @@ export class UploadButtonComponent {
translate: AlfrescoTranslationService;
constructor(public el: ElementRef,
private _uploaderService: UploadService,
translate: AlfrescoTranslationService) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields);
constructor(public el: ElementRef, private _uploaderService: UploadService, translate: AlfrescoTranslationService) {
this.translate = translate;
this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src');
}
ngOnChanges(changes) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields, this.versioning);
}
/**
* Method called when files are dropped in the drag area.
*
@ -123,21 +127,21 @@ export class UploadButtonComponent {
this._uploaderService.createFolder(absolutePath, directoryName)
.subscribe(
res => {
let relativeDir = this.currentFolderPath + '/' + directoryPath;
this.uploadFiles(relativeDir, filesDir);
},
let relativeDir = this.currentFolderPath + '/' + directoryPath;
this.uploadFiles(relativeDir, filesDir);
},
error => {
let errorMessagePlaceholder = this.getErrorMessage(error.response);
if (errorMessagePlaceholder) {
this.onError.emit({value: errorMessagePlaceholder});
let errorMessage = this.formatString(errorMessagePlaceholder, [directoryName]);
if (errorMessage) {
this._showErrorNotificationBar(errorMessage);
let errorMessagePlaceholder = this.getErrorMessage(error.response);
if (errorMessagePlaceholder) {
this.onError.emit({value: errorMessagePlaceholder});
let errorMessage = this.formatString(errorMessagePlaceholder, [directoryName]);
if (errorMessage) {
this._showErrorNotificationBar(errorMessage);
}
}
console.log(error);
}
console.log(error);
}
);
);
});
// reset the value of the input file
$event.target.value = '';

View File

@ -51,6 +51,9 @@ export class UploadDragAreaComponent {
@Input()
showUdoNotificationBar: boolean = true;
@Input()
versioning: boolean = false;
@Input()
currentFolderPath: string = '/Sites/swsdp/documentLibrary';
@ -59,16 +62,16 @@ export class UploadDragAreaComponent {
translate: AlfrescoTranslationService;
constructor(private _uploaderService: UploadService,
translate: AlfrescoTranslationService) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields);
constructor(private _uploaderService: UploadService, translate: AlfrescoTranslationService) {
this.translate = translate;
this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src');
}
ngOnChanges(changes) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields, this.versioning);
}
/**
* Method called when files are dropped in the drag area.
*
@ -227,7 +230,7 @@ export class UploadDragAreaComponent {
* @returns {string}
*/
private getErrorMessage(response: any): string {
if (response.body.error.statusCode === ERROR_FOLDER_ALREADY_EXIST ) {
if (response.body.error.statusCode === ERROR_FOLDER_ALREADY_EXIST) {
let errorMessage: any;
errorMessage = this.translate.get('FILE_UPLOAD.MESSAGES.FOLDER_ALREADY_EXIST');
return errorMessage.value;

View File

@ -44,7 +44,7 @@ describe('AlfrescoUploadService', () => {
];
});
beforeEach( inject([UploadService], (uploadService: UploadService) => {
beforeEach(inject([UploadService], (uploadService: UploadService) => {
jasmine.Ajax.install();
service = uploadService;
}));
@ -85,7 +85,7 @@ describe('AlfrescoUploadService', () => {
service.uploadFilesInTheQueue('fake-dir', emitter);
let request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children');
expect(request.url).toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true');
expect(request.method).toBe('POST');
jasmine.Ajax.requests.mostRecent().respondWith({
@ -107,7 +107,8 @@ describe('AlfrescoUploadService', () => {
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', emitter);
expect(jasmine.Ajax.requests.mostRecent().url)
.toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children');
.toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true');
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 404,
contentType: 'text/plain',
@ -156,7 +157,7 @@ describe('AlfrescoUploadService', () => {
};
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.filesUpload$.subscribe( (file) => {
service.filesUpload$.subscribe((file) => {
expect(file).toBeDefined();
expect(file[0]).toBeDefined();
expect(file[0].progress).toEqual(fakeProgress);
@ -169,7 +170,7 @@ describe('AlfrescoUploadService', () => {
file[0].emitProgres(fakeProgress);
});
it('should make XHR done request after the folder is created', (done) => {
it('should make XHR done request after the folder is created', (done) => {
let fakeRest = {
entry: {
isFile: false,
@ -190,7 +191,7 @@ describe('AlfrescoUploadService', () => {
});
});
it('should throws an exception when a folder already exist', (done) => {
it('should throws an exception when a folder already exist', (done) => {
let fakeRest = {
response: {
body: {
@ -208,13 +209,37 @@ describe('AlfrescoUploadService', () => {
let defaultPath = '';
let folderName = 'folder-duplicate-fake';
service.createFolder(defaultPath, folderName).subscribe(
res => {
res => {
},
error => {
error => {
expect(error).toEqual(fakeRest);
done();
}
);
});
it('Default upload behaviour should be autorename', () => {
let emitter = new EventEmitter();
service.setOptions(options);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', emitter);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('autoRename=true')).toBe(true);
});
it('If versioning is true autoRename should not be present and majorVersion should be a param', () => {
let emitter = new EventEmitter();
let enableVersioning = true;
service.setOptions(options, enableVersioning);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', emitter);
console.log(jasmine.Ajax.requests.mostRecent().url);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('autoRename=true')).toBe(false);
expect(jasmine.Ajax.requests.mostRecent().params.has('majorVersion')).toBe(true);
});
});

View File

@ -19,7 +19,7 @@ import { EventEmitter, Injectable } from '@angular/core';
import { Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { AlfrescoAuthenticationService} from 'ng2-alfresco-core';
import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { FileModel } from '../models/file.model';
/**
@ -34,6 +34,8 @@ export class UploadService {
private formFields: Object = {};
private queue: FileModel[] = [];
private versioning: boolean = false;
private filesUploadObserverProgressBar: Observer<FileModel[]>;
private totalCompletedObserver: Observer<number>;
@ -43,18 +45,20 @@ export class UploadService {
totalCompleted$: Observable<any>;
constructor(private authService: AlfrescoAuthenticationService) {
this.filesUpload$ = new Observable<FileModel[]>(observer => this.filesUploadObserverProgressBar = observer).share();
this.totalCompleted$ = new Observable<number>(observer => this.totalCompletedObserver = observer).share();
this.filesUpload$ = new Observable<FileModel[]>(observer => this.filesUploadObserverProgressBar = observer).share();
this.totalCompleted$ = new Observable<number>(observer => this.totalCompletedObserver = observer).share();
}
/**
* Configure the service
*
* @param {Object} - options to init the object
* @param {Object} - options formFields to init the object
* @param {boolean} - versioning true to indicate that a major version should be created
*
*/
public setOptions(options: any): void {
public setOptions(options: any, versioning: boolean): void {
this.formFields = options.formFields != null ? options.formFields : this.formFields;
this.versioning = versioning != null ? versioning : this.versioning;
}
/**
@ -88,11 +92,20 @@ export class UploadService {
return !uploadingFileModel.uploading && !uploadingFileModel.done && !uploadingFileModel.abort && !uploadingFileModel.error;
});
let opts: any = {};
opts.renditions = 'doclib';
if (this.versioning) {
opts.overwrite = true;
opts.majorVersion = true;
} else {
opts.autoRename = true;
}
filesToUpload.forEach((uploadingFileModel: FileModel) => {
uploadingFileModel.setUploading();
let promiseUpload = this.authService.getAlfrescoApi().
upload.uploadFile(uploadingFileModel.file, directory, null, null, {renditions: 'doclib'})
let promiseUpload = this.authService.getAlfrescoApi().upload.uploadFile(uploadingFileModel.file, directory, null, null, opts)
.on('progress', (progress: any) => {
uploadingFileModel.setProgres(progress);
this.updateFileListStream(this.queue);