Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small improvements #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
============
Expand Down
51 changes: 51 additions & 0 deletions tiny.cgi
Original file line number Diff line number Diff line change
@@ -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);
20 changes: 15 additions & 5 deletions zpaste
Original file line number Diff line number Diff line change
Expand Up @@ -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'};
Expand All @@ -92,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 ];
}

Expand Down
8 changes: 5 additions & 3 deletions zpaste.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand All @@ -107,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
{
Expand Down Expand Up @@ -188,7 +190,7 @@ if ($q->param('link'))
{
$rewrites{$name} = $data;
untie %rewrites;
print BASEURL, $name, "\n";
print BASETINYURL, $name, "\n";
exit;
}

Expand Down