From e39439b68c2b567f8515a8349665ec874b30b123 Mon Sep 17 00:00:00 2001 From: Will Abson Date: Tue, 7 Jun 2016 14:02:09 +0100 Subject: [PATCH] Add new AlfrescoContentService in ng2-alfresco-core - Returns v1 public API endpoints for content and thumbnails - Refactor getHost(), basePath and similar props into settings - Use AlfrescoAuthenticationService to get alf_ticket value --- .../ng2-alfresco-core/ng2-alfresco-core.ts | 3 ++ .../AlfrescoContentService.service.ts | 51 +++++++++++++++++++ .../AlfrescoSettingsService.service.ts | 7 +++ .../src/assets/alfresco.service.mock.ts | 12 +++-- .../src/services/alfresco.service.ts | 38 ++++---------- .../src/assets/alfresco.service.mock.ts | 12 +++-- .../components/alfresco-search.component.ts | 12 ----- .../src/services/alfresco.service.ts | 41 +++++---------- 8 files changed, 102 insertions(+), 74 deletions(-) create mode 100644 ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.service.ts diff --git a/ng2-components/ng2-alfresco-core/ng2-alfresco-core.ts b/ng2-components/ng2-alfresco-core/ng2-alfresco-core.ts index b37c614ece..3af89b70b7 100644 --- a/ng2-components/ng2-alfresco-core/ng2-alfresco-core.ts +++ b/ng2-components/ng2-alfresco-core/ng2-alfresco-core.ts @@ -20,6 +20,7 @@ import { AlfrescoTranslationLoader } from './src/services/AlfrescoTranslationLoa import { AlfrescoTranslationService } from './src/services/AlfrescoTranslationService.service'; import { AlfrescoPipeTranslate } from './src/services/AlfrescoPipeTranslate.service'; import { AlfrescoAuthenticationService } from './src/services/AlfrescoAuthenticationService.service'; +import { AlfrescoContentService } from './src/services/AlfrescoContentService.service'; export * from './src/services/AlfrescoSettingsService.service'; export * from './src/services/AlfrescoTranslationLoader.service'; @@ -27,9 +28,11 @@ export * from './src/services/AlfrescoTranslationService.service'; export * from './src/services/AlfrescoPipeTranslate.service'; export * from './src/material/MaterialDesignLiteUpgradeElement'; export * from './src/services/AlfrescoAuthenticationService.service'; +export * from './src/services/AlfrescoContentService.service'; export const ALFRESCO_CORE_PROVIDERS: [any] = [ AlfrescoAuthenticationService, + AlfrescoContentService, AlfrescoSettingsService, AlfrescoTranslationLoader, AlfrescoTranslationService, diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.service.ts new file mode 100644 index 0000000000..dffecbf90f --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.service.ts @@ -0,0 +1,51 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Injectable } from 'angular2/core'; + +import { AlfrescoAuthenticationService } from './AlfrescoAuthenticationService.service'; +import { AlfrescoSettingsService } from './AlfrescoSettingsService.service'; + +@Injectable() +export class AlfrescoContentService { + + constructor( + private settings: AlfrescoSettingsService, + private authService: AlfrescoAuthenticationService + ) { + } + + /** + * Get thumbnail URL for the given document node. + * @param document Node to get URL for. + * @returns {string} URL address. + */ + getDocumentThumbnailUrl(document: any) { + return this.settings.getApiBaseUrl() + '/nodes/' + document.entry.id + + '/renditions/doclib/content' + '?attachment=false&alf_ticket=' + this.authService.getToken(); + } + + /** + * Get content URL for the given node. + * @param document Node to get URL for. + * @returns {string} URL address. + */ + getContentUrl(document: any) { + return this.settings.getApiBaseUrl() + '/nodes/' + document.entry.id + + '/content' + '?attachment=false&alf_ticket=' + this.authService.getToken(); + } +} diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoSettingsService.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoSettingsService.service.ts index 33741c73fe..754a96e6fc 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoSettingsService.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoSettingsService.service.ts @@ -19,7 +19,10 @@ import { Injectable } from 'angular2/core'; @Injectable() export class AlfrescoSettingsService { + private _host: string = 'http://127.0.0.1:8080'; + private _contextPath = '/alfresco'; + private _apiBasePath: string = '/api/-default-/public/alfresco/versions/1'; public get host(): string { return this._host; @@ -29,6 +32,10 @@ export class AlfrescoSettingsService { this._host = value; } + getApiBaseUrl(): string { + return this._host + this._contextPath + this._apiBasePath; + } + getAuthToken(): string { // todo: get proper token value return 'Basic ' + btoa('admin:admin'); diff --git a/ng2-components/ng2-alfresco-documentlist/src/assets/alfresco.service.mock.ts b/ng2-components/ng2-alfresco-documentlist/src/assets/alfresco.service.mock.ts index ebcb6c6466..1a30abe443 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/assets/alfresco.service.mock.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/assets/alfresco.service.mock.ts @@ -17,16 +17,22 @@ import {Observable} from 'rxjs/Observable'; import {AlfrescoService} from '../../src/services/alfresco.service'; -import {AlfrescoSettingsService} from 'ng2-alfresco-core/dist/ng2-alfresco-core'; +import { + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoContentService +} from 'ng2-alfresco-core/dist/ng2-alfresco-core'; export class AlfrescoServiceMock extends AlfrescoService { _folderToReturn: any = {}; constructor( - settings: AlfrescoSettingsService = null + settings: AlfrescoSettingsService = null, + authService: AlfrescoAuthenticationService = null, + contentService: AlfrescoContentService = null ) { - super(settings); + super(settings, authService, contentService); } getFolder(folder: string) { diff --git a/ng2-components/ng2-alfresco-documentlist/src/services/alfresco.service.ts b/ng2-components/ng2-alfresco-documentlist/src/services/alfresco.service.ts index 58f49456fe..02e5fbc38d 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/services/alfresco.service.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/services/alfresco.service.ts @@ -19,7 +19,11 @@ import { Injectable } from 'angular2/core'; import { Response } from 'angular2/http'; import { Observable } from 'rxjs/Rx'; import { NodePaging, MinimalNodeEntity } from './../models/document-library.model'; -import { AlfrescoSettingsService } from 'ng2-alfresco-core/dist/ng2-alfresco-core'; +import { + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoContentService +} from 'ng2-alfresco-core/dist/ng2-alfresco-core'; declare let AlfrescoApi: any; @@ -62,23 +66,14 @@ export class AlfrescoService { }; constructor( - private settings: AlfrescoSettingsService) { - } - - public getHost(): string { - return this.settings.host; - } - - private getBaseUrl(): string { - return this.getHost() + this._baseUrlPath; - } - - private getAlfrescoTicket() { - return localStorage.getItem('token'); + private settings: AlfrescoSettingsService, + private authService: AlfrescoAuthenticationService, + private contentService: AlfrescoContentService + ) { } private getAlfrescoClient() { - return AlfrescoApi.getClientWithTicket(this.getBaseUrl(), this.getAlfrescoTicket()); + return AlfrescoApi.getClientWithTicket(this.settings.getApiBaseUrl(), this.authService.getToken()); } private getNodesPromise(folder: string) { @@ -117,18 +112,7 @@ export class AlfrescoService { * @returns {string} URL address. */ getDocumentThumbnailUrl(document: MinimalNodeEntity) { - return this.getContentUrl(document) + '/thumbnails/doclib?c=queue&ph=true&lastModified=1&alf_ticket=' + this.getAlfrescoTicket(); - } - - /** - * Get content URL for the given node. - * @param document Node to get URL for. - * @returns {string} URL address. - */ - getContentUrl(document: MinimalNodeEntity) { - return this.getHost() + - '/alfresco/service/api/node/workspace/SpacesStore/' + - document.entry.id + '/content'; + return this.contentService.getDocumentThumbnailUrl(document); } getMimeTypeIcon(mimeType: string): string { diff --git a/ng2-components/ng2-alfresco-search/src/assets/alfresco.service.mock.ts b/ng2-components/ng2-alfresco-search/src/assets/alfresco.service.mock.ts index e748a63bf5..1746fded55 100644 --- a/ng2-components/ng2-alfresco-search/src/assets/alfresco.service.mock.ts +++ b/ng2-components/ng2-alfresco-search/src/assets/alfresco.service.mock.ts @@ -18,7 +18,11 @@ import {Http} from 'angular2/http'; import {Observable} from 'rxjs/Observable'; -import {AlfrescoSettingsService} from 'ng2-alfresco-core/dist/ng2-alfresco-core'; +import { + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoContentService +} from 'ng2-alfresco-core/dist/ng2-alfresco-core'; import {AlfrescoService} from './../../src/services/alfresco.service'; export class AlfrescoServiceMock extends AlfrescoService { @@ -26,9 +30,11 @@ export class AlfrescoServiceMock extends AlfrescoService { _folderToReturn: any = {}; constructor( - settings: AlfrescoSettingsService = null + settings: AlfrescoSettingsService = null, + authService: AlfrescoAuthenticationService = null, + contentService: AlfrescoContentService = null ) { - super(settings); + super(settings, authService, contentService); } getFolder(folder: string) { diff --git a/ng2-components/ng2-alfresco-search/src/components/alfresco-search.component.ts b/ng2-components/ng2-alfresco-search/src/components/alfresco-search.component.ts index b94b97859b..c94a146211 100644 --- a/ng2-components/ng2-alfresco-search/src/components/alfresco-search.component.ts +++ b/ng2-components/ng2-alfresco-search/src/components/alfresco-search.component.ts @@ -64,18 +64,6 @@ export class AlfrescoSearchComponent implements OnChanges, OnInit { this.displaySearchResults(this.searchTerm); } - /** - * Gets content URL for the given node. - * @param node Node to get URL for. - * @returns {string} URL address. - */ - getContentUrl(node: any): string { - if (this._alfrescoService) { - return this._alfrescoService.getContentUrl(node); - } - return null; - } - /** * Gets thumbnail URL for the given document node. * @param node Node to get URL for. diff --git a/ng2-components/ng2-alfresco-search/src/services/alfresco.service.ts b/ng2-components/ng2-alfresco-search/src/services/alfresco.service.ts index 4e88e59051..4c961e7ac8 100644 --- a/ng2-components/ng2-alfresco-search/src/services/alfresco.service.ts +++ b/ng2-components/ng2-alfresco-search/src/services/alfresco.service.ts @@ -18,7 +18,11 @@ import { Injectable } from 'angular2/core'; import { Observable } from 'rxjs/Observable'; -import { AlfrescoSettingsService } from 'ng2-alfresco-core/dist/ng2-alfresco-core'; +import { + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoContentService +} from 'ng2-alfresco-core/dist/ng2-alfresco-core'; declare let AlfrescoApi: any; @@ -28,25 +32,15 @@ declare let AlfrescoApi: any; @Injectable() export class AlfrescoService { - private _baseUrlPath: string = '/alfresco/api/-default-/public/alfresco/versions/1'; - - constructor(private settings: AlfrescoSettingsService) { - } - - public getHost(): string { - return this.settings.host; - } - - private getBaseUrl(): string { - return this.getHost() + this._baseUrlPath; - } - - private getAlfrescoTicket() { - return localStorage.getItem('token'); + constructor( + private settings: AlfrescoSettingsService, + private authService: AlfrescoAuthenticationService, + private contentService: AlfrescoContentService + ) { } private getAlfrescoClient() { - return AlfrescoApi.getClientWithTicket(this.getBaseUrl(), this.getAlfrescoTicket()); + return AlfrescoApi.getClientWithTicket(this.settings.getApiBaseUrl(), this.authService.getToken()); } private getSearchNodesPromise(term: string) { @@ -78,18 +72,7 @@ export class AlfrescoService { * @returns {string} URL address. */ getDocumentThumbnailUrl(document: any) { - return this.getContentUrl(document) + '/thumbnails/doclib?c=queue&ph=true&lastModified=1&alf_ticket=' + this.getAlfrescoTicket(); - } - - /** - * Get content URL for the given node. - * @param document Node to get URL for. - * @returns {string} URL address. - */ - getContentUrl(document: any) { - return this.getHost() + - '/alfresco/service/api/node/workspace/SpacesStore/' + - document.entry.id + '/content'; + return this.contentService.getDocumentThumbnailUrl(document); } private handleError(error: any) {