Skip to content

Commit f1fa6ff

Browse files
nitrosxJunjiequan
authored andcommitted
added downloadable json datafile action
1 parent 83bd5d0 commit f1fa6ff

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

Diff for: src/app/datasets/datafiles-actions/datafiles-action.component.ts

+38-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { UsersService } from "@scicatproject/scicat-sdk-ts-angular";
1010
import { ActionConfig, ActionDataset } from "./datafiles-action.interfaces";
1111
import { DataFiles_File } from "datasets/datafiles/datafiles.interfaces";
1212
import { AuthService } from "shared/services/auth/auth.service";
13+
import { v4 } from "uuid";
1314

1415
@Component({
1516
selector: "datafiles-action",
@@ -111,8 +112,8 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
111112
perform_action() {
112113
const action_type = this.actionConfig.type || "form";
113114
switch (action_type) {
114-
case "json":
115-
return this.type_json();
115+
case "json-download":
116+
return this.type_json_download();
116117
case "form":
117118
default:
118119
return this.type_form();
@@ -161,23 +162,27 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
161162
return true;
162163
}
163164

164-
type_json() {
165+
type_json_download() {
165166
let payload = "";
166167
if (this.actionConfig.payload) {
167168
payload = this.actionConfig.payload
168169
.replace(/{{ auth_token }}/, `Bearer ${this.authService.getToken().id}`)
169170
.replace(/{{ jwt }}/, this.jwt)
170171
.replace(/{{ datasetPid }}/, this.actionDataset.pid)
171172
.replace(/{{ sourceFolder }}/, this.actionDataset.sourceFolder)
172-
.replace(/{{ filesPath }}/, JSON.stringify(
173-
this.files.filter(
174-
(item) =>
175-
this.actionConfig.files === "all" ||
176-
(this.actionConfig.files === "selected" && item.selected),
177-
)
178-
.map((item) => item.path)));
179-
}
180-
else {
173+
.replace(
174+
/{{ filesPath }}/,
175+
JSON.stringify(
176+
this.files
177+
.filter(
178+
(item) =>
179+
this.actionConfig.files === "all" ||
180+
(this.actionConfig.files === "selected" && item.selected),
181+
)
182+
.map((item) => item.path),
183+
),
184+
);
185+
} else {
181186
const data = {
182187
auth_token: `Bearer ${this.authService.getToken().id}`,
183188
jwt: this.jwt,
@@ -194,15 +199,32 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
194199
payload = JSON.stringify(data);
195200
}
196201

202+
const filename = this.actionConfig.filename.replace(/{{ uuid }}/, v4());
203+
197204
fetch(this.actionConfig.url, {
198205
method: this.actionConfig.method || "POST",
199-
headers: {
206+
headers: {
200207
"Content-Type": "application/json",
201208
},
202209
body: payload,
203-
}).then((response) => {
204-
console.log(response.json());
205-
});
210+
})
211+
.then((response) => response.blob())
212+
.then((blob) => URL.createObjectURL(blob))
213+
.then((url) => {
214+
const a = document.createElement("a");
215+
a.href = url;
216+
a.download = filename;
217+
a.click();
218+
URL.revokeObjectURL(url);
219+
// Object.assign(document.createElement("a"), {
220+
// url,
221+
// download: "filename.csv",
222+
// }).click();
223+
});
224+
// .then((url) => {
225+
// window.open(url, "_blank");
226+
// URL.revokeObjectURL(url);
227+
// });
206228

207229
return true;
208230
}

Diff for: src/app/datasets/datafiles-actions/datafiles-action.interfaces.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface ActionConfig {
22
id: string;
3+
description?: string;
34
order: number;
45
label: string;
56
files: string;
@@ -13,6 +14,7 @@ export interface ActionConfig {
1314
enabled?: string;
1415
disabled?: string;
1516
payload?: string;
17+
filename?: string;
1618
}
1719

1820
export interface ActionDataset {

Diff for: src/assets/config.json

+18-6
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,52 @@
5757
"datafilesActions": [
5858
{
5959
"id": "eed8efec-4354-11ef-a3b5-d75573a5d37f",
60-
"order": 4,
60+
"order": 5,
6161
"label": "Download All",
6262
"files": "all",
6363
"mat_icon": "download",
6464
"type": "form",
65-
"url": "https://www.scicat.info/download/all",
65+
"url": "https://zip.scicatproject.org/download/all",
6666
"target": "_blank",
6767
"enabled": "#SizeLimit",
6868
"authorization": ["#datasetAccess", "#datasetPublic"]
6969
},
7070
{
7171
"id": "3072fafc-4363-11ef-b9f9-ebf568222d26",
72-
"order": 3,
72+
"order": 4,
7373
"label": "Download Selected",
7474
"files": "selected",
7575
"mat_icon": "download",
7676
"type": "form",
77-
"url": "https://www.scicat.info/download/selected",
77+
"url": "https://zip.scicatproject.org/download/selected",
7878
"target": "_blank",
7979
"enabled": "#Selected && #SizeLimit",
8080
"authorization": ["#datasetAccess", "#datasetPublic"]
8181
},
8282
{
8383
"id": "4f974f0e-4364-11ef-9c63-03d19f813f4e",
8484
"order": 2,
85-
"label": "Notebook All",
85+
"label": "Notebook All (Form)",
8686
"files": "all",
8787
"icon": "/assets/icons/jupyter_logo.png",
8888
"type": "form",
8989
"url": "https://www.scicat.info/notebook/all",
9090
"target": "_blank",
9191
"authorization": ["#datasetAccess", "#datasetPublic"]
9292
},
93+
{
94+
"id": "0cd5b592-0b1a-11f0-a42c-23e177127ee7",
95+
"order": 3,
96+
"label": "Notebook All (Download JSON)",
97+
"files": "all",
98+
"type": "json-download",
99+
"icon": "/assets/icons/jupyter_logo.png",
100+
"url": "https://www.sciwyrm.info/notebook",
101+
"target": "_blank",
102+
"authorization": ["#datasetAccess", "#datasetPublic"],
103+
"payload": "{\"template_id\":\"c975455e-ede3-11ef-94fb-138c9cd51fc0\",\"parameters\":{\"dataset\":\"{{ datasetPid }}\",\"directory\":\"{{ sourceFolder }}\",\"files\": {{ filesPath }},\"jwt\":\"{{ jwt }}\",\"scicat_url\":\"https://staging.scicat.ess.url\",\"file_server_url\":\"sftserver2.esss.dk\",\"file_server_port\":\"22\"}}",
104+
"filename": "{{ uuid }}.ipynb"
105+
},
93106
{
94107
"id": "fa3ce6ee-482d-11ef-95e9-ff2c80dd50bd",
95108
"order": 1,
@@ -222,7 +235,6 @@
222235
},
223236
"proposal" : {
224237
"General Information": "Proposal Information",
225-
//"Title" : "Proposal Title",
226238
"Abstract" : "Abstract",
227239
"Proposal Id" : "Proposal Id",
228240
"Proposal Type" : "Proposal Type",

0 commit comments

Comments
 (0)