- # Move the attachment into place.
- # Try to use a fast rename; fall back to copying.
- IkiWiki::prep_writefile($filename, $config{srcdir});
- unlink($config{srcdir}."/".$filename);
- if (! rename($tempfile, $config{srcdir}."/".$filename)) {
- my $fh=$q->upload('attachment');
- if (! defined $fh || ! ref $fh) {
- error("failed to get filehandle");
+sub formbuilder (@) {
+ my %params=@_;
+ my $form=$params{form};
+ my $q=$params{cgi};
+
+ return if ! defined $form->field("do") || ($form->field("do") ne "edit" && $form->field("do") ne "create") ;
+
+ my $filename=Encode::decode_utf8(scalar $q->param('attachment'));
+ my $handle=$q->upload('attachment');
+
+ if (defined $filename && length $filename) {
+ attachment_store($filename, $handle, $form, $q, $params{session});
+ }
+
+ if ($form->submitted eq "Save Page") {
+ attachments_save($form, $params{session});
+ }
+
+ if ($form->submitted eq "Insert Links") {
+ my $page=quotemeta(Encode::decode_utf8(scalar $q->param("page")));
+ my $add="";
+ foreach my $f (@{$q->param_fetch("attachment_select")}) {
+ $f=Encode::decode_utf8($f);
+ $f=~s/^$page\///;
+ if (IkiWiki::isinlinableimage($f) &&
+ IkiWiki::Plugin::img->can("import")) {
+ $add.='[[!img '.$f.' align="right" size="" alt=""]]';
+ }
+ else {
+ $add.="[[$f]]";