/demo/src/view-dir.adb

http://github.com/ThomasLocke/yolk · Ada · 93 lines · 56 code · 13 blank · 24 comment · 1 complexity · f4fd1e29b59b793efa60b5bd7cb1e7af MD5 · raw file

  1. -------------------------------------------------------------------------------
  2. -- --
  3. -- Copyright (C) 2010-, Thomas ¸cke --
  4. -- --
  5. -- This is free software; you can redistribute it and/or modify it --
  6. -- under terms of the GNU General Public License as published by the --
  7. -- Free Software Foundation; either version 3, or (at your option) any --
  8. -- later version. This library is distributed in the hope that it will be --
  9. -- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of --
  10. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
  11. -- You should have received a copy of the GNU General Public License and --
  12. -- a copy of the GCC Runtime Library Exception along with this program; --
  13. -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
  14. -- <http://www.gnu.org/licenses/>. --
  15. -- --
  16. -------------------------------------------------------------------------------
  17. with Ada.Directories;
  18. with AWS.Services.Directory;
  19. with Yolk.Configuration;
  20. with Yolk.Not_Found;
  21. package body View.Dir is
  22. function Directory_Browse_Response
  23. (Directory_Name : in String;
  24. Request : in AWS.Status.Data)
  25. return AWS.Response.Data;
  26. -- Generate the directory listing response.
  27. ---------------------------------
  28. -- Directory_Browse_Response --
  29. ---------------------------------
  30. function Directory_Browse_Response
  31. (Directory_Name : in String;
  32. Request : in AWS.Status.Data)
  33. return AWS.Response.Data
  34. is
  35. use AWS.Templates;
  36. use Yolk.Configuration;
  37. T : Translate_Set;
  38. begin
  39. T := AWS.Services.Directory.Browse
  40. (Directory_Name => Directory_Name,
  41. Request => Request);
  42. Insert (T, Assoc ("YOLK_VERSION", Yolk.Version));
  43. return Build_Response
  44. (Status_Data => Request,
  45. Template_File =>
  46. Config.Get (System_Templates_Path) & "/directory.tmpl",
  47. Translations => T);
  48. end Directory_Browse_Response;
  49. ---------------
  50. -- Generate --
  51. ---------------
  52. function Generate
  53. (Request : in AWS.Status.Data)
  54. return AWS.Response.Data
  55. is
  56. use Ada.Directories;
  57. use Yolk.Configuration;
  58. URL : constant String := AWS.Status.URI (Request);
  59. Resource : constant String (1 .. (URL'Length - 4))
  60. := URL (5 .. (URL'Length));
  61. -- Get rid of the /dir part of the URL
  62. Parent_Directory : constant String := Config.Get (WWW_Root);
  63. begin
  64. if not Exists (Parent_Directory & Resource) then
  65. return Not_Found.Generate (Request);
  66. end if;
  67. case Kind (Parent_Directory & Resource) is
  68. when Directory =>
  69. return Directory_Browse_Response
  70. (Directory_Name => Parent_Directory & Resource,
  71. Request => Request);
  72. when Ordinary_File =>
  73. return AWS.Response.File
  74. (Content_Type => AWS.Status.Content_Type (Request),
  75. Filename => Parent_Directory & Resource);
  76. when others =>
  77. return Not_Found.Generate (Request);
  78. end case;
  79. end Generate;
  80. end View.Dir;