PageRenderTime 34ms CodeModel.GetById 11ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/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 Lø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
18with Ada.Directories;
19with AWS.Services.Directory;
20with Yolk.Configuration;
21with Yolk.Not_Found;
22
23package body View.Dir is
24
25   function Directory_Browse_Response
26     (Directory_Name : in String;
27      Request        : in AWS.Status.Data)
28      return AWS.Response.Data;
29   --  Generate the directory listing response.
30
31   ---------------------------------
32   --  Directory_Browse_Response  --
33   ---------------------------------
34
35   function Directory_Browse_Response
36     (Directory_Name : in String;
37      Request        : in AWS.Status.Data)
38      return AWS.Response.Data
39   is
40      use AWS.Templates;
41      use Yolk.Configuration;
42
43      T : Translate_Set;
44   begin
45      T := AWS.Services.Directory.Browse
46        (Directory_Name => Directory_Name,
47         Request        => Request);
48
49      Insert (T, Assoc ("YOLK_VERSION", Yolk.Version));
50
51      return Build_Response
52        (Status_Data   => Request,
53         Template_File =>
54           Config.Get (System_Templates_Path) & "/directory.tmpl",
55         Translations  => T);
56   end Directory_Browse_Response;
57
58   ---------------
59   --  Generate --
60   ---------------
61
62   function Generate
63     (Request : in AWS.Status.Data)
64      return AWS.Response.Data
65   is
66      use Ada.Directories;
67      use Yolk.Configuration;
68
69      URL              : constant String := AWS.Status.URI (Request);
70      Resource         : constant String (1 .. (URL'Length - 4))
71        := URL (5 .. (URL'Length));
72      --  Get rid of the /dir part of the URL
73      Parent_Directory : constant String := Config.Get (WWW_Root);
74   begin
75      if not Exists (Parent_Directory & Resource) then
76         return Not_Found.Generate (Request);
77      end if;
78
79      case Kind (Parent_Directory & Resource) is
80         when Directory =>
81            return Directory_Browse_Response
82              (Directory_Name => Parent_Directory & Resource,
83               Request        => Request);
84         when Ordinary_File =>
85            return AWS.Response.File
86              (Content_Type  => AWS.Status.Content_Type (Request),
87               Filename      => Parent_Directory & Resource);
88         when others =>
89            return Not_Found.Generate (Request);
90      end case;
91   end Generate;
92
93end View.Dir;