Support for users, roles, restrictions.
[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.2 2004/09/07 13:40:10 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Merjisforwiki
13
14 open Cocanwiki
15
16 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
17   let name = q#param "name" in
18   let version =
19     try Some (int_of_string (q#param "version")) with Not_found -> None in
20
21   (* Get the file and its MIME type. *)
22   let where, args =
23     match version with
24         None -> "hostid = ? and name = ?", [`Int hostid; `String name]
25       | Some version ->
26           "hostid = ? and (name = ? or name_deleted = ?) and id = ?",
27           [`Int hostid; `String name; `String name; `Int version] in
28
29   let sth =
30     dbh#prepare_cached ("select content, mime_type from files
31                           where " ^ where) in
32   sth#execute args;
33
34   let data, mime_type =
35     match sth#fetch1 () with
36         [ `Binary data; `String mime_type ] ->
37           data, mime_type
38       | _ -> assert false in
39
40   if version <> None then
41     (* Set a medium-length expiry time on this resource. *)
42     Table.set (Request.headers_out r) "Expires" (Cgi_expires.expires_medium());
43
44   q#header ~content_type:mime_type ();
45   print_string r data
46
47 let () =
48   register_script run