forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem-page.component.ts
254 lines (222 loc) · 7.11 KB
/
item-page.component.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { map, take } from 'rxjs/operators';
import { ItemDataService } from '../../core/data/item-data.service';
import { RemoteData } from '../../core/data/remote-data';
import { Item } from '../../core/shared/item.model';
import { fadeInOut } from '../../shared/animations/fade';
import {
getAllSucceededRemoteDataPayload,
getAllSucceededRemoteListPayload,
} from '../../core/shared/operators';
import { ViewMode } from '../../core/shared/view-mode.model';
import { AuthService } from '../../core/auth/auth.service';
import { getItemPageRoute } from '../item-page-routing-paths';
import { isNotEmpty} from '../../shared/empty.util';
import { FeatureID } from '../../core/data/feature-authorization/feature-id';
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
import { redirectOn4xx } from '../../core/shared/authorized.operators';
import { RegistryService } from 'src/app/core/registry/registry.service';
import { MetadataBitstream } from 'src/app/core/metadata/metadata-bitstream.model';
import { BehaviorSubject, Observable } from 'rxjs';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
/**
* This component renders a simple item page.
* The route parameter 'id' is used to request the item it represents.
* All fields of the item that should be displayed, are defined in its template.
*/
@Component({
selector: 'ds-item-page',
styleUrls: ['./item-page.component.scss'],
templateUrl: './item-page.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
animations: [fadeInOut]
})
export class ItemPageComponent implements OnInit {
/**
* The item's id
*/
id: number;
/**
* The item wrapped in a remote-data object
*/
itemRD$: Observable<RemoteData<Item>>;
/**
* The view-mode we're currently on
*/
viewMode = ViewMode.StandalonePage;
/**
* Route to the item's page
*/
itemPageRoute$: Observable<string>;
/**
* handle of the specific item
*/
itemHandle: string;
/**
* handle of the specific item
*/
fileName: string;
/**
* determine to show download all zip button or not
*/
canDownloadAllFiles = false;
/**
* command for the download command feature
*/
command: string;
/**
* Whether the current user is an admin or not
*/
isAdmin$: Observable<boolean>;
/**
* If item is withdrawn and has new destination in the metadata: `dc.relation.isreplacedby`
*/
replacedTombstone = false;
/**
* If item is withdrawn and has/doesn't has reason of withdrawal
*/
withdrawnTombstone = false;
/**
* If download by command button is click, the command line will be shown
*/
isCommandLineVisible = false;
/**
* list of files uploaded by users to this item
*/
listOfFiles: MetadataBitstream[];
/**
* total size of list of files uploaded by users to this item
*/
totalFileSizes: string;
itemUrl: string;
canShowCurlDownload = false;
/**
* True if the item has files, false otherwise.
*/
hasFiles: BehaviorSubject<boolean> = new BehaviorSubject(false);
constructor(
protected route: ActivatedRoute,
private router: Router,
private items: ItemDataService,
private authService: AuthService,
private authorizationService: AuthorizationDataService,
protected registryService: RegistryService,
protected halService: HALEndpointService,
) {
}
/**
* Initialize instance variables
*/
ngOnInit(): void {
this.itemRD$ = this.route.data.pipe(
map((data) => data.dso as RemoteData<Item>),
redirectOn4xx(this.router, this.authService)
);
this.itemPageRoute$ = this.itemRD$.pipe(
getAllSucceededRemoteDataPayload(),
map((item) => getItemPageRoute(item))
);
this.processItem();
this.registryService
.getMetadataBitstream(this.itemHandle, 'ORIGINAL,TEXT,THUMBNAIL')
.pipe(getAllSucceededRemoteListPayload())
.subscribe((data: MetadataBitstream[]) => {
this.listOfFiles = data;
this.generateCurlCommand();
this.sumFileSizes();
});
}
/**
* Check if the item has files and assign the result into the `hasFiles` variable.
* */
private checkIfItemHasFiles(item: Item) {
const hasFilesMetadata = item.metadata?.['local.has.files']?.[0]?.value;
this.hasFiles.next(hasFilesMetadata !== 'no');
}
sumFileSizes() {
const sizeUnits = {
B: 1,
KB: 1000,
MB: 1000 * 1000,
GB: 1000 * 1000 * 1000,
TB: 1000 * 1000 * 1000 * 1000,
};
let totalBytes = this.listOfFiles.reduce((total, file) => {
const [valueStr, unit] = file.fileSize.split(' ');
const value = parseFloat(valueStr);
const bytes = value * sizeUnits[unit.toUpperCase()];
return total + bytes;
}, 0);
let finalUnit = 'B';
for (const unit of ['KB', 'MB', 'GB', 'TB']) {
if (totalBytes < 1000) {
break;
}
totalBytes /= 1000;
finalUnit = unit;
}
this.totalFileSizes = totalBytes.toFixed(2) + ' ' + finalUnit;
}
/**
* Process the tombstone of the Item and check if it has files or not.
*/
processItem() {
// if the item is withdrawn
let isWithdrawn = false;
// metadata value from `dc.relation.isreplacedby`
let isReplaced = '';
// load values from item
this.itemRD$.pipe(
take(1),
getAllSucceededRemoteDataPayload())
.subscribe((item: Item) => {
this.itemHandle = item.handle;
isWithdrawn = item.isWithdrawn;
isReplaced = item.metadata['dc.relation.isreplacedby']?.[0]?.value;
// check if the item has files
this.checkIfItemHasFiles(item);
});
// do not show tombstone for non withdrawn items
if (!isWithdrawn) {
return;
}
// for users navigate to the custom tombstone
// for admin stay on the item page with tombstone flag
this.isAdmin$ = this.authorizationService.isAuthorized(FeatureID.AdministratorOf);
this.isAdmin$.subscribe(isAdmin => {
// do not show tombstone for admin but show it for users
if (!isAdmin) {
if (isNotEmpty(isReplaced)) {
this.replacedTombstone = true;
} else {
this.withdrawnTombstone = true;
}
}
});
}
setCommandline() {
this.isCommandLineVisible = !this.isCommandLineVisible;
}
generateCurlCommand() {
const fileNames = this.listOfFiles.map((file: MetadataBitstream) => {
// Show `Download All Files` only if there are more files.
if (this.listOfFiles.length > 1) {
this.canDownloadAllFiles = true;
}
if (file.canPreview) {
this.canShowCurlDownload = true;
}
return file.name;
});
this.command = `curl --remote-name-all ` + this.halService.getRootHref() + `/core/bitstreams/handle/${
this.itemHandle
}/{${fileNames.join(',')}}`;
}
downloadFiles() {
this.itemRD$.pipe(
take(1),
getAllSucceededRemoteDataPayload())
.subscribe((item: Item) => void this.router.navigate([getItemPageRoute(item), 'download']));
}
}