2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: upload_file.ml,v 1.2 2004/09/07 13:40:10 rich Exp $
17 open Cocanwiki_template
19 open Cocanwiki_emailnotify
21 let is_ws_re = Pcre.regexp "^\\s*$"
22 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
24 (* Valid file names. *)
25 let file_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*$"
27 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
28 let name = q#param "name" in
29 let title = q#param "title" in
31 (* See if there was an upload. *)
34 let upload = q#upload "file" in
38 error ~title:"No file" ~back_button:true
39 q "No file was uploaded.";
42 (* Check the name is valid. *)
43 if not (Pcre.pmatch ~rex:file_ok_re name) then (
44 error ~title:"Bad File Name" ~back_button:true
45 q ("The File Name must contain only lowercase English letters, " ^
46 "numbers, dots, dashes and underscore.");
50 (* Identify the MIME type from the extension. *)
51 let mime_type = mime_type_of_filename name in
53 let title = if is_whitespace title then `Null else `String title in
55 (* Put the file into the database. *)
58 "insert into files (hostid, name, content, title, mime_type)
59 values (?, ?, ?, ?, ?)" in
60 sth#execute [`Int hostid; `String name; `Binary file; title;
66 let subject = "File " ^ name ^ " has been uploaded." in
68 "Page: http://" ^ hostname ^ "/_files" in
70 email_notify ~body ~subject dbh hostid;
72 let buttons = [ ok_button "/_files" ] in
73 ok ~title:"File uploaded" ~buttons
74 q "File was uploaded successfully."