mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-4647] download service (#4836)
* split download api into separate service * move tests and fix code * break dependency for thumbnail service * update tests * test fixes * fix code * fix unit tests
This commit is contained in:
committed by
Eugenio Romano
parent
d0d1154f84
commit
a37f935c05
@@ -79,7 +79,7 @@ describe('ContentService', () => {
|
||||
|
||||
it('should return a valid content URL', (done) => {
|
||||
authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||
expect(contentService.getContentUrl(node)).toBe('http://localhost:9876/ecm/alfresco/api/' +
|
||||
expect(contentService.getContentUrl(node)).toContain('/ecm/alfresco/api/' +
|
||||
'-default-/public/alfresco/versions/1/nodes/fake-node-id/content?attachment=false&alf_ticket=fake-post-ticket');
|
||||
done();
|
||||
});
|
||||
@@ -94,7 +94,7 @@ describe('ContentService', () => {
|
||||
it('should return a valid thumbnail URL', (done) => {
|
||||
authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||
expect(contentService.getDocumentThumbnailUrl(node))
|
||||
.toBe('http://localhost:9876/ecm/alfresco/api/-default-/public/alfresco' +
|
||||
.toContain('/ecm/alfresco/api/-default-/public/alfresco' +
|
||||
'/versions/1/nodes/fake-node-id/renditions/doclib/content?attachment=false&alf_ticket=fake-post-ticket');
|
||||
done();
|
||||
});
|
||||
@@ -178,19 +178,4 @@ describe('ContentService', () => {
|
||||
expect(contentService.hasPermissions(permissionNode, '!Consumer')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Download blob', () => {
|
||||
|
||||
it('Should use native msSaveOrOpenBlob if the browser is IE', (done) => {
|
||||
|
||||
const navigatorAny: any = window.navigator;
|
||||
|
||||
navigatorAny.__defineGetter__('msSaveOrOpenBlob', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
const blob = new Blob([''], { type: 'text/html' });
|
||||
contentService.downloadBlob(blob, 'test_ie');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -26,14 +26,14 @@ import { LogService } from './log.service';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import { PermissionsEnum } from '../models/permissions.enum';
|
||||
import { AllowableOperationsEnum } from '../models/allowable-operations.enum';
|
||||
import { DownloadService } from './download.service';
|
||||
import { ThumbnailService } from './thumbnail.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ContentService {
|
||||
|
||||
private saveData: Function;
|
||||
|
||||
folderCreated: Subject<FolderCreatedEvent> = new Subject<FolderCreatedEvent>();
|
||||
folderCreate: Subject<MinimalNode> = new Subject<MinimalNode>();
|
||||
folderEdit: Subject<MinimalNode> = new Subject<MinimalNode>();
|
||||
@@ -41,66 +41,39 @@ export class ContentService {
|
||||
constructor(public authService: AuthenticationService,
|
||||
public apiService: AlfrescoApiService,
|
||||
private logService: LogService,
|
||||
private sanitizer: DomSanitizer) {
|
||||
this.saveData = (function () {
|
||||
const a = document.createElement('a');
|
||||
document.body.appendChild(a);
|
||||
a.style.display = 'none';
|
||||
|
||||
return function (fileData, format, fileName) {
|
||||
let blob = null;
|
||||
|
||||
if (format === 'blob' || format === 'data') {
|
||||
blob = new Blob([fileData], { type: 'octet/stream' });
|
||||
}
|
||||
|
||||
if (format === 'object' || format === 'json') {
|
||||
const json = JSON.stringify(fileData);
|
||||
blob = new Blob([json], { type: 'octet/stream' });
|
||||
}
|
||||
|
||||
if (blob) {
|
||||
|
||||
if (typeof window.navigator !== 'undefined' && window.navigator.msSaveOrOpenBlob) {
|
||||
navigator.msSaveOrOpenBlob(blob, fileName);
|
||||
} else {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
a.click();
|
||||
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
};
|
||||
}());
|
||||
private sanitizer: DomSanitizer,
|
||||
private downloadService: DownloadService,
|
||||
private thumbnailService: ThumbnailService) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated in 3.2.0, use DownloadService instead.
|
||||
* Invokes content download for a Blob with a file name.
|
||||
* @param blob Content to download.
|
||||
* @param fileName Name of the resulting file.
|
||||
*/
|
||||
downloadBlob(blob: Blob, fileName: string): void {
|
||||
this.saveData(blob, 'blob', fileName);
|
||||
this.downloadService.downloadBlob(blob, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated in 3.2.0, use DownloadService instead.
|
||||
* Invokes content download for a data array with a file name.
|
||||
* @param data Data to download.
|
||||
* @param fileName Name of the resulting file.
|
||||
*/
|
||||
downloadData(data: any, fileName: string): void {
|
||||
this.saveData(data, 'data', fileName);
|
||||
this.downloadService.downloadData(data, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated in 3.2.0, use DownloadService instead.
|
||||
* Invokes content download for a JSON object with a file name.
|
||||
* @param json JSON object to download.
|
||||
* @param fileName Name of the resulting file.
|
||||
*/
|
||||
downloadJSON(json: any, fileName: string): void {
|
||||
this.saveData(json, 'json', fileName);
|
||||
this.downloadService.downloadJSON(json, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,35 +92,38 @@ export class ContentService {
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated in 3.2.0, use ThumbnailService instead.
|
||||
* Gets a thumbnail URL for the given document node.
|
||||
* @param node Node to get URL for.
|
||||
* @param node Node or Node ID to get URL for.
|
||||
* @param attachment Toggles whether to retrieve content as an attachment for download
|
||||
* @param ticket Custom ticket to use for authentication
|
||||
* @returns URL string
|
||||
*/
|
||||
getDocumentThumbnailUrl(node: any, attachment?: boolean, ticket?: string): string {
|
||||
|
||||
if (node && node.entry) {
|
||||
node = node.entry.id;
|
||||
}
|
||||
|
||||
return this.contentApi.getDocumentThumbnailUrl(node, attachment, ticket);
|
||||
getDocumentThumbnailUrl(node: NodeEntry | string, attachment?: boolean, ticket?: string): string {
|
||||
return this.thumbnailService.getDocumentThumbnailUrl(node, attachment, ticket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a content URL for the given node.
|
||||
* @param node Node to get URL for.
|
||||
* @param node Node or Node ID to get URL for.
|
||||
* @param attachment Toggles whether to retrieve content as an attachment for download
|
||||
* @param ticket Custom ticket to use for authentication
|
||||
* @returns URL string
|
||||
* @returns URL string or `null`
|
||||
*/
|
||||
getContentUrl(node: any, attachment?: boolean, ticket?: string): string {
|
||||
getContentUrl(node: NodeEntry | string, attachment?: boolean, ticket?: string): string {
|
||||
if (node) {
|
||||
let nodeId: string;
|
||||
|
||||
if (node && node.entry) {
|
||||
node = node.entry.id;
|
||||
if (typeof node === 'string') {
|
||||
nodeId = node;
|
||||
} else if (node.entry) {
|
||||
nodeId = node.entry.id;
|
||||
}
|
||||
|
||||
return this.contentApi.getContentUrl(nodeId, attachment, ticket);
|
||||
}
|
||||
|
||||
return this.contentApi.getContentUrl(node, attachment, ticket);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
39
lib/core/services/download.service.spec.ts
Normal file
39
lib/core/services/download.service.spec.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2019 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 { DownloadService } from './download.service';
|
||||
|
||||
describe('DownloadService', () => {
|
||||
let service: DownloadService;
|
||||
|
||||
beforeEach(() => {
|
||||
service = new DownloadService();
|
||||
});
|
||||
|
||||
describe('Download blob', () => {
|
||||
it('Should use native msSaveOrOpenBlob if the browser is IE', (done) => {
|
||||
const navigatorAny: any = window.navigator;
|
||||
|
||||
navigatorAny.__defineGetter__('msSaveOrOpenBlob', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
const blob = new Blob([''], { type: 'text/html' });
|
||||
service.downloadBlob(blob, 'test_ie');
|
||||
});
|
||||
});
|
||||
});
|
89
lib/core/services/download.service.ts
Normal file
89
lib/core/services/download.service.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2019 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';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DownloadService {
|
||||
private saveData: Function;
|
||||
|
||||
constructor() {
|
||||
this.saveData = (function() {
|
||||
const a = document.createElement('a');
|
||||
document.body.appendChild(a);
|
||||
a.style.display = 'none';
|
||||
|
||||
return function(fileData, format, fileName) {
|
||||
let blob = null;
|
||||
|
||||
if (format === 'blob' || format === 'data') {
|
||||
blob = new Blob([fileData], { type: 'octet/stream' });
|
||||
}
|
||||
|
||||
if (format === 'object' || format === 'json') {
|
||||
const json = JSON.stringify(fileData);
|
||||
blob = new Blob([json], { type: 'octet/stream' });
|
||||
}
|
||||
|
||||
if (blob) {
|
||||
if (
|
||||
typeof window.navigator !== 'undefined' &&
|
||||
window.navigator.msSaveOrOpenBlob
|
||||
) {
|
||||
navigator.msSaveOrOpenBlob(blob, fileName);
|
||||
} else {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
a.click();
|
||||
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes content download for a Blob with a file name.
|
||||
* @param blob Content to download.
|
||||
* @param fileName Name of the resulting file.
|
||||
*/
|
||||
downloadBlob(blob: Blob, fileName: string): void {
|
||||
this.saveData(blob, 'blob', fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes content download for a data array with a file name.
|
||||
* @param data Data to download.
|
||||
* @param fileName Name of the resulting file.
|
||||
*/
|
||||
downloadData(data: any, fileName: string): void {
|
||||
this.saveData(data, 'data', fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes content download for a JSON object with a file name.
|
||||
* @param json JSON object to download.
|
||||
* @param fileName Name of the resulting file.
|
||||
*/
|
||||
downloadJSON(json: any, fileName: string): void {
|
||||
this.saveData(json, 'json', fileName);
|
||||
}
|
||||
}
|
@@ -56,3 +56,4 @@ export * from './download-zip.service';
|
||||
export * from './lock.service';
|
||||
export * from './automation.service';
|
||||
export * from './automation.service';
|
||||
export * from './download.service';
|
||||
|
@@ -19,10 +19,12 @@ import { TestBed } from '@angular/core/testing';
|
||||
import { ThumbnailService } from './thumbnail.service';
|
||||
import { setupTestBed } from '../testing/setupTestBed';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
|
||||
describe('ThumbnailService', () => {
|
||||
|
||||
let service: ThumbnailService;
|
||||
let apiService: AlfrescoApiService;
|
||||
|
||||
setupTestBed({
|
||||
imports: [CoreTestingModule]
|
||||
@@ -30,6 +32,7 @@ describe('ThumbnailService', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
service = TestBed.get(ThumbnailService);
|
||||
apiService = TestBed.get(AlfrescoApiService);
|
||||
});
|
||||
|
||||
it('should return the correct icon for a plain text file', () => {
|
||||
@@ -49,8 +52,8 @@ describe('ThumbnailService', () => {
|
||||
});
|
||||
|
||||
it('should return the thumbnail URL for a content item', () => {
|
||||
spyOn(service.contentService, 'getDocumentThumbnailUrl').and.returnValue('/fake-thumbnail.png');
|
||||
expect(service.getDocumentThumbnailUrl({})).toContain('/fake-thumbnail.png');
|
||||
spyOn(apiService.contentApi, 'getDocumentThumbnailUrl').and.returnValue('/fake-thumbnail.png');
|
||||
expect(service.getDocumentThumbnailUrl('some-id')).toContain('/fake-thumbnail.png');
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -19,7 +19,8 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { MatIconRegistry } from '@angular/material';
|
||||
import { DomSanitizer } from '@angular/platform-browser';
|
||||
import { ContentService } from './content.service';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { NodeEntry } from '@alfresco/js-api';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -156,7 +157,7 @@ export class ThumbnailService {
|
||||
'selected': './assets/images/ft_ic_selected.svg'
|
||||
};
|
||||
|
||||
constructor(public contentService: ContentService, matIconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
|
||||
constructor(protected apiService: AlfrescoApiService, matIconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
|
||||
Object.keys(this.mimeTypeIcons).forEach((key) => {
|
||||
const url = sanitizer.bypassSecurityTrustResourceUrl(this.mimeTypeIcons[key]);
|
||||
|
||||
@@ -167,12 +168,25 @@ export class ThumbnailService {
|
||||
|
||||
/**
|
||||
* Gets a thumbnail URL for the given document node.
|
||||
* @param node Node to get URL for.
|
||||
* @param node Node or Node ID to get URL for.
|
||||
* @returns URL string
|
||||
*/
|
||||
public getDocumentThumbnailUrl(node: any): string {
|
||||
const thumbnail = this.contentService.getDocumentThumbnailUrl(node);
|
||||
return thumbnail || this.DEFAULT_ICON;
|
||||
getDocumentThumbnailUrl(node: NodeEntry | string, attachment?: boolean, ticket?: string): string {
|
||||
let resultUrl: string;
|
||||
|
||||
if (node) {
|
||||
let nodeId: string;
|
||||
|
||||
if (typeof node === 'string') {
|
||||
nodeId = node;
|
||||
} else if (node.entry) {
|
||||
nodeId = node.entry.id;
|
||||
}
|
||||
|
||||
resultUrl = this.apiService.contentApi.getDocumentThumbnailUrl(nodeId, attachment, ticket);
|
||||
}
|
||||
|
||||
return resultUrl || this.DEFAULT_ICON;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user