]> Devoid-pointer.net GitWeb - Nine-Q.git/commitdiff
Add "handler_resources.adb" file
authorMichal Malý <madcatxster@devoid-pointer.net>
Mon, 15 Dec 2014 22:30:14 +0000 (23:30 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Mon, 15 Dec 2014 22:30:14 +0000 (23:30 +0100)
src/handlers/handler_resources.adb [new file with mode: 0644]

diff --git a/src/handlers/handler_resources.adb b/src/handlers/handler_resources.adb
new file mode 100644 (file)
index 0000000..d611642
--- /dev/null
@@ -0,0 +1,50 @@
+with Ada.Strings.Fixed;
+with AWS.Messages;
+with AWS.MIME;
+with AWS.Response;
+with AWS.Status;
+with Ada.Text_IO;
+
+package body Handler_Resources is
+
+  function File_Exists(Path: in String) return Boolean is
+    File: Ada.Text_IO.File_Type;
+  begin
+    begin
+      Ada.Text_IO.Open(File => File, Mode => Ada.Text_IO.In_File, Name => Path);
+      Ada.Text_IO.Close(File);
+      return True;
+    exception
+      when Ada.Text_IO.Name_Error =>
+       return False;
+    end;
+  end File_Exists;
+
+  function Handle(Request: AWS.Status.Data) return AWS.Response.Data is
+    use Ada.Strings.Fixed;
+
+    URI: constant String := AWS.Status.URI(Request);
+    Idx: Positive;
+  begin
+    Idx := Index(Source => URI, Pattern => "/", From => URI'Last, Going => Ada.Strings.Backward);
+    declare
+      Image_Path: constant String := "resources/" & URI(Idx + 1 .. URI'Last);
+    begin
+      if File_Exists(Image_Path) = False then
+       return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML,
+                                 Message_Body => "",
+                                 Status_Code => AWS.Messages.S404);
+      else
+       return AWS.Response.File(Content_Type => AWS.MIME.Image_Png,
+                                Filename => Image_Path,
+                                Status_Code => AWS.Messages.S200);
+      end if;
+    end;
+  end Handle;
+
+  function Callback return AWS.Dispatchers.Callback.Handler is
+  begin
+    return AWS.Dispatchers.Callback.Create(Handle'Access);
+  end Callback;
+
+end Handler_Resources;