+ let title = if string_is_whitespace title then None else Some title 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 rows = PGSQL(dbh)
+ "select 1 from files where hostid = $hostid and name = $name" in
+ let exists = rows = [Some 1l] in
+
+ if exists then (
+ if not replace then (
+ error ~title:"File already exists" ~back_button:true
+ dbh hostid q "An file with the same name already exists.";
+ return ()
+ ) else (
+ PGSQL(dbh) "update files
+ set name_deleted = name, name = null
+ where hostid = $hostid and name = $name"
+ )
+ );