From 310fa0e441a4720e485b89a9753bfd87c037f201 Mon Sep 17 00:00:00 2001 From: Jesus Ruiz Date: Tue, 11 Apr 2017 14:21:09 +0200 Subject: [PATCH 1/6] add --name option to zpaste --- zpaste | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/zpaste b/zpaste index ed08561..ff52163 100755 --- a/zpaste +++ b/zpaste @@ -63,20 +63,24 @@ $ua->agent('zpaste/0.1'); # parse command line arguments my %o = (); -GetOptions(\%o, 'del', 'force', 'link=s', 'type=s') +GetOptions(\%o, 'name=s', 'del', 'force', 'link=s', 'type=s') or exit 1; die "usage: $0 [opts] [name]\n" if @ARGV > 1; -my $name = shift @ARGV; +my $fname = shift @ARGV; + +if ($fname and not $o{'name'}) { + $o{'name'} = $fname; +} die "paste name not given; mandatory for --del\n" - if $o{'del'} and not $name; + if $o{'del'} and not $o{'name'}; # construct the request fields my @rdata = ('key' => KEY); -push @rdata, 'name' => $name if $name; +push @rdata, 'name' => $o{'name'} if $o{'name'}; push @rdata, 'del' => 1 if $o{'del'}; push @rdata, 'force' => 1 if $o{'force'}; From 5f0e958fc3627b790e0574eec3920fde96600a39 Mon Sep 17 00:00:00 2001 From: Jesus Ruiz Date: Tue, 11 Apr 2017 14:22:23 +0200 Subject: [PATCH 2/6] allow to paste from stdin or from a file --- zpaste | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zpaste b/zpaste index ff52163..1ca8bf3 100755 --- a/zpaste +++ b/zpaste @@ -96,9 +96,15 @@ if ($o{'link'}) } elsif (not $o{'del'}) { + my $fh = undef; + if ($fname) { + open($fh, "<:raw", $fname) || die "error: can't read $fname: $!\n"; + } else { + $fh = \*STDIN; + } my $content = ''; my $buf; - $content .= $buf while read(STDIN, $buf, 65536) > 0; + $content .= $buf while read($fh, $buf, 65536) > 0; push @rdata, 'data' => [ undef, 'stdin', 'Content' => $content ]; } From 5d4e85f88c5b512fc253d51d3028788e41b6bb3e Mon Sep 17 00:00:00 2001 From: Jesus Ruiz Date: Tue, 11 Apr 2017 14:45:41 +0200 Subject: [PATCH 3/6] separate redirect database dir from paste dir, and redirect url from paste url --- zpaste.cgi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/zpaste.cgi b/zpaste.cgi index 103a64d..1e3eb09 100755 --- a/zpaste.cgi +++ b/zpaste.cgi @@ -8,8 +8,10 @@ use warnings; use constant KEY => 'zi8ahkoobahko,xaefeetuphei6eaCee'; use constant DATADIR => '/space/www/data/zpaste'; use constant METADIR => '/space/www/data/zpaste/.web'; +use constant DBDIR => '/srv/zpaste'; use constant METASUFFIX => '.meta'; use constant BASEURL => 'http://p.example.com/'; +use constant BASETINYURL => 'http://example.com/tiny/'; # documentation @@ -89,7 +91,7 @@ print $q->header(-type => 'text/plain', -charset => 'utf-8'); # attach to the rewrite mapping db -my $rewritedb = catfile(DATADIR, 'rewrite.db'); +my $rewritedb = catfile(DBDIR, 'rewrite.db'); my %rewrites; tie %rewrites, 'SDBM_File', $rewritedb, O_RDWR|O_CREAT, 0644; @@ -188,7 +190,7 @@ if ($q->param('link')) { $rewrites{$name} = $data; untie %rewrites; - print BASEURL, $name, "\n"; + print BASETINYURL, $name, "\n"; exit; } From cc9871df2d7a3876d98824627c0b3d099ad52569 Mon Sep 17 00:00:00 2001 From: Jesus Ruiz Date: Tue, 11 Apr 2017 14:48:33 +0200 Subject: [PATCH 4/6] allow dots in paste and redirect names --- zpaste.cgi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zpaste.cgi b/zpaste.cgi index 1e3eb09..9ba73c9 100755 --- a/zpaste.cgi +++ b/zpaste.cgi @@ -109,7 +109,7 @@ my $name = $q->param('name'); if ($name) { - $name =~ s/[^a-zA-Z0-9_-]//g; + $name =~ s/[^\.a-zA-Z0-9_-]//g; } else { From 7dde7fa6fc8825d49110ce5d2dfa9f9e586c2906 Mon Sep 17 00:00:00 2001 From: Jesus Ruiz Date: Tue, 11 Apr 2017 14:53:39 +0200 Subject: [PATCH 5/6] Update README.md, demostrate usage with examples --- README.md | 57 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 565ee5f..fb44938 100644 --- a/README.md +++ b/README.md @@ -14,23 +14,58 @@ Main selling points: Usage ===== - ... | zpaste [name] - zpaste --link http://example.com/ [name] +Paste the contents of standard input: -The first command above pastes the contents of standard input. A link -to the generated paste is written to standard output. The optional -argument *name* gives the name (alphanumerics, underscore and dash -only); if not provided, a random name is used. + $ ... | zpaste + https://example.com/zpaste/wz9w -The second form makes a tinyurl-style address redirection entry to the -provided URL. +Give the paste link an optional ending: -Command line options accepted by the command: + $ ... | zpaste --name=somename + https://example.com/zpaste/somename + +Upload a file: + + $ zpaste somefile.txt + https://example.com/zpaste/somefile.txt + +Upload a file, optionally give the link a different ending: + + $ zpaste --name=somename.txt somefile.txt + https://example.com/zpaste/somename.txt + +`zpaste` writes a link to the generated paste to standard output. The +optional argument passed with **--name** gives the ending of the link +(alphanumerics, dot, underscore and dash only); if not provided, a random name +or the passed file name is used. + +Generate a redirect link: + + $ zpaste --link http://example.com/ + https://example.com/tiny/6lu9 + +Generate a redirect link, with an optional ending: + + $ zpaste --link http://example.com/ somename + https://example.com/tiny/somename + +This form generates a tinyurl-style address redirection link to the provided +URL and outputs it to stdout. + +Delete an existing paste or redirect link: + + $ zpaste --del wz9w + paste 'wz9w' deleted + + $ zpaste --del 6lu9 + link '6lu9' deleted + +The *name* argument is required in this case. + +Other command line options accepted by `zpaste`: * **--force**: If specified, existing pastes/links are overwritten; otherwise, a duplicate name is an error. -* **--del**: Instead of adding a new paste, delete an existing one. - The *name* argument is required in this case. Installation ============ From 42f6fdbcc14d41c58a3fc12f4c505b055416faa4 Mon Sep 17 00:00:00 2001 From: Jesus Ruiz Date: Tue, 11 Apr 2017 14:59:28 +0200 Subject: [PATCH 6/6] add redirect cgi script --- tiny.cgi | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 tiny.cgi diff --git a/tiny.cgi b/tiny.cgi new file mode 100755 index 0000000..f584fd1 --- /dev/null +++ b/tiny.cgi @@ -0,0 +1,51 @@ +#! /usr/bin/env perl + +use strict; +use warnings; + +# settings + +use constant DBDIR => '/srv/zpaste'; + +use CGI; +use File::Spec::Functions; +use Fcntl; + +use SDBM_File; +#use GDBM_File; + +# CGI setup + +my $q = CGI->new; + +# attach to the rewrite mapping db + +my $rewritedb = catfile(DBDIR, 'rewrite.db'); +my %rewrites; + +tie %rewrites, 'SDBM_File', $rewritedb, O_RDWR|O_CREAT, 0644; +#tie %rewrites, 'GDBM_File', $rewritedb, &GDBM_READER, 0644; + +unless (tied %rewrites) +{ + print "unable to attach: $rewritedb: $!"; + exit; +} + +# extract tiny url name + +my $name = $ENV{'PATH_INFO'}; +$name =~ s,/,,; + +#print "name: $name\n"; + +# look for the redirect link + +my $link = $rewrites{$name}; + +unless ($link) { + print "no link found for $name\n"; + exit; +} + +print $q->redirect($link);