Tags:
create new tag
view all tags

Question

Hi,

I've been able to use EditTWikiExternalEditorAddOn to remotely edit pages through a script. I'm using the kind of method described on HowToUploadTextFilesIntoTheContentArea. However, my pages contain GIF images (aka: attachments). Does anybody have a script to upload a binary file as an attachment to a Topic name though a web request so that it gets version-controlled in Twiki ?

Thanks,

Environment

TWiki version: TWikiRelease02Sep2004
TWiki plugins: DefaultPlugin, EmptyPlugin, InterwikiPlugin
Server OS: Tao Linux 1.0
Web server: Apache 2.0.46
Perl version: any
Client OS: any
Web Browser: any
Categories: Add-Ons

-- GillesEricDescamps - 16 Mar 2005

Answer

Example taken from the code of BuildContrib:

{   package TWiki::Contrib::Build::UserAgent;
    @TWiki::Contrib::Build::UserAgent::ISA = qw(LWP::UserAgent);

    my ( $knownUser, $knownPass );

    sub get_basic_credentials {
        my($self, $realm, $uri) = @_;
        unless ( $knownUser ) {
            print "Logon to ",$uri->host_port,"\n";
            print "Enter $realm: ";
            $knownUser = <STDIN>;
            chomp($knownUser);
            return (undef, undef) unless length $knownUser;
            print "Password on ",$uri->host_port,": ";
            system("stty -echo");
            $knownPass = <STDIN>;
            system("stty echo");
            print "\n";  # because we disabled echo
            chomp($knownPass);
        }
        return ($knownUser, $knownPass);
    }
}

sub upload {
    my( $url, $filename, $filepath ) = @_;

    eval 'use LWP';
    if ( $@ ) {
        print STDERR "LWP is not installed; cannot upload\n";
        return 0;
    }

    my $userAgent = TWiki::Contrib::Build::UserAgent->new();
    $userAgent->agent( "TWikiContribBuild/$VERSION " );


    $response =
      $userAgent->post( $url,
                        [
                         'filename' => $filename,
                         'filepath' => [ $filepath ],
                         'filecomment' => "Unzip in the root directory of your TWiki installation"
                        ],
                        'Content_Type' => "form-data" );

    die "Upload failed ", $response->request->uri,
      " -- ", $response->status_line, "\nAborting\n", $response->as_string
        unless $response->is_redirect &&
          $response->headers->header('Location') =~ /view([\.\w]*)\/Plugins\/$to/;
}

upload( "http://twiki.org/cgi-bin/upload/Sandbox/JunkTopic", "wombat.gif", "/home/animals/wombat.gif" );

If you want to see more, such as updating a topic without damaging the form, see the source code of BuildContrib.

-- CrawfordCurrie - 16 Mar 2005

If anyone is thinking of packaging this up, please make it a TWikiShellContrib CommandSet

-- MartinCleaver - 16 Mar 2005

See also possible use of WebDAVPlugin.

-- PeterThoeny - 19 Mar 2005

Thanks for your help. Here is the QUICK & DIRTY perl script I'm now using:

#!/usr/bin/perl -w
                                                                                                                                                                                                           
{   package TWiki::Contrib::Build::UserAgent;
    @TWiki::Contrib::Build::UserAgent::ISA = qw(LWP::UserAgent);
                                                                                                                                                                                                           
    my ( $knownUser, $knownPass );
                                                                                                                                                                                                           
    sub get_basic_credentials {
        my($self, $realm, $uri) = @_;
        unless ( $knownUser ) {
            print "Logon to ",$uri->host_port,"\n";
            print "Enter $realm: ";
            $knownUser = <STDIN>;
            chomp($knownUser);
            return (undef, undef) unless length $knownUser;
            print "Password on ",$uri->host_port,": ";
            system("stty -echo");
            $knownPass = <STDIN>;
            system("stty echo");
            print "\n";  # because we disabled echo
            chomp($knownPass);
        }
        return ($knownUser, $knownPass);
    }
}
                                                                                                                                                                                                           
sub upload {
    my( $topic, $filepath ) = @_;
                                                                                                                                                                                                           
    $topic =~ s/\./\//g;
    eval 'use LWP';
    if ( $@ ) {
        print STDERR "LWP is not installed; cannot upload\n";
        return 0;
    }
    use File::Basename;
    my $filename=basename($filepath);
                                                                                                                                                                                                           
    my $userAgent = TWiki::Contrib::Build::UserAgent->new();
    $userAgent->agent( "attachtwiki.pl/1.0" );
                                                                                                                                                                                                           
    my $url = "";
    my $cfgname = "/user/".$ENV{"USER"}."/.edittwiki.cfg";
    open (CFG, $cfgname) or die "ERROR: cannot read $cfgname due to $!";
    my $host="";
    my $userid="";
    my $password="";
    my $subdir="";
    my $cgibin="";
    while (<CFG>) {
        chomp;
        if (/host=(.*)/) { $host=$1; };
        if (/userid=(.*)/) { $userid=$1; };
        if (/password=(.*)/) { $password=$1; };
        if (/subdir=(.*)/) { $subdir=$1; };
        if (/cgibin=(.*)/) { $cgibin=$1; };
    }
    close (CFG);
    $url = "http://".$userid.":".$password."\@".$host.$subdir."/".$cgibin."/upload/".$topic;
                                                                                                                                                                                                           
    $response =
      $userAgent->post( $url,
                        [
                         'filename' => $filename,
                         'filepath' => [ $filepath ],
                         'filecomment' => "uploaded by attachsharedbook"
                        ],
                        'Content_Type' => "form-data" );
                                                                                                                                                                                                           
    if ($response->is_redirect && ($response->headers->header('Location') =~ /view\/$topic/) ) {
        print "OK, file:$filename attached to topic:$topic\n";
    } else {
        die "Upload failed ", $response->request->uri,
      " -- ", $response->status_line, "\nAborting\n", $response->as_string
    };
}
                                                                                                                                                                                                           
# upload( "Sandbox.TestTopicged", "/usr/share/doc/cups-1.1.17/images/printer-stopped.gif" );
my $to = $ARGV[0];
$to =~ s/^-T//;
my $file = $ARGV[1];
if ( ! -r $file ) {
    die "ERROR: not a readable file:$file";
};
upload ($to, $file);

-- GillesEricDescamps - 30 Mar 2005

Gilles-Eric: Had you seen the BatchUploadPlugin ?

-- MartinCleaver - 05 Apr 2005

Edit | Attach | Watch | Print version | History: r6 < r5 < r4 < r3 < r2 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r6 - 2005-04-05 - MartinCleaver
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2026 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.