From dc14a267863251fd154a3aeafb9fe22f2273605b Mon Sep 17 00:00:00 2001 From: mauriziovitale84 Date: Fri, 27 May 2016 10:42:29 +0100 Subject: [PATCH] #102 Fixed folder creation structure on folder upload button --- .../components/upload-button.component.html | 2 +- .../src/components/upload-button.component.ts | 95 ++++++++++++++++++- .../src/services/upload.service.ts | 3 +- 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.html b/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.html index 0470f3bcc0..6cbd731776 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.html +++ b/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.html @@ -28,7 +28,7 @@ file_upload { + let directoryName = this.getDirectoryName(directoryPath); + let absolutePath = this.currentFolderPath + this.getDirectoryPath(directoryPath); + + this._uploaderService.createFolder(absolutePath, directoryName) + .subscribe( + res => { + let relativeDir = this.uploaddirectory + '/' + directoryPath; + this.uploadFiles(relativeDir, filesDir); + }, + error => { + console.log(error); + } + ); + }); + } + + /** + * Upload a list of file in the specified path + * @param path + * @param files + */ + private uploadFiles(path: string, files: any[]) { if (files.length) { let latestFilesAdded = this._uploaderService.addToQueue(files); - this._uploaderService.uploadFilesInTheQueue(this.uploaddirectory, this.onSuccess); + this._uploaderService.uploadFilesInTheQueue(path, this.onSuccess); this.filesUploadingList = this._uploaderService.getQueue(); if (this.showUploadDialog) { this._showDialog(); @@ -130,6 +165,52 @@ export class UploadButtonComponent { } } + /** + * It converts the array given as input into a map. The map is a key values pairs, where the key is the directory name and the value are + * all the files that the directory contains. + * @param files - array of files + * @returns {Map} + */ + private convertIntoHashMap(files: any[]) { + let directoryMap = new Map(); + for (let file of files) { + let directory = this.getDirectoryPath(file.webkitRelativePath); + let filesSomeDir = directoryMap.get(directory) || []; + filesSomeDir.push(file); + directoryMap.set(directory, filesSomeDir); + } + return directoryMap; + } + + /** + * Split the directory path given as input and cut the last directory name + * @param directory + * @returns {string} + */ + private getDirectoryPath(directory: string) { + let relativeDirPath = ''; + let dirPath = directory.split('/'); + if (dirPath.length > 1) { + dirPath.pop(); + relativeDirPath = '/' + dirPath.join('/'); + } + return relativeDirPath; + } + + /** + * Split a directory path passed in input and return the first directory name + * @param directory + * @returns {string} + */ + private getDirectoryName(directory: string) { + let dirPath = directory.split('/'); + if (dirPath.length > 1) { + return dirPath.pop(); + } else { + return dirPath[0]; + } + } + /** * Show undo notification bar. * @@ -140,15 +221,19 @@ export class UploadButtonComponent { componentHandler.upgradeAllRegistered(); } + let messageTranslate, actionTranslate: any; + messageTranslate = this.translate.get('FILE_UPLOAD.MESSAGES.PROGRESS'); + actionTranslate = this.translate.get('FILE_UPLOAD.ACTION.UNDO'); + this.undoNotificationBar.nativeElement.MaterialSnackbar.showSnackbar({ - message: this.translate.get('FILE_UPLOAD.MESSAGES.PROGRESS'), + message: messageTranslate.value, timeout: 5000, actionHandler: function () { latestFilesAdded.forEach((uploadingFileModel: FileModel) => { uploadingFileModel.setAbort(); }); }, - actionText: this.translate.get('FILE_UPLOAD.ACTION.UNDO') + actionText: actionTranslate.value }); } diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts b/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts index d6dc4f1ea9..8e105169ef 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts @@ -111,6 +111,7 @@ export class UploadService { */ private getAlfrescoClient() { let defaultClient = new AlfrescoApi.ApiClient(); + defaultClient.basePath = this.getHost() + this.getBaseUrl(); // Configure HTTP basic authorization: basicAuth let basicAuth = defaultClient.authentications['basicAuth']; @@ -258,7 +259,7 @@ export class UploadService { }; return Observable.fromPromise(apiInstance.addNode(nodeId, nodeBody)) .map(res => { - console.log(res); + return res; }) .do(data => console.log('Node data', data)) // eyeball results in the console .catch(this.handleError);