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