2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: upload_file.ml,v 1.4 2004/09/08 09:54:28 rich Exp $
15 open Cocanwiki_template
17 open Cocanwiki_emailnotify
20 let is_ws_re = Pcre.regexp "^\\s*$"
21 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
23 (* Valid file names. *)
24 let file_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*$"
26 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
27 let name = q#param "name" in
28 let title = q#param "title" in
30 (* See if there was an upload. *)
33 let upload = q#upload "file" in
37 error ~title:"No file" ~back_button:true
38 q "No file was uploaded.";
41 (* Check the name is valid. *)
42 if not (Pcre.pmatch ~rex:file_ok_re name) then (
43 error ~title:"Bad File Name" ~back_button:true
44 q ("The File Name must contain only lowercase English letters, " ^
45 "numbers, dots, dashes and underscore.");
49 (* Identify the MIME type from the extension. *)
50 let mime_type = mime_type_of_filename name in
52 let title = if is_whitespace title then `Null else `String title in
54 (* Put the file into the database. *)
57 "insert into files (hostid, name, content, title, mime_type)
58 values (?, ?, ?, ?, ?)" in
59 sth#execute [`Int hostid; `String name; `Binary file; title;
65 let subject = "File " ^ name ^ " has been uploaded." in
67 "Page: http://" ^ hostname ^ "/_files" in
69 email_notify ~body ~subject dbh hostid;
71 let buttons = [ ok_button "/_files" ] in
72 ok ~title:"File uploaded" ~buttons
73 q "File was uploaded successfully."
76 register_script ~restrict:[CanEdit] run