All new templates. Site menu displays basically everywhere.
[cocanwiki.git] / scripts / file.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: file.ml,v 1.4 2004/09/09 09:35:33 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13
14 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
15   let name = q#param "name" in
16   let version =
17     try Some (int_of_string (q#param "version")) with Not_found -> None in
18
19   (* Get the file and its MIME type. *)
20   let where, args =
21     match version with
22         None -> "hostid = ? and name = ?", [`Int hostid; `String name]
23       | Some version ->
24           "hostid = ? and (name = ? or name_deleted = ?) and id = ?",
25           [`Int hostid; `String name; `String name; `Int version] in
26
27   let sth =
28     dbh#prepare_cached ("select content, mime_type from files
29                           where " ^ where) in
30   sth#execute args;
31
32   let data, mime_type =
33     match sth#fetch1 () with
34         [ `Binary data; `String mime_type ] ->
35           data, mime_type
36       | _ -> assert false in
37
38   if version <> None then
39     (* Set a medium-length expiry time on this resource. *)
40     Table.set (Request.headers_out r) "Expires" (Cgi_expires.expires_medium());
41
42   q#header ~content_type:mime_type ();
43   print_string r data
44
45 let () =
46   register_script run