forked from sirodcar/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonAsyncPages.pas
More file actions
217 lines (162 loc) · 6.75 KB
/
CommonAsyncPages.pas
File metadata and controls
217 lines (162 loc) · 6.75 KB
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
unit CommonAsyncPages;
interface
uses
managedthread, memoryfilestream, asyncclasses, backgroundthreads, exe, windows, webconfig, systemx, ExceptionsX, variants, webresource,WebString, stringx, stringx.ansi, CommonRequests, WebFunctions, Sysutils, RequestDispatcher, RequestInfo, classes, dataobject, serverinterface, MTDTInterface;
procedure AWRQ_AsyncTest(rqInfo: TRequestInfo; thread: TAsyncProcess);
procedure WRQ_AsyncTest(rqInfo: TrequestInfo);
procedure WRQ_GeneratePDFFormat(rqInfo: TRequestInfo);
procedure WRQ_AsyncStatus(rqInfo: TRequestInfo);
procedure WRQ_AsyncWait(rqInfo: TRequestInfo);
procedure WRQ_AsyncResult(rqINfo: TRequestINfo);
procedure WRQ_AsyncPDFResult(rqINfo: TRequestINfo);
implementation
function UniqueTempFilename(sTempFileLocation: ansistring): ansistring;
var
sPrefix, sTempFile: ansistring;
chrArray: array[0..250] of AnsiChar;
begin
// create a uniquely named temp file for the pdf file to avoid conflicts
// on multiple requests
sPrefix := 'tmp';
sTempFile := '';
GetTempFileName(
PChar(sTempFileLocation), // address of directory name for temporary file
PChar(sPrefix), // address of filename prefix
0, // number used to create temporary filename
@chrArray[0] // address of buffer that receives the new filename
);
sTempFile := chrArray;
result := sTempFile;
end;
procedure WRQ_AsyncPDFResult(rqINfo: TRequestINfo);
var
doResult: TDataObject;
begin
raise exception.Create('unimplmented');
end;
procedure WRQ_AsyncResult(rqINfo: TRequestINfo);
var
doResult: TDataObject;
begin
Notimplemented;
{
doResult := QueryMap(rqInfo, 'SELECT * from AsyncData where processid='+rQInfo.request['processid'], 'TdoAsyncProcessData', 'processid');
rqINfo.response.content.text := DecodewebString(doResult['ProcessData'].AsString);
rqInfo.response.NeedsProcessing := false;
}
end;
procedure WRQ_AsyncWait(rqInfo: TRequestInfo);
begin
rqINfo.request.default('pdf', 'false');
rqINfo.response.varpool['var_url'] := 'async_status.ms?sessionid='+rqInfo.sessionhash+'&processid='+rqInfo.request['processid'];
if lowercase(rqINfo.request['pdf']) = 'true' then
rqINfo.response.varpool['return_url'] := 'async_result.pdf?sessionid='+rqInfo.sessionhash+'&processid='+rqInfo.request['processid']+'&radioorientation='+rqInfo.request['orientation']
else
rqINfo.response.varpool['return_url'] := 'async_result.ms?sessionid='+rqInfo.sessionhash+'&processid='+rqInfo.request['processid'];
LoadWebResource(rqInfo, 'async_response.html');
end;
procedure AWRQ_AsyncTest(rqInfo: TRequestInfo; thread: TAsyncProcess);
var
t: integer;
begin
for t:= 0 to 30 do begin
rqInfo.response.content.add(inttostr(t)+'<BR>');
Sleep(300);
thread.UpdateStatus(t, 30, 'Counting... '+inttostr(t));
end;
end;
procedure WRQ_AsyncStatus(rqInfo: TRequestInfo);
var
status: TDataObject;
sResult: ansistring;
begin
NotImplemented;
{
try
status := QueryMap(rqInfo, 'SELECT * from async where processid='+rqInfo.Request['processid'], 'TdoAsyncProcess', vararrayof(['processid']));
sResult := '';
sResult := sResult + 'display_text=' + status['Name'].AsWebString;
sResult := sResult + '&progress_pos=' + status['StepNumber'].AsWebString;
sResult := sResult + '&progress_max=' + status['TotalSteps'].AsWebString;
if status['State'].AsVariant < 1 then
sResult := sResult + '&complete=1'
else
sResult := sResult + '&complete=0';
sResult := sResult + '&error=' + '';
sResult := sResult + '&loaded=1&nocrlfhere=';
rqInfo.response.contenttype := 'text/plain';
rqInfo.response.content.text := sResult;
except
On E:Exception do begin
sResult := 'loaded=1&junk=1234&complete=0&error='+E.Message;
rqInfo.response.content.text := sResult;
end;
end;
}
end;
procedure WRQ_GeneratePDFFormat(rqInfo: TRequestInfo);
var
objFileStream: TMemoryFileStream;
sProgram, sWorkingDirectory, sParameters, sDocument, sTempFile: ansistring;
sOrientation, sPageSize, sEncryption, sPassword: ansistring;
begin
rQInfo.request.default('radioorientation', 'portrait');
sOrientation := ' --' + rqInfo.Request.Params['radioOrientation'];
sPageSize := 'Letter';
if rqInfo.Request.HasParam('selectPageSize') and (rqInfo.Request.Params['selectPageSize']<>'Letter') then
sPageSize := rqInfo.Request.Params['selectPageSize'];
sEncryption := '';
sPassword := '';
if rqInfo.Request.HasParam('checkEncryption') then begin
sEncryption := ' --encryption ';
// require a password if encryption is checked
if rqInfo.Request.HasParam('textPassword') then begin
if rqInfo.Request.Params['textPassword'] <> '' then
sPassword := '--owner-password ' + rqInfo.Request.Params['textPassword'] + ' --user-password ' + rqInfo.Request.Params['textPassword']
else
raise ENewException.create(907,'Password cannot be blank','');
end else begin
raise ENewException.create(907,'Password cannot be blank','');
end;
end;
sProgram := slash(WebServerconfig.HTMLDocPath) + 'htmldoc.exe';
sWorkingDirectory := slash(WebServerConfig.ReportPDFTempFileLocation);
ForceDirectories(sWorkingDirectory);
sDocument := rqInfo.Request.Document;
system.Delete(sDocument, 1, 1);
sTempFile := UniqueTempFilename(sWorkingDirectory);
rqINfo.response.content.savetofile(sTempFile+'.html');
rqInfo.response.DeleteFileOnDestroy := sTempFile+'.html';
sParameters := '--webpage --compression=9 ' + sEncryption + sPassword + ' --browserwidth 900 --embedfonts --footer D./ --format pdf14 --outfile '
+ sTempFile + ' --quiet ' + sOrientation + ' --size ' + sPageSize + ' "' + sTempFile+'.html'
+ '" --permissions copy,print';
if RunProgramAndWait(sProgram, sParameters, sWorkingDirectory,true) then begin
rqInfo.Response.DeleteFileOnDestroy := sTempFile;
objFileStream := TMemoryFileStream.Create(sTempFile, fmOpenRead);
objFileStream.Seek(0,0);
rqInfo.response.ContentLength := objFileStream.size;
if IsIE(rqInfo) and IsPC(rqInfo) then
// rqInfo.Response.ContentType := 'application/x-pdf'
rqInfo.Response.ContentType := 'application/pdf'
else
rqInfo.response.contenttype := 'application/pdf';
rqInfo.Response.ContentStream := objFileStream;
end;
end;
procedure WRQ_AsyncTest(rqInfo: TRequestInfo);
var
ap: TAsyncProcess;
begin
ap := TPM.NeedThread<TAsyncProcess>(nil);
ap.rqInfo := rqInfo;
ap.CallbackProcedure := AWRQ_AsyncTest;
sendAsyncResponse(rqInfo, ap.ProcessID);
ap.Start;
end;
initialization
RQD.AddRequest('/async_test.ms', 'GET', wrq_asynctest);
RQD.AddRequest('/Async_Status.ms', 'GET', wrq_asyncstatus);
RQD.AddRequest('/Async_Wait.ms', 'GET', wrq_asyncWait);
RQD.AddRequest('/Async_Result.ms', 'GET', wrq_asyncResult);
RQD.AddRequest('/Async_Result.pdf', 'GET', wrq_AsyncPDFResult);
end.