@@ -10,6 +10,7 @@ import { UsersService } from "@scicatproject/scicat-sdk-ts-angular";
10
10
import { ActionConfig , ActionDataset } from "./datafiles-action.interfaces" ;
11
11
import { DataFiles_File } from "datasets/datafiles/datafiles.interfaces" ;
12
12
import { AuthService } from "shared/services/auth/auth.service" ;
13
+ import { v4 } from "uuid" ;
13
14
14
15
@Component ( {
15
16
selector : "datafiles-action" ,
@@ -111,8 +112,8 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
111
112
perform_action ( ) {
112
113
const action_type = this . actionConfig . type || "form" ;
113
114
switch ( action_type ) {
114
- case "json" :
115
- return this . type_json ( ) ;
115
+ case "json-download " :
116
+ return this . type_json_download ( ) ;
116
117
case "form" :
117
118
default :
118
119
return this . type_form ( ) ;
@@ -161,23 +162,27 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
161
162
return true ;
162
163
}
163
164
164
- type_json ( ) {
165
+ type_json_download ( ) {
165
166
let payload = "" ;
166
167
if ( this . actionConfig . payload ) {
167
168
payload = this . actionConfig . payload
168
169
. replace ( / { { a u t h _ t o k e n } } / , `Bearer ${ this . authService . getToken ( ) . id } ` )
169
170
. replace ( / { { j w t } } / , this . jwt )
170
171
. replace ( / { { d a t a s e t P i d } } / , this . actionDataset . pid )
171
172
. replace ( / { { s o u r c e F o l d e r } } / , this . actionDataset . sourceFolder )
172
- . replace ( / { { f i l e s P a t h } } / , 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
+ / { { f i l e s P a t h } } / ,
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 {
181
186
const data = {
182
187
auth_token : `Bearer ${ this . authService . getToken ( ) . id } ` ,
183
188
jwt : this . jwt ,
@@ -194,15 +199,32 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
194
199
payload = JSON . stringify ( data ) ;
195
200
}
196
201
202
+ const filename = this . actionConfig . filename . replace ( / { { u u i d } } / , v4 ( ) ) ;
203
+
197
204
fetch ( this . actionConfig . url , {
198
205
method : this . actionConfig . method || "POST" ,
199
- headers : {
206
+ headers : {
200
207
"Content-Type" : "application/json" ,
201
208
} ,
202
209
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
+ // });
206
228
207
229
return true ;
208
230
}
0 commit comments