958292bf6b88c6a7d7068874e01ee06e20862ad2
[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.1 2004/09/07 10:14:09 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 image = q#param "image" in
18   let is_thumbnail = q#param_true "thumbnail" in
19   let version =
20     try Some (int_of_string (q#param "version")) with Not_found -> None in
21
22   (* Get the image and its MIME type. *)
23   let what =
24     if not is_thumbnail then "image, mime_type"
25     else "thumbnail, tn_mime_type" in
26   let where, args =
27     match version with
28         None -> "hostid = ? and name = ?", [`Int hostid; `String image]
29       | Some version ->
30           "hostid = ? and (name = ? or name_deleted = ?) and id = ?",
31           [`Int hostid; `String image; `String image; `Int version] in
32
33   let sth = dbh#prepare_cached
34               ("select " ^ what ^ " from images where " ^ where) in
35   sth#execute args;
36
37   let data, mime_type =
38     match sth#fetch1 () with
39         [ `Binary data; `String mime_type ] ->
40           data, mime_type
41       | _ -> assert false in
42
43   if version <> None then
44     (* Set a medium-length expiry time on this resource. *)
45     Table.set (Request.headers_out r) "Expires" (Cgi_expires.expires_medium());
46
47   q#header ~content_type:mime_type ();
48   print_string r data
49
50 let () =
51   register_script run