mp_streamfile.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Streams a file to _webout according to content type
4  @details Will set headers using appropriate functions per the server type
5  (Viya, EBI, [SASjs Server](https://github.com/sasjs/server)) and stream
6  content using mp_binarycopy().
7 
8  Usage:
9 
10  filename mc url
11  "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
12  %inc mc;
13 
14  %mp_streamfile(contenttype=csv,inloc=/some/where.txt,outname=myfile.txt)
15 
16  @param [in] contenttype= (TEXT) Supported:
17  @li CSV
18  @li EXCEL
19  @li MARKDOWN
20  @li TEXT
21  @li ZIP
22  Feel free to submit PRs to support more mime types! The official list is
23  here: https://www.iana.org/assignments/media-types/media-types.xhtml
24  @param [in] inloc= /path/to/file.ext to be sent
25  @param [in] inref= fileref of file to be sent (if provided, overrides `inloc`)
26  @param [in] iftrue= (1=1) Provide a condition under which to execute.
27  @param [out] outname= the name of the file, as downloaded by the browser
28  @param [out] outref= (_webout) The destination where the file should be
29  streamed.
30 
31  <h4> SAS Macros </h4>
32  @li mf_getplatform.sas
33  @li mfs_httpheader.sas
34  @li mp_binarycopy.sas
35 
36  @author Allan Bowe
37 
38 **/
39 
40 %macro mp_streamfile(
41  contenttype=TEXT
42  ,inloc=
43  ,inref=0
44  ,iftrue=%str(1=1)
45  ,outname=
46  ,outref=_webout
47 )/*/STORE SOURCE*/;
48 
49 %if not(%eval(%unquote(&iftrue))) %then %return;
50 
51 %let contentype=%upcase(&contenttype);
52 %let outref=%upcase(&outref);
53 %local platform; %let platform=%mf_getplatform();
54 
55 /**
56  * check engine type to avoid the below err message:
57  * > Function is only valid for filerefs using the CACHE access method.
58  */
59 %local streamweb;
60 %let streamweb=0;
61 data _null_;
62  set sashelp.vextfl(where=(upcase(fileref)="&outref"));
63  if xengine='STREAM' then call symputx('streamweb',1,'l');
64 run;
65 
66 %if &contentype=CSV %then %do;
67  %if (&platform=SASMETA and &streamweb=1) %then %do;
68  data _null_;
69  rc=stpsrv_header('Content-Type','application/csv');
70  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
71  run;
72  %end;
73  %else %if &platform=SASVIYA %then %do;
74  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
75  contenttype='application/csv'
76  contentdisp="attachment; filename=&outname";
77  %end;
78  %else %if &platform=SASJS %then %do;
79  %mfs_httpheader(Content-Type,application/csv)
80  %mfs_httpheader(Content-disposition,%str(attachment; filename=&outname))
81  %end;
82 %end;
83 %else %if &contentype=EXCEL %then %do;
84  /* suitable for XLS format */
85  %if (&platform=SASMETA and &streamweb=1) %then %do;
86  data _null_;
87  rc=stpsrv_header('Content-Type','application/vnd.ms-excel');
88  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
89  run;
90  %end;
91  %else %if &platform=SASVIYA %then %do;
92  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
93  contenttype='application/vnd.ms-excel'
94  contentdisp="attachment; filename=&outname";
95  %end;
96  %else %if &platform=SASJS %then %do;
97  %mfs_httpheader(Content-Type,application/vnd.ms-excel)
98  %mfs_httpheader(Content-disposition,%str(attachment; filename=&outname))
99  %end;
100 %end;
101 %else %if &contentype=GIF or &contentype=JPEG or &contentype=PNG %then %do;
102  %if (&platform=SASMETA and &streamweb=1) %then %do;
103  data _null_;
104  rc=stpsrv_header('Content-Type',"image/%lowcase(&contenttype)");
105  run;
106  %end;
107  %else %if &platform=SASVIYA %then %do;
108  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI"
109  contenttype="image/%lowcase(&contenttype)";
110  %end;
111  %else %if &platform=SASJS %then %do;
112  %mfs_httpheader(Content-Type,image/%lowcase(&contenttype))
113  %end;
114 %end;
115 %else %if &contentype=HTML or &contenttype=MARKDOWN %then %do;
116  %if (&platform=SASMETA and &streamweb=1) %then %do;
117  data _null_;
118  rc=stpsrv_header('Content-Type',"text/%lowcase(&contenttype)");
119  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
120  run;
121  %end;
122  %else %if &platform=SASVIYA %then %do;
123  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json"
124  contenttype="text/%lowcase(&contenttype)"
125  contentdisp="attachment; filename=&outname";
126  %end;
127  %else %if &platform=SASJS %then %do;
128  %mfs_httpheader(Content-Type,text/%lowcase(&contenttype))
129  %mfs_httpheader(Content-disposition,%str(attachment; filename=&outname))
130  %end;
131 %end;
132 %else %if &contentype=TEXT %then %do;
133  %if (&platform=SASMETA and &streamweb=1) %then %do;
134  data _null_;
135  rc=stpsrv_header('Content-Type','application/text');
136  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
137  run;
138  %end;
139  %else %if &platform=SASVIYA %then %do;
140  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
141  contenttype='application/text'
142  contentdisp="attachment; filename=&outname";
143  %end;
144  %else %if &platform=SASJS %then %do;
145  %mfs_httpheader(Content-Type,application/text)
146  %mfs_httpheader(Content-disposition,%str(attachment; filename=&outname))
147  %end;
148 %end;
149 %else %if &contentype=WOFF or &contentype=WOFF2 or &contentype=TTF %then %do;
150  %if (&platform=SASMETA and &streamweb=1) %then %do;
151  data _null_;
152  rc=stpsrv_header('Content-Type',"font/%lowcase(&contenttype)");
153  run;
154  %end;
155  %else %if &platform=SASVIYA %then %do;
156  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI"
157  contenttype="font/%lowcase(&contenttype)";
158  %end;
159  %else %if &platform=SASJS %then %do;
160  %mfs_httpheader(Content-Type,font/%lowcase(&contenttype))
161  %end;
162 %end;
163 %else %if &contentype=XLSX %then %do;
164  %if (&platform=SASMETA and &streamweb=1) %then %do;
165  data _null_;
166  rc=stpsrv_header('Content-Type',
167  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
168  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
169  run;
170  %end;
171  %else %if &platform=SASVIYA %then %do;
172  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
173  contenttype=
174  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
175  contentdisp="attachment; filename=&outname";
176  %end;
177  %else %if &platform=SASJS %then %do;
178  %mfs_httpheader(Content-Type
179  ,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
180  )
181  %mfs_httpheader(Content-disposition,%str(attachment; filename=&outname))
182  %end;
183 %end;
184 %else %if &contentype=ZIP %then %do;
185  %if (&platform=SASMETA and &streamweb=1) %then %do;
186  data _null_;
187  rc=stpsrv_header('Content-Type','application/zip');
188  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
189  run;
190  %end;
191  %else %if &platform=SASVIYA %then %do;
192  filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip'
193  contenttype='application/zip'
194  contentdisp="attachment; filename=&outname";
195  %end;
196  %else %if &platform=SASJS %then %do;
197  %mfs_httpheader(Content-Type,application/zip)
198  %mfs_httpheader(Content-disposition,%str(attachment; filename=&outname))
199  %end;
200 %end;
201 %else %do;
202  %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!;
203 %end;
204 
205 %if &inref ne 0 %then %do;
206  %mp_binarycopy(inref=&inref,outref=&outref)
207 %end;
208 %else %do;
209  %mp_binarycopy(inloc="&inloc",outref=&outref)
210 %end;
211 
212 %mend mp_streamfile;