Skip to content

Commit

Permalink
Item11808: perltidy them all, so people are not accidentally impacted…
Browse files Browse the repository at this point in the history
… by the new enforced rule

git-svn-id: http://svn.foswiki.org/trunk/UploadToTWikiAddOn@14680 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
OlivierRaginel authored and OlivierRaginel committed May 1, 2012
1 parent 9969681 commit 28f8576
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 54 deletions.
5 changes: 4 additions & 1 deletion lib/TWiki/Contrib/UploadToTWikiAddOn/build.pl
Expand Up @@ -12,12 +12,15 @@ BEGIN

# name of web to upload to
$build->{UPLOADTARGETWEB} = 'Extensions';

# Full URL of pub directory
$build->{UPLOADTARGETPUB} = 'http://foswiki.org/pub';

# Full URL of bin directory
$build->{UPLOADTARGETSCRIPT} = 'http://foswiki.org/bin';

# Script extension
$build->{UPLOADTARGETSUFFIX} = '';

# Build the target on the command line, or the default target
$build->build($build->{target});
$build->build( $build->{target} );
118 changes: 65 additions & 53 deletions tools/uploadtotwiki.pl
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w
#
# Quick & dirty utility to attach a local file to a TWiki topic
# Quick & dirty utility to attach a local file to a TWiki topic
# via http. Add-on home is at
# http://twiki.org/cgi-bin/view/Plugins/UploadToTWikiAddOn
#
Expand All @@ -24,80 +24,89 @@

use strict;

unless( $ARGV[2] ) {
print "Utility to attach files to any TWiki topic. Copyright (C) 2007, Peter\@Thoeny.org\n";
print "Add-on home: http://twiki.org/cgi-bin/view/Plugins/UploadToTWikiAddOn\n";
unless ( $ARGV[2] ) {
print
"Utility to attach files to any TWiki topic. Copyright (C) 2007, Peter\@Thoeny.org\n";
print
"Add-on home: http://twiki.org/cgi-bin/view/Plugins/UploadToTWikiAddOn\n";
print "Usage:\n";
print " ./uploadtotwiki.pl -l <login> -c <comment> -h 1 <file(s)> <TWiki URL>\n";
print
" ./uploadtotwiki.pl -l <login> -c <comment> -h 1 <file(s)> <TWiki URL>\n";
print "Parameters:\n";
print " -l login - login name of TWiki account (optional)\n";
print " -p password - password of TWiki account (optional)\n";
print " -c 'comment' - comment of attached file (default: 'Uploaded by " . $toolName . "')\n";
print " -c 'comment' - comment of attached file (default: 'Uploaded by "
. $toolName . "')\n";
print " -h 1 - hide attachment, 0 or 1 (default: 0)\n";
print " -i 1 - inline attachment, e.g create link in topic text (default: 0)\n";
print " -d 60 - delay in seconds between uploads of multiple files (default: 30)\n";
print
" -i 1 - inline attachment, e.g create link in topic text (default: 0)\n";
print
" -d 60 - delay in seconds between uploads of multiple files (default: 30)\n";
print " file(s) - one or more local files to upload (required)\n";
print " URL - view URL of TWiki topic, http or https protocol (required, must be last)\n";
print
" URL - view URL of TWiki topic, http or https protocol (required, must be last)\n";
print "Example:\n";
print " ./uploadtotwiki.pl -l MyWikiName *.gif http://twiki.org/cgi-bin/view/Sandbox/UploadTest\n";
print
" ./uploadtotwiki.pl -l MyWikiName *.gif http://twiki.org/cgi-bin/view/Sandbox/UploadTest\n";
exit 1;
}

my $switch = '';
my %opts = ();
my @files = ();
foreach my $item ( @ARGV ) {
if( $switch ) {
$opts{$switch} = $item;
my %opts = ();
my @files = ();
foreach my $item (@ARGV) {
if ($switch) {
$opts{$switch} = $item;
$switch = '';
} elsif( $item =~ /^-([a-zA-Z0-9]+)$/ ) {
}
elsif ( $item =~ /^-([a-zA-Z0-9]+)$/ ) {
$switch = $1;
} else {
push( @files, $item);
}
else {
push( @files, $item );
}
}
my $login = $opts{l} || '';
my $password = $opts{p} || '';
my $comment = $opts{c} || "Uploaded by $toolName";
my $hide = $opts{h} || '';
my $link = $opts{i} || '';
my $delay = $opts{d} || '30';
my $url = pop( @files ) || '';
my $login = $opts{l} || '';
my $password = $opts{p} || '';
my $comment = $opts{c} || "Uploaded by $toolName";
my $hide = $opts{h} || '';
my $link = $opts{i} || '';
my $delay = $opts{d} || '30';
my $url = pop(@files) || '';

exit uploadFile( $comment, $hide, $link, $delay, $url, @files );

# =========================
sub uploadFile
{
my( $theComment, $theHide, $theLink, $theDelay, $theUrl, @theFiles ) = @_;
sub uploadFile {
my ( $theComment, $theHide, $theLink, $theDelay, $theUrl, @theFiles ) = @_;

require LWP;
if ( $@ ) {
if ($@) {
print STDERR "Error: LWP is not installed; cannot upload\n";
return 0;
}
my $ua = UploadToTWiki::UserAgent->new();
$ua->agent( $toolName );
$ua->agent($toolName);
push @{ $ua->requests_redirectable }, 'POST';

my $uploadUrl = $theUrl;
unless( $uploadUrl =~ /^https?:/ ) {
unless ( $uploadUrl =~ /^https?:/ ) {
print STDERR "Error: Only http and https protocols are supported\n";
return 0;
}
unless( $uploadUrl =~ s|/view|/upload| ) {
unless ( $uploadUrl =~ s|/view|/upload| ) {
print STDERR "Error: This is not the URL of a TWiki topic\n";
return 0;
}

my $todo = scalar( @theFiles );
unless( $todo ) {
my $todo = scalar(@theFiles);
unless ($todo) {
print STDERR "Error: No files specified to upload\n";
return 0;
}

foreach my $file ( @theFiles ) {
unless( -e $file ) {
foreach my $file (@theFiles) {
unless ( -e $file ) {
print STDERR "Error: File $file does not exist\n";
return 0;
}
Expand All @@ -111,61 +120,64 @@ sub uploadFile
$uploadUrl,
[
'filename' => $fileName,
'filepath' => [ $file ],
'filepath' => [$file],
'filecomment' => $theComment,
'hidefile' => $theHide,
'createlink' => $theLink
],
'Content_Type' => 'form-data' );
'Content_Type' => 'form-data'
);

if( $response->is_success ) {
if ( $response->is_success ) {
print "... upload finished\n";
} else {
}
else {
print STDERR "Error: " . $response->status_line . "\n";
return 0;
}
$todo--;
if( $todo ) {
if ($todo) {
print "Wait for $theDelay seconds (be nice to the server)\n";
sleep( $theDelay );
sleep($theDelay);
}
}
return 1;
}

# =========================
{

package UploadToTWiki::UserAgent;

use LWP::UserAgent;
our @ISA = qw( LWP::UserAgent );

sub new {
my ($class, $id) = @_;
my ( $class, $id ) = @_;
my $this = $class->SUPER::new();
$this->{domain} = $id;
return $this;
}

sub get_basic_credentials {
my($this, $realm, $uri) = @_;
my ( $this, $realm, $uri ) = @_;
my $host = $uri->host_port();
$host =~ s/\:[0-9]*$//;
local $/ = "\n";
unless( $login ) {
print( "Enter TWiki login name for $host: " );
unless ($login) {
print("Enter TWiki login name for $host: ");
$login = <STDIN>;
chomp( $login );
chomp($login);
}
unless( $password ) {
print( "Enter password for $login at $host: " );
system( 'stty -echo' );
unless ($password) {
print("Enter password for $login at $host: ");
system('stty -echo');
$password = <STDIN>;
system( 'stty echo' );
system('stty echo');
print "\n";
chomp( $password );
chomp($password);
}
return( $login, $password );
return ( $login, $password );
}
}

Expand Down

0 comments on commit 28f8576

Please sign in to comment.