Serial columns now 64 bits.
[cocanwiki.git] / scripts / upload_file.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: upload_file.ml,v 1.10 2004/11/01 17:05:14 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open ExtString
28
29 open Cocanwiki
30 open Cocanwiki_template
31 open Cocanwiki_ok
32 open Cocanwiki_emailnotify
33 open Cocanwiki_images
34 open Cocanwiki_strings
35
36 (* Valid file names. *)
37 let file_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*$"
38
39 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
40   let name = q#param "name" in
41   let title = q#param "title" in
42
43   (* See if there was an upload. *)
44   let file =
45     try
46       let upload = q#upload "file" in
47       upload.upload_value
48     with
49         Not_found ->
50           error ~title:"No file" ~back_button:true
51             q "No file was uploaded.";
52           return () in
53
54   (* Check the name is valid. *)
55   if not (Pcre.pmatch ~rex:file_ok_re name) then (
56     error ~title:"Bad File Name" ~back_button:true
57       q ("The File Name must contain only lowercase English letters, " ^
58          "numbers, dots, dashes and underscore.");
59     return ()
60   );
61
62   (* Identify the MIME type from the extension. *)
63   let mime_type = mime_type_of_filename name in
64
65   let title = if string_is_whitespace title then `Null else `String title in
66
67   (* Check if something with the same name already exists.  If replace=1
68    * then we can replace it, otherwise we must present an error message.
69    *)
70   let replace = q#param_true "replace" in
71   let sth = dbh#prepare_cached "select 1 from files
72                                  where hostid = ? and name = ?" in
73   sth#execute [`Int hostid; `String name];
74
75   let exists = try sth#fetch1int () = 1 with Not_found -> false in
76
77   if exists then (
78     if not replace then (
79       error ~title:"File already exists" ~back_button:true
80         q ("An file with the same name already exists.");
81     return ()
82     ) else (
83       let sth = dbh#prepare_cached "update files
84                                        set name_deleted = name, name = null
85                                      where hostid = ? and name = ?" in
86       sth#execute [`Int hostid; `String name];
87     )
88   );
89
90   (* Put the file into the database. *)
91   let sth =
92     dbh#prepare_cached
93       "insert into files (hostid, name, content, title, mime_type)
94        values (?, ?, ?, ?, ?)" in
95   sth#execute [`Int hostid; `String name; `Binary file; title;
96                `String mime_type];
97
98   dbh#commit ();
99
100   (* Email notify. *)
101   let subject = "File " ^ name ^ " has been uploaded." in
102   let body = fun () ->
103     "Page: http://" ^ hostname ^ "/_files" in
104
105   email_notify ~body ~subject ~user dbh hostid;
106
107   let buttons = [ ok_button "/_files" ] in
108   ok ~title:"File uploaded" ~buttons
109     q "File was uploaded successfully."
110
111 let () =
112   register_script ~restrict:[CanEdit] run