About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / upload_image.ml
index 5b74bb9..4101832 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: upload_image.ml,v 1.8 2004/10/21 19:54:29 rich Exp $
+ * $Id: upload_image.ml,v 1.12 2005/11/24 14:54:13 rich Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,12 +30,10 @@ open Cocanwiki
 open Cocanwiki_ok
 open Cocanwiki_emailnotify
 open Cocanwiki_images
-
-let is_ws_re = Pcre.regexp "^\\s*$"
-let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
+open Cocanwiki_strings
 
 (* Valid image names. *)
-let image_ok_re = Pcre.regexp "^[a-z0-9][_a-z0-9]*\\.(jpg|jpeg|gif|ico|png)$"
+let image_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*\\.(jpg|jpeg|gif|ico|png)$"
 
 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
   let name = q#param "name" in
@@ -47,20 +45,22 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
   (* See if there was an upload. *)
   let image =
     try
-      let upload = q#upload "image" in
+      let upload = q#upload "file" in
       upload.upload_value
     with
        Not_found ->
          error ~title:"No image" ~back_button:true
-           q "No image was uploaded.";
+           dbh hostid q "No image was uploaded.";
          return () in
 
   (* Check the name is valid. *)
   if not (Pcre.pmatch ~rex:image_ok_re name) then (
     error ~title:"Bad Image Name" ~back_button:true
-      q ("The Image Name must contain only lowercase English letters, " ^
-        "numbers and underscore.  It must end with .jpg, .gif or .png " ^
-        "depending on the image format.");
+      dbh hostid q
+      ("The Image Name must contain only lowercase English letters, " ^
+       "numbers, dots, dashes and underscore.  " ^
+       "It must end with .jpg, .gif or .png " ^
+       "depending on the image format.");
     return ()
   );
 
@@ -70,8 +70,9 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
     with
        Invalid_argument _ ->
          error ~title:"Bad image" ~back_button:true
-           q ("Unknown image type.  Is the file you uploaded really an " ^
-              "image?");
+           dbh hostid q
+           ("Unknown image type.  Is the file you uploaded really an " ^
+            "image?");
          return () in
 
   (* Check the image filename extension matches the MIME type. *)
@@ -87,30 +88,56 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
       | _ -> assert false in
   if not ext_ok then (
     error ~title:"Bad Image Name" ~back_button:true
-      q ("The Image Name extension has to match the image format.  " ^
-        "For example if the image is in JPEG format, the name must " ^
-        "be 'something.jpg'.  I detected the following image type " ^
-        "in the file you uploaded: " ^ mime_type);
+      dbh hostid q
+      ("The Image Name extension has to match the image format.  " ^
+       "For example if the image is in JPEG format, the name must " ^
+       "be 'something.jpg'.  I detected the following image type " ^
+       "in the file you uploaded: " ^ mime_type);
     return ()
   );
 
   (* Check some ALT text was supplied. *)
-  if is_whitespace alt then (
+  if string_is_whitespace alt then (
     error ~title:"Missing Alt text" ~back_button:true
-      q ("You must supply Alt text describing the image.  This is required " ^
-        "by accessibility laws and to allow search engines to discover the " ^
-        "content of images.");
+      dbh hostid q
+      ("You must supply Alt text describing the image.  This is required " ^
+       "by accessibility laws and to allow search engines to discover the " ^
+       "content of images.");
     return ()
   );
 
-  let title = if is_whitespace title then `Null else `String title in
-  let longdesc = if is_whitespace longdesc then `Null else `String longdesc in
-  let clazz = if is_whitespace clazz then `Null else `String clazz in
+  let title = if string_is_whitespace title then `Null else `String title in
+  let longdesc =
+    if string_is_whitespace longdesc then `Null else `String longdesc in
+  let clazz = if string_is_whitespace clazz then `Null else `String clazz in
 
   (* Make a thumbnail of this image. *)
   let thumbnail, tn_mime_type, tn_width, tn_height =
     image_thumbnail image 120 120 in
 
+  (* Check if something with the same name already exists.  If replace=1
+   * then we can replace it, otherwise we must present an error message.
+   *)
+  let replace = q#param_true "replace" in
+  let sth = dbh#prepare_cached "select 1 from images
+                                 where hostid = ? and name = ?" in
+  sth#execute [`Int hostid; `String name];
+
+  let exists = try sth#fetch1int () = 1 with Not_found -> false in
+
+  if exists then (
+    if not replace then (
+      error ~title:"Image already exists" ~back_button:true
+       dbh hostid q "An image with the same name already exists.";
+    return ()
+    ) else (
+      let sth = dbh#prepare_cached "update images
+                                       set name_deleted = name, name = null
+                                     where hostid = ? and name = ?" in
+      sth#execute [`Int hostid; `String name];
+    )
+  );
+
   (* Put the image into the database. *)
   let sth =
     dbh#prepare_cached
@@ -134,7 +161,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
 
   let buttons = [ ok_button "/_images" ] in
   ok ~title:"Image uploaded" ~buttons
-    q "Image was uploaded successfully."
+    dbh hostid q "Image was uploaded successfully."
 
 let () =
   register_script ~restrict:[CanEdit] run