Support for users, roles, restrictions.
[cocanwiki.git] / scripts / image.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: image.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 open Cocanwiki
16
17 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
18   let image = q#param "image" in
19   let is_thumbnail = q#param_true "thumbnail" in
20   let version =
21     try Some (int_of_string (q#param "version")) with Not_found -> None in
22
23   (* Get the image and its MIME type. *)
24   let what =
25     if not is_thumbnail then "image, mime_type"
26     else "thumbnail, tn_mime_type" in
27   let where, args =
28     match version with
29         None -> "hostid = ? and name = ?", [`Int hostid; `String image]
30       | Some version ->
31           "hostid = ? and (name = ? or name_deleted = ?) and id = ?",
32           [`Int hostid; `String image; `String image; `Int version] in
33
34   let sth = dbh#prepare_cached
35               ("select " ^ what ^ " from images where " ^ where) in
36   sth#execute args;
37
38   let data, mime_type =
39     match sth#fetch1 () with
40         [ `Binary data; `String mime_type ] ->
41           data, mime_type
42       | _ -> assert false in
43
44   if version <> None then
45     (* Set a medium-length expiry time on this resource. *)
46     Table.set (Request.headers_out r) "Expires" (Cgi_expires.expires_medium());
47
48   q#header ~content_type:mime_type ();
49   print_string r data
50
51 let () =
52   register_script run