forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlegacy-bitstream-url-redirect.guard.ts
More file actions
60 lines (58 loc) · 2.34 KB
/
Copy pathlegacy-bitstream-url-redirect.guard.ts
File metadata and controls
60 lines (58 loc) · 2.34 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
import { inject } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivateFn,
Router,
RouterStateSnapshot,
UrlTree,
} from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { APP_CONFIG, AppConfig } from '../../config/app-config.interface';
import { PAGE_NOT_FOUND_PATH } from '../app-routing-paths';
import { BitstreamDataService } from '../core/data/bitstream-data.service';
import { RemoteData } from '../core/data/remote-data';
import { HardRedirectService } from '../core/services/hard-redirect.service';
import { Bitstream } from '../core/shared/bitstream.model';
import { getFirstCompletedRemoteData } from '../core/shared/operators';
import { hasNoValue } from '../shared/empty.util';
/**
* Redirects to a bitstream based on the handle of the item, and the sequence id or the filename of the
* bitstream. In production mode the status code will also be set the status code to 301 marking it as a permanent URL
* redirect for bots.
*
* @returns Observable<UrlTree> Returns a URL to redirect the user to the new URL format
*/
export const legacyBitstreamURLRedirectGuard: CanActivateFn = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
bitstreamDataService: BitstreamDataService = inject(BitstreamDataService),
serverHardRedirectService: HardRedirectService = inject(HardRedirectService),
router: Router = inject(Router),
appConfig: AppConfig = inject(APP_CONFIG),
): Observable<UrlTree | boolean> => {
const prefix = route.params.prefix;
const suffix = route.params.suffix;
const filename = route.params.filename;
let sequenceId = route.params.sequence_id;
if (hasNoValue(sequenceId)) {
sequenceId = route.queryParams.sequenceId;
}
return bitstreamDataService.findByItemHandle(
`${prefix}/${suffix}`,
sequenceId,
filename,
).pipe(
getFirstCompletedRemoteData(),
map((rd: RemoteData<Bitstream>) => {
if (rd.hasSucceeded && !rd.hasNoContent) {
const nameSpace = appConfig.ui.nameSpace?.replace(/\/$/, '') || '';
const redirectUrl = new URL(nameSpace + `/bitstreams/${rd.payload.uuid}/download`, serverHardRedirectService.getCurrentOrigin()).href;
serverHardRedirectService.redirect(redirectUrl, 301);
return false;
} else {
return router.createUrlTree([PAGE_NOT_FOUND_PATH]);
}
})
);
};