-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathdso-route.utils.ts
More file actions
106 lines (93 loc) · 3.54 KB
/
dso-route.utils.ts
File metadata and controls
106 lines (93 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {
hasValue,
isNotEmpty,
} from '@dspace/shared/utils/empty.util';
import { Collection } from '../../shared/collection.model';
import { Community } from '../../shared/community.model';
import { DSpaceObject } from '../../shared/dspace-object.model';
import { Item } from '../../shared/item.model';
import { URLCombiner } from '../../url-combiner/url-combiner';
import {
ENTITY_MODULE_PATH,
getBitstreamModuleRoute,
getCollectionModuleRoute,
getCommunityModuleRoute,
getItemModuleRoute,
} from '../core-routing-paths';
/**
* Regex to validate that a custom URL contains only safe latin-compatible characters.
* If the custom URL does not match this pattern, routing falls back to the item UUID.
*/
export const CUSTOM_URL_VALID_PATTERN = /^[.a-zA-Z0-9\-_]+$/;
export function getCollectionPageRoute(collectionId: string) {
return new URLCombiner(getCollectionModuleRoute(), collectionId).toString();
}
export function getCommunityPageRoute(communityId: string) {
return new URLCombiner(getCommunityModuleRoute(), communityId).toString();
}
/**
* Get the route to an item's page
* Depending on the item's entity type, the route will either start with /items or /entities.
*
* @param item The item to retrieve the route for
* @param ignoreCustomUrl When true, always use the UUID even if a valid custom URL exists
*/
export function getItemPageRoute(item: Item, ignoreCustomUrl = false) {
const type = item.firstMetadataValue('dspace.entity.type');
let url = item.uuid;
if (isNotEmpty(item.metadata) && item.hasMetadata('dspace.customurl') && !ignoreCustomUrl) {
url = item.firstMetadataValue('dspace.customurl');
}
return getEntityPageRoute(type, url);
}
export function getEntityPageRoute(entityType: string, itemId: string) {
if (isNotEmpty(entityType)) {
return new URLCombiner(`/${ENTITY_MODULE_PATH}`, encodeURIComponent(entityType.toLowerCase()), itemId).toString();
} else {
return new URLCombiner(getItemModuleRoute(), itemId).toString();
}
}
export function getDSORoute(dso: DSpaceObject): string {
if (hasValue(dso)) {
switch ((dso as any).type) {
case Community.type.value:
return getCommunityPageRoute(dso.uuid);
case Collection.type.value:
return getCollectionPageRoute(dso.uuid);
case Item.type.value:
return getItemPageRoute(dso as Item);
}
}
}
export function getBitstreamDownloadRoute(bitstream): string {
return new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString();
}
export function getBitstreamRequestACopyRoute(item, bitstream): { routerLink: string, queryParams: any } {
const url = new URLCombiner(getItemModuleRoute(), item.uuid, 'request-a-copy').toString();
return {
routerLink: url,
queryParams: {
bitstream: bitstream.uuid,
},
};
}
/**
* Get a bitstream download route with an access token (to provide direct access to a user) added as a query parameter
* @param bitstream the bitstream to download
* @param accessToken the access token, which should match an access_token in the requestitem table
*/
export function getBitstreamDownloadWithAccessTokenRoute(bitstream, accessToken): {
routerLink: string,
queryParams: any
} {
const url = new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString();
const options = {
routerLink: url,
queryParams: {},
};
// Only add the access token if it is not empty, otherwise keep valid empty query parameters
if (hasValue(accessToken)) {
options.queryParams = { accessToken: accessToken };
}
return options;
}