diff --git a/ng2-components/ng2-alfresco-core/README.md b/ng2-components/ng2-alfresco-core/README.md index 078df01b91..ca92172b1f 100644 --- a/ng2-components/ng2-alfresco-core/README.md +++ b/ng2-components/ng2-alfresco-core/README.md @@ -154,6 +154,12 @@ bootstrap(MyDemoApp, [ ``` +#### Renditions Service + +* getRenditionsListByNodeId(nodeId: string) +* createRendition(nodeId: string, encoding: string) +* getRendition(nodeId: string, encoding: string) +* isRenditionAvailable(nodeId: string, encoding: string) ## Build from sources diff --git a/ng2-components/ng2-alfresco-core/src/services/index.ts b/ng2-components/ng2-alfresco-core/src/services/index.ts index b4d963d6e1..c5eb956dc8 100644 --- a/ng2-components/ng2-alfresco-core/src/services/index.ts +++ b/ng2-components/ng2-alfresco-core/src/services/index.ts @@ -22,3 +22,4 @@ export * from './AlfrescoTranslation.service'; export * from './AlfrescoPipeTranslate.service'; export * from './AlfrescoAuthentication.service'; export * from './AlfrescoContent.service'; +export * from './renditions.service'; diff --git a/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts new file mode 100644 index 0000000000..fafae1bdb8 --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts @@ -0,0 +1,174 @@ +/*! + * @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 { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; +import { RenditionsService } from './renditions.service'; +import { AlfrescoApiService } from './AlfrescoApi.service'; +import { AlfrescoSettingsService } from './AlfrescoSettings.service'; +import { AlfrescoAuthenticationService } from './AlfrescoAuthentication.service'; + +declare let AlfrescoApi: any; +declare let jasmine: any; + +describe('RenditionsService', () => { + let service: any; + + let fakeRedition = { + 'entry': { + 'id': 'pdf', + 'content': {'mimeType': 'application/pdf', 'mimeTypeName': 'Adobe PDF Document'}, + 'status': 'NOT_CREATED' + } + }; + + let fakeReditionsList = { + 'list': { + 'pagination': { + 'count': 6, + 'hasMoreItems': false, + 'totalItems': 6, + 'skipCount': 0, + 'maxItems': 100 + }, + 'entries': [{ + 'entry': { + 'id': 'avatar', + 'content': {'mimeType': 'image/png', 'mimeTypeName': 'PNG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'avatar32', + 'content': {'mimeType': 'image/png', 'mimeTypeName': 'PNG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'doclib', + 'content': {'mimeType': 'image/png', 'mimeTypeName': 'PNG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'imgpreview', + 'content': {'mimeType': 'image/jpeg', 'mimeTypeName': 'JPEG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'medium', + 'content': {'mimeType': 'image/jpeg', 'mimeTypeName': 'JPEG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'pdf', + 'content': {'mimeType': 'application/pdf', 'mimeTypeName': 'Adobe PDF Document'}, + 'status': 'NOT_CREATED' + } + }] + } + }; + + beforeEachProviders(() => { + return [ + AlfrescoSettingsService, + AlfrescoApiService, + AlfrescoAuthenticationService, + RenditionsService + ]; + }); + + beforeEach(inject([RenditionsService, AlfrescoApiService], (renditionsService: RenditionsService, apiService: AlfrescoApiService) => { + jasmine.Ajax.install(); + service = renditionsService; + apiService.setInstance(new AlfrescoApi({})); + })); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + + it('Get redition list service should call the server with the ID passed', (done) => { + service.getRenditionsListByNodeId('fake-node-id').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeReditionsList) + }); + }); + + it('Get redition service should call the server with the ID passed', (done) => { + service.getRendition('fake-node-id', 'pdf').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions/pdf'); + expect(res.entry.status).toBe('NOT_CREATED'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeRedition) + }); + }); + + it('isRenditionsAvailable service should call the server with the ID passed and return false if is not created', (done) => { + service.isRenditionAvailable('fake-node-id', 'pdf').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions/pdf'); + expect(res).toBe(false); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeRedition) + }); + }); + + it('Create redition service should call the server with the ID passed and the asked encoding', (done) => { + service.createRendition('fake-node-id', 'pdf').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: '' + }); + }); + + it('Get redition service should catch the error', (done) => { + service.getRenditionsListByNodeId('fake-node-id').subscribe((res) => { + }, (res) => { + done(); + } + ); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 403, + contentType: 'application/json', + responseText: 'error' + }); + }); +}); diff --git a/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts b/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts new file mode 100644 index 0000000000..3960c5948d --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts @@ -0,0 +1,66 @@ +/*! + * @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 '@angular/core'; +import { Observable } from 'rxjs/Rx'; +import { AlfrescoApiService } from './AlfrescoApi.service'; + +/** + * RenditionsService + * + * @returns {RenditionsService} . + */ +@Injectable() +export class RenditionsService { + + constructor(private apiService: AlfrescoApiService) { + + } + + isRenditionAvailable(nodeId: string, encoding: string) { + return Observable.create((observer) => { + this.getRendition(nodeId, encoding).subscribe((res) => { + let isAvailable = true; + if (res.entry.status === 'NOT_CREATED') { + isAvailable = false; + } + observer.next(isAvailable); + observer.complete(); + }); + }); + } + + getRendition(nodeId: string, encoding: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.getRendition(nodeId, encoding)) + .catch(this.handleError); + } + + getRenditionsListByNodeId(nodeId: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.getRenditions(nodeId)) + .catch(this.handleError); + } + + createRendition(nodeId: string, encoding: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.createRendition(nodeId, {id: 'pdf'})) + .catch(this.handleError); + } + + private handleError(error: any): Observable { + console.error(error); + return Observable.throw(error || 'Server error'); + } +}