mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
fixed google scholar links, todo: fix tests
This commit is contained in:
@@ -57,8 +57,8 @@ function generateEnvironmentFile(file: GlobalConfig): void {
|
||||
|
||||
// TODO remove workaround in beta 5
|
||||
if (file.rest.nameSpace.match("(.*)/api/?$") !== null) {
|
||||
const newValue = getNameSpace(file.rest.nameSpace);
|
||||
console.log(colors.white.bgMagenta.bold(`The rest.nameSpace property in your environment file or in your DSPACE_REST_NAMESPACE environment variable ends with '/api'.\nThis is deprecated. As '/api' isn't configurable on the rest side, it shouldn't be repeated in every environment file.\nPlease change the rest nameSpace to '${newValue}'`));
|
||||
file.rest.nameSpace = getNameSpace(file.rest.nameSpace);
|
||||
console.log(colors.white.bgMagenta.bold(`The rest.nameSpace property in your environment file or in your DSPACE_REST_NAMESPACE environment variable ends with '/api'.\nThis is deprecated. As '/api' isn't configurable on the rest side, it shouldn't be repeated in every environment file.\nPlease change the rest nameSpace to '${file.rest.nameSpace}'`));
|
||||
}
|
||||
|
||||
const contents = `export const environment = ` + JSON.stringify(file);
|
||||
|
@@ -52,6 +52,7 @@ import { UUIDService } from '../shared/uuid.service';
|
||||
import { MetadataService } from './metadata.service';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { storeModuleConfig } from '../../app.reducer';
|
||||
import { HardRedirectService } from '../services/hard-redirect.service';
|
||||
|
||||
/* tslint:disable:max-classes-per-file */
|
||||
@Component({
|
||||
@@ -170,6 +171,7 @@ describe('MetadataService', () => {
|
||||
Title,
|
||||
// tslint:disable-next-line:no-empty
|
||||
{ provide: ItemDataService, useValue: { findById: () => {} } },
|
||||
{ provide: HardRedirectService, useValue: { rewriteDownloadURL: (a) => a }, getRequestOrigin: () => environment.ui.baseUrl },
|
||||
BrowseService,
|
||||
MetadataService
|
||||
],
|
||||
|
@@ -21,6 +21,7 @@ import { DSpaceObject } from '../shared/dspace-object.model';
|
||||
import { Item } from '../shared/item.model';
|
||||
import { getFirstSucceededRemoteDataPayload, getFirstSucceededRemoteListPayload } from '../shared/operators';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { HardRedirectService } from '../services/hard-redirect.service';
|
||||
|
||||
@Injectable()
|
||||
export class MetadataService {
|
||||
@@ -39,6 +40,7 @@ export class MetadataService {
|
||||
private dsoNameService: DSONameService,
|
||||
private bitstreamDataService: BitstreamDataService,
|
||||
private bitstreamFormatDataService: BitstreamFormatDataService,
|
||||
private redirectService: HardRedirectService
|
||||
) {
|
||||
// TODO: determine what open graph meta tags are needed and whether
|
||||
// the differ per route. potentially add image based on DSpaceObject
|
||||
@@ -254,7 +256,7 @@ export class MetadataService {
|
||||
*/
|
||||
private setCitationAbstractUrlTag(): void {
|
||||
if (this.currentObject.value instanceof Item) {
|
||||
const value = [environment.ui.baseUrl, this.router.url].join('');
|
||||
const value = [this.redirectService.getRequestOrigin(), this.router.url].join('');
|
||||
this.addMetaTag('citation_abstract_html_url', value);
|
||||
}
|
||||
}
|
||||
@@ -279,7 +281,8 @@ export class MetadataService {
|
||||
getFirstSucceededRemoteDataPayload()
|
||||
).subscribe((format: BitstreamFormat) => {
|
||||
if (format.mimetype === 'application/pdf') {
|
||||
this.addMetaTag('citation_pdf_url', bitstream._links.content.href);
|
||||
const rewrittenURL= this.redirectService.rewriteDownloadURL(bitstream._links.content.href);
|
||||
this.addMetaTag('citation_pdf_url', rewrittenURL);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -2,11 +2,12 @@ import {TestBed} from '@angular/core/testing';
|
||||
import {BrowserHardRedirectService} from './browser-hard-redirect.service';
|
||||
|
||||
describe('BrowserHardRedirectService', () => {
|
||||
|
||||
const origin = 'test origin';
|
||||
const mockLocation = {
|
||||
href: undefined,
|
||||
pathname: '/pathname',
|
||||
search: '/search',
|
||||
origin
|
||||
} as Location;
|
||||
|
||||
const service: BrowserHardRedirectService = new BrowserHardRedirectService(mockLocation);
|
||||
@@ -38,4 +39,12 @@ describe('BrowserHardRedirectService', () => {
|
||||
expect(service.getCurrentRoute()).toEqual(mockLocation.pathname + mockLocation.search);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when requesting the origin', () => {
|
||||
|
||||
it('should return the location origin', () => {
|
||||
expect(service.getRequestOrigin()).toEqual(origin);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -11,11 +11,12 @@ export function locationProvider(): Location {
|
||||
* Service for performing hard redirects within the browser app module
|
||||
*/
|
||||
@Injectable()
|
||||
export class BrowserHardRedirectService implements HardRedirectService {
|
||||
export class BrowserHardRedirectService extends HardRedirectService {
|
||||
|
||||
constructor(
|
||||
@Inject(LocationToken) protected location: Location,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,4 +33,11 @@ export class BrowserHardRedirectService implements HardRedirectService {
|
||||
getCurrentRoute() {
|
||||
return this.location.pathname + this.location.search;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hostname of the request
|
||||
*/
|
||||
getRequestOrigin() {
|
||||
return this.location.origin;
|
||||
}
|
||||
}
|
||||
|
53
src/app/core/services/hard-redirect.service.spec.ts
Normal file
53
src/app/core/services/hard-redirect.service.spec.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { HardRedirectService } from './hard-redirect.service';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
const requestOrigin = 'http://dspace-angular-ui.dspace.com';
|
||||
|
||||
fdescribe('HardRedirectService', () => {
|
||||
const service: TestHardRedirectService = new TestHardRedirectService();
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
describe('when calling rewriteDownloadURL', () => {
|
||||
let originalValue;
|
||||
const relativePath = '/test/url/path';
|
||||
const testURL = environment.rest.baseUrl + relativePath;
|
||||
beforeEach(() => {
|
||||
originalValue = environment.rewriteDownloadUrls;
|
||||
});
|
||||
|
||||
it('it should return the same url when rewriteDownloadURL is false', () => {
|
||||
environment.rewriteDownloadUrls = false;
|
||||
expect(service.rewriteDownloadURL(testURL)).toEqual(testURL);
|
||||
});
|
||||
|
||||
it('it should replace part of the url when rewriteDownloadURL is true', () => {
|
||||
environment.rewriteDownloadUrls = true;
|
||||
expect(service.rewriteDownloadURL(testURL)).toEqual(requestOrigin + relativePath);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
environment.rewriteDownloadUrls = originalValue;
|
||||
})
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
class TestHardRedirectService extends HardRedirectService {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
redirect(url: string) {
|
||||
}
|
||||
|
||||
getCurrentRoute() {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
getRequestOrigin() {
|
||||
return requestOrigin;
|
||||
}
|
||||
}
|
@@ -1,4 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { URLCombiner } from '../url-combiner/url-combiner';
|
||||
|
||||
/**
|
||||
* Service to take care of hard redirects
|
||||
@@ -19,4 +21,20 @@ export abstract class HardRedirectService {
|
||||
* e.g. /search?page=1&query=open%20access&f.dateIssued.min=1980&f.dateIssued.max=2020
|
||||
*/
|
||||
abstract getCurrentRoute();
|
||||
|
||||
/**
|
||||
* Get the hostname of the request
|
||||
*/
|
||||
abstract getRequestOrigin();
|
||||
|
||||
public rewriteDownloadURL(originalUrl: string): string {
|
||||
if (environment.rewriteDownloadUrls) {
|
||||
let hostName = this.getRequestOrigin();
|
||||
let namespace = environment.rest.nameSpace;
|
||||
let rewrittenUrl = new URLCombiner(hostName, namespace).toString();
|
||||
return originalUrl.replace(environment.rest.baseUrl, rewrittenUrl);
|
||||
} else {
|
||||
return originalUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -7,8 +7,13 @@ describe('ServerHardRedirectService', () => {
|
||||
const mockResponse = jasmine.createSpyObj(['redirect', 'end']);
|
||||
|
||||
const service: ServerHardRedirectService = new ServerHardRedirectService(mockRequest, mockResponse);
|
||||
const origin = 'test-host';
|
||||
|
||||
beforeEach(() => {
|
||||
mockRequest.headers = {
|
||||
origin: 'test-host',
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
@@ -40,4 +45,12 @@ describe('ServerHardRedirectService', () => {
|
||||
expect(service.getCurrentRoute()).toEqual(mockRequest.originalUrl);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when requesting the origin', () => {
|
||||
|
||||
it('should return the location origin', () => {
|
||||
expect(service.getRequestOrigin()).toEqual(origin);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -7,12 +7,13 @@ import { HardRedirectService } from './hard-redirect.service';
|
||||
* Service for performing hard redirects within the server app module
|
||||
*/
|
||||
@Injectable()
|
||||
export class ServerHardRedirectService implements HardRedirectService {
|
||||
export class ServerHardRedirectService extends HardRedirectService {
|
||||
|
||||
constructor(
|
||||
@Inject(REQUEST) protected req: Request,
|
||||
@Inject(RESPONSE) protected res: Response,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,4 +60,11 @@ export class ServerHardRedirectService implements HardRedirectService {
|
||||
getCurrentRoute() {
|
||||
return this.req.originalUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hostname of the request
|
||||
*/
|
||||
getRequestOrigin() {
|
||||
return this.req.headers.origin;
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ import { FileDownloadLinkComponent } from './file-download-link.component';
|
||||
import { AuthService } from '../../core/auth/auth.service';
|
||||
import { FileService } from '../../core/shared/file.service';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
import { HardRedirectService } from '../../core/services/hard-redirect.service';
|
||||
|
||||
describe('FileDownloadLinkComponent', () => {
|
||||
let component: FileDownloadLinkComponent;
|
||||
@@ -23,13 +24,14 @@ describe('FileDownloadLinkComponent', () => {
|
||||
beforeEach(async(() => {
|
||||
init();
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ FileDownloadLinkComponent ],
|
||||
declarations: [FileDownloadLinkComponent],
|
||||
providers: [
|
||||
{ provide: AuthService, useValue: authService },
|
||||
{ provide: FileService, useValue: fileService }
|
||||
{ provide: FileService, useValue: fileService },
|
||||
{ provide: HardRedirectService, useValue: { rewriteDownloadURL: (a) => a } },
|
||||
]
|
||||
})
|
||||
.compileComponents();
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
|
@@ -2,6 +2,10 @@ import { Component, Input, OnInit } from '@angular/core';
|
||||
import { FileService } from '../../core/shared/file.service';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import { AuthService } from '../../core/auth/auth.service';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { hasValue } from '../empty.util';
|
||||
import { URLCombiner } from '../../core/url-combiner/url-combiner';
|
||||
import { HardRedirectService } from '../../core/services/hard-redirect.service';
|
||||
|
||||
@Component({
|
||||
selector: 'ds-file-download-link',
|
||||
@@ -30,10 +34,13 @@ export class FileDownloadLinkComponent implements OnInit {
|
||||
isAuthenticated$: Observable<boolean>;
|
||||
|
||||
constructor(private fileService: FileService,
|
||||
private authService: AuthService) { }
|
||||
private authService: AuthService,
|
||||
private redirectService: HardRedirectService) {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.isAuthenticated$ = this.authService.isAuthenticated();
|
||||
this.href = this.redirectService.rewriteDownloadURL(this.href);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,5 +51,4 @@ export class FileDownloadLinkComponent implements OnInit {
|
||||
this.fileService.downloadFile(this.href);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -31,4 +31,5 @@ export interface GlobalConfig extends Config {
|
||||
item: ItemPageConfig;
|
||||
collection: CollectionPageConfig;
|
||||
theme: Theme;
|
||||
rewriteDownloadUrls: boolean;
|
||||
}
|
||||
|
@@ -216,4 +216,5 @@ export const environment: GlobalConfig = {
|
||||
theme: {
|
||||
name: 'default',
|
||||
},
|
||||
rewriteDownloadUrls: false,
|
||||
};
|
||||
|
Reference in New Issue
Block a user