HTTPUpload_frMain.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit HTTPUpload_frMain;
  26. interface
  27. uses
  28. System.SysUtils,
  29. {$IFDEF ANDROID}
  30. System.IOUtils,
  31. {$ENDIF}
  32. System.UITypes,
  33. System.Classes,
  34. System.Actions,
  35. FMX.Types,
  36. FMX.ActnList,
  37. FMX.Graphics,
  38. FMX.Controls,
  39. FMX.StdCtrls,
  40. FMX.Edit,
  41. FMX.EditBox,
  42. FMX.NumberBox,
  43. FMX.DialogService,
  44. FMX.Forms,
  45. FMX.Controls.Presentation,
  46. BrookUtility,
  47. BrookStringMap,
  48. BrookHandledClasses,
  49. BrookHTTPUploads,
  50. BrookHTTPRequest,
  51. BrookHTTPResponse,
  52. BrookHTTPServer,
  53. Utility;
  54. type
  55. TfrMain = class(TForm)
  56. lbPort: TLabel;
  57. edPort: TNumberBox;
  58. btStart: TButton;
  59. btStop: TButton;
  60. lbLink: TLabel;
  61. alMain: TActionList;
  62. acStart: TAction;
  63. acStop: TAction;
  64. BrookHTTPServer1: TBrookHTTPServer;
  65. pnTop: TPanel;
  66. procedure acStartExecute(Sender: TObject);
  67. procedure acStopExecute(Sender: TObject);
  68. procedure lbLinkMouseEnter(Sender: TObject);
  69. procedure lbLinkMouseLeave(Sender: TObject);
  70. procedure lbLinkClick(Sender: TObject);
  71. procedure BrookHTTPServer1Request(ASender: TObject;
  72. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  73. procedure BrookHTTPServer1RequestError(ASender: TObject;
  74. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  75. AException: Exception);
  76. procedure BrookHTTPServer1Start(Sender: TObject);
  77. procedure BrookHTTPServer1Stop(Sender: TObject);
  78. procedure edPortChange(Sender: TObject);
  79. procedure edPortChangeTracking(Sender: TObject);
  80. procedure FormShow(Sender: TObject);
  81. public
  82. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  83. end;
  84. const
  85. PAGE_FORM = Concat(
  86. '<html>',
  87. '<body>',
  88. '<form action="" method="post" enctype="multipart/form-data">',
  89. '<fieldset>',
  90. '<legend>Choose the files:</legend>',
  91. 'File 1: <input type="file" name="file1"/><br>',
  92. 'File 2: <input type="file" name="file2"/><br>',
  93. 'User 1: <input type="text" name="user1"/><br>',
  94. 'User 2: <input type="text" name="user2"/><br>',
  95. '<input type="submit"/>',
  96. '</fieldset>',
  97. '</form>',
  98. '</body>',
  99. '</html>'
  100. );
  101. PAGE_DONE = Concat(
  102. '<html>',
  103. '<head>',
  104. '<title>Uploads</title>',
  105. '</head>',
  106. '<body>',
  107. '<strong>Uploaded files:</strong><br>',
  108. '%s',
  109. '<strong>Users:</strong><br>',
  110. '%s',
  111. '</body>',
  112. '</html>'
  113. );
  114. CONTENT_TYPE = 'text/html; charset=utf-8';
  115. var
  116. frMain: TfrMain;
  117. implementation
  118. {$R *.fmx}
  119. procedure TfrMain.FormShow(Sender: TObject);
  120. begin
  121. if BrookHTTPServer1.UploadsDir.IsEmpty then
  122. BrookHTTPServer1.UploadsDir :=
  123. {$IFDEF ANDROID}TPath.GetTempPath{$ELSE}Sagui.TmpDir{$ENDIF};
  124. end;
  125. procedure TfrMain.UpdateControls;
  126. begin
  127. if Application.Terminated then
  128. Exit;
  129. if BrookHTTPServer1.Active then
  130. edPort.Value := BrookHTTPServer1.Port
  131. else
  132. BrookHTTPServer1.Port := edPort.Text.ToInteger;
  133. lbLink.Text := Concat('http://localhost:', edPort.Value.ToString);
  134. acStart.Enabled := not BrookHTTPServer1.Active;
  135. acStop.Enabled := not acStart.Enabled;
  136. edPort.Enabled := acStart.Enabled;
  137. lbLink.Enabled := not acStart.Enabled;
  138. end;
  139. procedure TfrMain.acStartExecute(Sender: TObject);
  140. begin
  141. BrookHTTPServer1.Open;
  142. end;
  143. procedure TfrMain.acStopExecute(Sender: TObject);
  144. begin
  145. BrookHTTPServer1.Close;
  146. end;
  147. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  148. begin
  149. lbLink.Font.Style := lbLink.Font.Style + [TFontStyle.fsUnderline];
  150. end;
  151. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  152. begin
  153. lbLink.Font.Style := lbLink.Font.Style - [TFontStyle.fsUnderline];
  154. end;
  155. procedure TfrMain.lbLinkClick(Sender: TObject);
  156. begin
  157. Utility.OpenURL(lbLink.Text);
  158. end;
  159. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  160. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  161. var
  162. VUpload: TBrookHTTPUpload;
  163. VField: TBrookStringPair;
  164. VFile, VFileList, VUsersList, VError: string;
  165. begin
  166. if ARequest.IsUploading then
  167. begin
  168. VFileList := '<ol>';
  169. for VUpload in ARequest.Uploads do
  170. if VUpload.Save(False, VError) then
  171. VFileList := Concat(VFileList, '<li><a href="?file=', VUpload.Name, '">',
  172. VUpload.Name, '</a></li>')
  173. else
  174. VFileList := Concat(VFileList, '<li><font color="red">', VUpload.Name,
  175. ' - failed - ', VError, '</font></li>');
  176. VFileList := Concat(VFileList, '</ol>');
  177. VUsersList := '<ol>';
  178. for VField in ARequest.Fields do
  179. VUsersList := Concat(VUsersList, '<li>', VField.Value, '</li>');
  180. VUsersList := Concat(VUsersList, '</ol>');
  181. AResponse.SendFmt(PAGE_DONE, [VFileList, VUsersList], CONTENT_TYPE, 200);
  182. end
  183. else
  184. begin
  185. if ARequest.Params.TryValue('file', VFile) then
  186. AResponse.Download(
  187. Concat(BrookHTTPServer1.UploadsDir, PathDelim, VFile))
  188. else
  189. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  190. end;
  191. end;
  192. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  193. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  194. AException: Exception);
  195. begin
  196. AResponse.SendFmt(
  197. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  198. [AException.Message], 'text/html; charset=utf-8', 500);
  199. end;
  200. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  201. begin
  202. UpdateControls;
  203. end;
  204. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  205. begin
  206. UpdateControls;
  207. end;
  208. procedure TfrMain.edPortChange(Sender: TObject);
  209. begin
  210. UpdateControls;
  211. end;
  212. procedure TfrMain.edPortChangeTracking(Sender: TObject);
  213. begin
  214. UpdateControls;
  215. end;
  216. end.