[ACS-437] Content Version Service (#7312)

* content version service

* remove fdescribe
This commit is contained in:
Denys Vuika 2021-10-20 16:01:18 +01:00 committed by GitHub
parent 5226f919ff
commit e4949b6cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 15 deletions

View File

@ -0,0 +1,49 @@
/*!
* @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';
import { AlfrescoApiService } from '@alfresco/adf-core';
import { ContentApi } from '@alfresco/js-api';
import { Observable, of } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class ContentVersionService {
private _contentApi: ContentApi;
get contentApi(): ContentApi {
if (!this._contentApi) {
this._contentApi = new ContentApi(this.alfrescoApi.getInstance());
}
return this._contentApi;
}
constructor(private alfrescoApi: AlfrescoApiService) {}
/**
* Get content URL for the given nodeId and specific version.
* @param nodeId The ID of the node
* @param versionId The ID of the version
* @param attachment Retrieve content as an attachment for download
* @returns The URL address pointing to the content.
*/
getVersionContentUrl(nodeId: string, versionId: string, attachment?: boolean): Observable<string> {
let nodeDownloadUrl = this.contentApi.getContentUrl(nodeId, attachment);
nodeDownloadUrl = nodeDownloadUrl.replace('/content', '/versions/' + versionId + '/content');
return of(nodeDownloadUrl);
}
}

View File

@ -19,5 +19,5 @@ export * from './version-list.component';
export * from './version-manager.component';
export * from './version-upload.component';
export * from './version-comparison.component';
export * from './content-version.service';
export * from './version-manager.module';

View File

@ -25,11 +25,13 @@ import { of } from 'rxjs';
import { Node, VersionPaging, VersionEntry, NodeEntry } from '@alfresco/js-api';
import { ContentTestingModule } from '../testing/content.testing.module';
import { TranslateModule } from '@ngx-translate/core';
import { ContentVersionService } from './content-version.service';
describe('VersionListComponent', () => {
let component: VersionListComponent;
let fixture: ComponentFixture<VersionListComponent>;
let dialog: MatDialog;
let contentVersionService: ContentVersionService;
const nodeId = 'test-id';
const versionId = '1.0';
@ -59,6 +61,7 @@ describe('VersionListComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(VersionListComponent);
dialog = TestBed.inject(MatDialog);
contentVersionService = TestBed.inject(ContentVersionService);
component = fixture.componentInstance;
component.node = <Node> { id: nodeId, allowableOperations: ['update'] };
@ -218,12 +221,12 @@ describe('VersionListComponent', () => {
}
};
spyOn(component['versionsApi'], 'listVersionHistory').and.returnValue(Promise.resolve(new VersionPaging({ list: { entries: [versionEntry] } })));
spyOn(component['contentApi'], 'getContentUrl').and.returnValue('the/download/url');
spyOn(contentVersionService.contentApi, 'getContentUrl').and.returnValue('the/download/url');
fixture.detectChanges();
component.downloadVersion('1.0');
expect(component['contentApi'].getContentUrl).toHaveBeenCalledWith(nodeId, true);
expect(contentVersionService.contentApi.getContentUrl).toHaveBeenCalledWith(nodeId, true);
});
it('should be able to view a version', () => {

View File

@ -20,6 +20,7 @@ import { Component, Input, OnChanges, ViewEncapsulation, EventEmitter, Output }
import { VersionsApi, Node, VersionEntry, VersionPaging, NodesApi, NodeEntry, ContentApi } from '@alfresco/js-api';
import { MatDialog } from '@angular/material/dialog';
import { ConfirmDialogComponent } from '../dialogs/confirm.dialog';
import { ContentVersionService } from './content-version.service';
@Component({
selector: 'adf-version-list',
@ -75,18 +76,19 @@ export class VersionListComponent implements OnChanges {
/** Emitted when a version is restored */
@Output()
restored: EventEmitter<Node> = new EventEmitter<Node>();
restored = new EventEmitter<Node>();
/** Emitted when a version is deleted */
@Output()
deleted: EventEmitter<Node> = new EventEmitter<Node>();
deleted = new EventEmitter<Node>();
/** Emitted when viewing a version */
@Output()
viewVersion: EventEmitter<string> = new EventEmitter<string>();
viewVersion = new EventEmitter<string>();
constructor(private alfrescoApi: AlfrescoApiService,
private contentService: ContentService,
private contentVersionService: ContentVersionService,
private dialog: MatDialog) {
}
@ -102,7 +104,7 @@ export class VersionListComponent implements OnChanges {
return this.contentService.hasAllowableOperations(this.node, 'delete') && this.versions.length > 1;
}
restore(versionId) {
restore(versionId: string) {
if (this.canUpdate()) {
this.versionsApi
.revertVersion(this.node.id, versionId, { majorVersion: true, comment: '' })
@ -116,7 +118,7 @@ export class VersionListComponent implements OnChanges {
}
}
onViewVersion(versionId) {
onViewVersion(versionId: string) {
this.viewVersion.emit(versionId);
}
@ -130,8 +132,9 @@ export class VersionListComponent implements OnChanges {
downloadVersion(versionId: string) {
if (this.allowDownload) {
const versionDownloadUrl = this.getVersionContentUrl(this.node.id, versionId, true);
this.downloadContent(versionDownloadUrl);
this.contentVersionService
.getVersionContentUrl(this.node.id, versionId, true)
.subscribe(versionDownloadUrl => this.downloadContent(versionDownloadUrl));
}
}
@ -167,11 +170,6 @@ export class VersionListComponent implements OnChanges {
this.restored.emit(node?.entry);
}
private getVersionContentUrl(nodeId: string, versionId: string, attachment?: boolean) {
const nodeDownloadUrl = this.contentApi.getContentUrl(nodeId, attachment);
return nodeDownloadUrl.replace('/content', '/versions/' + versionId + '/content');
}
downloadContent(url: string) {
if (url) {
const link = document.createElement('a');