(* COCANWIKI scripts. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: file.ml,v 1.3 2004/09/07 14:58:34 rich Exp $ *) open Apache open Registry open Cgi open Printf open Cocanwiki let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ = let name = q#param "name" in let version = try Some (int_of_string (q#param "version")) with Not_found -> None in (* Get the file and its MIME type. *) let where, args = match version with None -> "hostid = ? and name = ?", [`Int hostid; `String name] | Some version -> "hostid = ? and (name = ? or name_deleted = ?) and id = ?", [`Int hostid; `String name; `String name; `Int version] in let sth = dbh#prepare_cached ("select content, mime_type from files where " ^ where) in sth#execute args; let data, mime_type = match sth#fetch1 () with [ `Binary data; `String mime_type ] -> data, mime_type | _ -> assert false in if version <> None then (* Set a medium-length expiry time on this resource. *) Table.set (Request.headers_out r) "Expires" (Cgi_expires.expires_medium()); q#header ~content_type:mime_type (); print_string r data let () = register_script run