2
0

httpuploads.lpr 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. program httpuploads;
  26. {$MODE DELPHI}
  27. uses
  28. SysUtils,
  29. BrookUtility,
  30. BrookStringMap,
  31. BrookHTTPUploads,
  32. BrookHTTPRequest,
  33. BrookHTTPResponse,
  34. BrookHTTPServer;
  35. const
  36. PAGE_FORM = Concat(
  37. '<html>',
  38. '<body>',
  39. '<form action="" method="post" enctype="multipart/form-data">',
  40. '<fieldset>',
  41. '<legend>Choose the files:</legend>',
  42. 'File 1: <input type="file" name="file1"/><br>',
  43. 'File 2: <input type="file" name="file2"/><br>',
  44. 'User 1: <input type="text" name="user1"/><br>',
  45. 'User 2: <input type="text" name="user2"/><br>',
  46. '<input type="submit"/>',
  47. '</fieldset>',
  48. '</form>',
  49. '</body>',
  50. '</html>'
  51. );
  52. PAGE_DONE = Concat(
  53. '<html>',
  54. '<head>',
  55. '<title>Uploads</title>',
  56. '</head>',
  57. '<body>',
  58. '<strong>Uploaded files:</strong><br>',
  59. '%s',
  60. '<strong>Users:</strong><br>',
  61. '%s',
  62. '</body>',
  63. '</html>'
  64. );
  65. CONTENT_TYPE = 'text/html; charset=utf-8';
  66. type
  67. THTTPServer = class(TBrookHTTPServer)
  68. protected
  69. procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  70. AResponse: TBrookHTTPResponse); override;
  71. end;
  72. procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  73. AResponse: TBrookHTTPResponse);
  74. var
  75. VUpload: TBrookHTTPUpload;
  76. VField: TBrookStringPair;
  77. VFile, VFileList, VUsersList, VError: string;
  78. begin
  79. if ARequest.IsUploading then
  80. begin
  81. VFileList := '<ol>';
  82. for VUpload in ARequest.Uploads do
  83. if VUpload.Save(False, VError) then
  84. VFileList := Concat(VFileList, '<li><a href="?file=', VUpload.Name, '">',
  85. VUpload.Name, '</a></li>')
  86. else
  87. VFileList := Concat(VFileList, '<li><font color="red">', VUpload.Name,
  88. ' - failed - ', VError, '</font></li>');
  89. VFileList := Concat(VFileList, '</ol>');
  90. VUsersList := '<ol>';
  91. for VField in ARequest.Fields do
  92. VUsersList := Concat(VUsersList, '<li>', VField.Value, '</li>');
  93. VUsersList := Concat(VUsersList, '</ol>');
  94. AResponse.SendFmt(PAGE_DONE, [VFileList, VUsersList], CONTENT_TYPE, 200);
  95. end
  96. else
  97. begin
  98. if ARequest.Params.TryValue('file', VFile) then
  99. AResponse.Download(Concat(UploadsDir, PathDelim, VFile))
  100. else
  101. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  102. end;
  103. end;
  104. begin
  105. with THTTPServer.Create(nil) do
  106. try
  107. UploadsDir := Concat(IncludeTrailingPathDelimiter(Sagui.TmpDir), 'uploads');
  108. NoFavicon := True;
  109. Open;
  110. if not Active then
  111. Exit;
  112. ForceDirectories(UploadsDir);
  113. WriteLn('Server running at http://localhost:', Port);
  114. ReadLn;
  115. finally
  116. Free;
  117. end;
  118. end.