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