Merge pull request #257 from Alfresco/dev-mvitale-252

Use new upload API #252
This commit is contained in:
Maurizio Vitale 2016-06-22 17:59:50 +01:00 committed by GitHub
commit cd20820c18
5 changed files with 42 additions and 77 deletions

View File

@ -87,8 +87,9 @@ describe('AlfrescoUploadButton', () => {
.createAsync(UploadButtonComponent) .createAsync(UploadButtonComponent)
.then((fixture) => { .then((fixture) => {
let component = fixture.componentInstance; let component = fixture.componentInstance;
component.uploaddirectory = 'folder-default'; component.currentFolderPath = '/root-fake-/sites-fake/folder-fake';
component.uploadFiles = jasmine.createSpy('uploadFiles'); component.onSuccess = null;
component._uploaderService.uploadFilesInTheQueue = jasmine.createSpy('uploadFilesInTheQueue');
fixture.detectChanges(); fixture.detectChanges();
let file = {name: 'fake-name-1', size: 10, webkitRelativePath: 'fake-folder1/fake-name-1.json'}; let file = {name: 'fake-name-1', size: 10, webkitRelativePath: 'fake-folder1/fake-name-1.json'};
@ -101,7 +102,7 @@ describe('AlfrescoUploadButton', () => {
}; };
component.onFilesAdded(fakeEvent); component.onFilesAdded(fakeEvent);
expect(component.uploadFiles).toHaveBeenCalledWith('folder-default', [file]); expect(component._uploaderService.uploadFilesInTheQueue).toHaveBeenCalledWith('/root-fake-/sites-fake/folder-fake', null);
}); });
})); }));

View File

@ -73,9 +73,6 @@ export class UploadButtonComponent {
@Input() @Input()
currentFolderPath: string = '/Sites/swsdp/documentLibrary'; currentFolderPath: string = '/Sites/swsdp/documentLibrary';
@Input()
uploaddirectory: string = '';
@Output() @Output()
onSuccess = new EventEmitter(); onSuccess = new EventEmitter();
@ -87,15 +84,8 @@ export class UploadButtonComponent {
translate: AlfrescoTranslationService) { translate: AlfrescoTranslationService) {
console.log('UploadComponent constructor', el); console.log('UploadComponent constructor', el);
let site = this.getSiteId(); let formFields = this.createFormFields();
let container = this.getContainerId(); this._uploaderService.setOptions(formFields);
this._uploaderService.setOptions({
formFields: {
siteid: site,
containerid: container
}
});
this.translate = translate; this.translate = translate;
this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload'); this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload');
} }
@ -108,7 +98,7 @@ export class UploadButtonComponent {
onFilesAdded($event: any): void { onFilesAdded($event: any): void {
let files = $event.currentTarget.files; let files = $event.currentTarget.files;
this.printFileInfo(files); this.printFileInfo(files);
this.uploadFiles(this.uploaddirectory, files); this.uploadFiles(this.currentFolderPath, files);
// reset the value of the input file // reset the value of the input file
$event.target.value = ''; $event.target.value = '';
} }
@ -130,19 +120,19 @@ export class UploadButtonComponent {
this._uploaderService.createFolder(absolutePath, directoryName) this._uploaderService.createFolder(absolutePath, directoryName)
.subscribe( .subscribe(
res => { res => {
let relativeDir = this.uploaddirectory + '/' + directoryPath; let relativeDir = this.currentFolderPath + '/' + directoryPath;
this.uploadFiles(relativeDir, filesDir); this.uploadFiles(relativeDir, filesDir);
}, },
error => { error => {
let errorMessagePlaceholder = this.getErrorMessage(error.response); let errorMessagePlaceholder = this.getErrorMessage(error.response);
if (errorMessagePlaceholder) { if (errorMessagePlaceholder) {
let errorMessage = this.formatString(errorMessagePlaceholder, [directoryName]); let errorMessage = this.formatString(errorMessagePlaceholder, [directoryName]);
if (errorMessage) { if (errorMessage) {
this._showErrorNotificationBar(errorMessage); this._showErrorNotificationBar(errorMessage);
}
} }
console.log(error);
} }
console.log(error);
}
); );
}); });
// reset the value of the input file // reset the value of the input file
@ -244,7 +234,7 @@ export class UploadButtonComponent {
* @returns {string} * @returns {string}
*/ */
private getErrorMessage(response: any): string { private getErrorMessage(response: any): string {
if (response.body && response.body.error.statusCode === ERROR_FOLDER_ALREADY_EXIST ) { if (response.body && response.body.error.statusCode === ERROR_FOLDER_ALREADY_EXIST) {
let errorMessage: any; let errorMessage: any;
errorMessage = this.translate.get('FILE_UPLOAD.MESSAGES.FOLDER_ALREADY_EXIST'); errorMessage = this.translate.get('FILE_UPLOAD.MESSAGES.FOLDER_ALREADY_EXIST');
return errorMessage.value; return errorMessage.value;
@ -269,22 +259,6 @@ export class UploadButtonComponent {
} }
} }
/**
* Return the site from the path
* @returns {any}
*/
private getSiteId(): string {
return this.currentFolderPath.replace('/Sites/', '').split('/')[0];
}
/**
* Return the container from the path
* @returns {any}
*/
private getContainerId(): string {
return this.currentFolderPath.replace('/Sites/', '').split('/')[1];
}
/** /**
* Prints the basic information of a file * Prints the basic information of a file
* @param files * @param files
@ -310,4 +284,12 @@ export class UploadButtonComponent {
} }
return message; return message;
} }
private createFormFields(): any {
return {
formFields: {
overwrite: true
}
};
}
} }

View File

@ -51,9 +51,6 @@ export class UploadDragAreaComponent {
@Input() @Input()
showUdoNotificationBar: boolean = true; showUdoNotificationBar: boolean = true;
@Input()
uploaddirectory: string = '';
@Input() @Input()
currentFolderPath: string = '/Sites/swsdp/documentLibrary'; currentFolderPath: string = '/Sites/swsdp/documentLibrary';
@ -65,15 +62,8 @@ export class UploadDragAreaComponent {
constructor(private _uploaderService: UploadService, constructor(private _uploaderService: UploadService,
translate: AlfrescoTranslationService) { translate: AlfrescoTranslationService) {
let site = this.getSiteId(); let formFields = this.createFormFields();
let container = this.getContainerId(); this._uploaderService.setOptions(formFields);
this._uploaderService.setOptions({
formFields: {
siteid: site,
containerid: container
}
});
this.translate = translate; this.translate = translate;
this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload'); this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload');
@ -87,7 +77,7 @@ export class UploadDragAreaComponent {
onFilesDropped(files: File[]): void { onFilesDropped(files: File[]): void {
if (files.length) { if (files.length) {
this._uploaderService.addToQueue(files); this._uploaderService.addToQueue(files);
this._uploaderService.uploadFilesInTheQueue(this.uploaddirectory, this.onSuccess); this._uploaderService.uploadFilesInTheQueue(this.currentFolderPath, this.onSuccess);
let latestFilesAdded = this._uploaderService.getQueue(); let latestFilesAdded = this._uploaderService.getQueue();
if (this.showUdoNotificationBar) { if (this.showUdoNotificationBar) {
this._showUndoNotificationBar(latestFilesAdded); this._showUndoNotificationBar(latestFilesAdded);
@ -104,7 +94,7 @@ export class UploadDragAreaComponent {
item.file(function (file: any) { item.file(function (file: any) {
self._uploaderService.addToQueue([file]); self._uploaderService.addToQueue([file]);
let path = item.fullPath.replace(item.name, ''); let path = item.fullPath.replace(item.name, '');
let filePath = self.uploaddirectory + path; let filePath = self.currentFolderPath + path;
self._uploaderService.uploadFilesInTheQueue(filePath, self.onSuccess); self._uploaderService.uploadFilesInTheQueue(filePath, self.onSuccess);
let latestFilesAdded = self._uploaderService.getQueue(); let latestFilesAdded = self._uploaderService.getQueue();
if (self.showUdoNotificationBar) { if (self.showUdoNotificationBar) {
@ -162,22 +152,6 @@ export class UploadDragAreaComponent {
} }
} }
/**
* Return the site from the path
* @returns {string}
*/
private getSiteId(): string {
return this.currentFolderPath.replace('/Sites/', '').split('/')[0];
}
/**
* Return the container from the path
* @returns {string}
*/
private getContainerId(): string {
return this.currentFolderPath.replace('/Sites/', '').split('/')[1];
}
/** /**
* Show undo notification bar. * Show undo notification bar.
* *
@ -246,4 +220,12 @@ export class UploadDragAreaComponent {
} }
return message; return message;
} }
private createFormFields(): any {
return {
formFields: {
overwrite: true
}
};
}
} }

View File

@ -83,7 +83,7 @@ describe('AlfrescoUploadService', () => {
it('should show the default option if no method setOption is called', () => { it('should show the default option if no method setOption is called', () => {
let empty = {}; let empty = {};
service.setOptions(empty); service.setOptions(empty);
expect(service.getUrl()).toEqual('/alfresco/service/api/upload'); expect(service.getUrl()).toEqual('/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children');
let formFields: Object = {}; let formFields: Object = {};
expect(service.getFormFileds()).toEqual(formFields); expect(service.getFormFileds()).toEqual(formFields);
}); });

View File

@ -33,7 +33,7 @@ declare let AlfrescoApi: any;
*/ */
@Injectable() @Injectable()
export class UploadService { export class UploadService {
private _url: string = '/alfresco/service/api/upload'; private _url: string = '/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children';
private _method: string = 'POST'; private _method: string = 'POST';
private _fieldName: string = 'filedata'; private _fieldName: string = 'filedata';
@ -206,10 +206,10 @@ export class UploadService {
let form = new FormData(); let form = new FormData();
form.append(this._fieldName, uploadingFileModel.file, uploadingFileModel.name); form.append(this._fieldName, uploadingFileModel.file, uploadingFileModel.name);
Object.keys(this._formFields).forEach((key: any) => { Object.keys(this._formFields).forEach((key: any) => {
form.append(key, this._formFields[key]); form.append(key, this._formFields[key]);
}); });
form.append('uploaddirectory', directory); form.append('relativePath', directory);
let xmlHttpRequest = this.createXMLHttpRequestInstance(uploadingFileModel, elementEmit); let xmlHttpRequest = this.createXMLHttpRequestInstance(uploadingFileModel, elementEmit);
uploadingFileModel._xmlHttpRequest = xmlHttpRequest; uploadingFileModel._xmlHttpRequest = xmlHttpRequest;