App-Filite-Client

 view release on metacpan or  search on metacpan

lib/App/Filite/Client.pm  view on Meta::CPAN

## no Test::Tabs
sub _print_usage {
	print <<"STDERR"; return 0;
filite-client: share via a filite server

Usage:
  filite-client -T [filename]
  filite-client -F [filename]
  filite-client -L [url]
  cat blah | filite-client [options]

Options:
  --text, -T         Share as text
  --file, -F         Share as file
  --link, -L         Share as link
  --highlight, -H    Syntax highligh text
  --help, --usage    Show this usage information

STDERR
}
## use Test::Tabs

sub execute {
	my ( $self, $args ) = ( shift, @_ );
	$args //= [ @ARGV ];
	my $opts = $self->_parse_opts( $args );
	$args = [ '-' ] unless @$args;
	
	if ( $opts->{help} ) {
		return $self->_print_usage;
	}
	
	for my $file ( @$args ) {
		my $url = $self->share( $file, $opts );
		print "$url\n";
	}
	
	$self->errors;
}

sub _guess_mode {
	my ( $self, $file, $opts ) = ( shift, @_ );
	return 'link' if $opts->{link};
	return 'text' if $opts->{text};
	return 'file' if $opts->{file};
	return 'link' if $file =~ m{\Ahttps?://\S+\z}is;
	return 'text' if $opts->{highlight};
	return 'text' if $file eq '-';
	return 'file' if -B $file;
	return 'text';
}

sub share {
	my ( $self, $file, $opts ) = ( shift, @_ );
	$opts //= {};
	my $mode = $self->_guess_mode( $file, $opts );
	my $method = "share_$mode";
	return $self->$method( $file, $opts );
}

sub _get_endpoint {
	my ( $self, $mode ) = ( shift, @_ );
	my $server = $self->server;
	$server = "http://$server" unless $server =~ m{https?:}i;
	$server .= '/' unless $server =~ m{/$};
	return sprintf( '%s%s', $server, lc( substr( $mode, 0, 1 ) ) );
}

sub _handle_response {
	my ( $self, $response ) = ( shift, @_ );
	if ( $response->{success} ) {
		return $response->{content};
	}
	my $errs = $self->errors;
	++$errs;
	$self->errors( $errs );
	warn sprintf( "ERROR: %s %s\n", $response->{status}, $response->{reason} );
	return "-";
}

sub share_file {
	my ( $self, $file, $opts ) = ( shift, @_ );
	$opts //= {};
	
	my ( $filename, $content );
	if ( $file eq '-' ) {
		$filename = 'file.data';
		local $/;
		$content = <STDIN>;
	}
	else {
		my $pt = path( $file );
		$filename = $pt->basename;
		$content  = $pt->slurp;
	}
	
	my $endpoint = $self->_get_endpoint( 'file' );
	my $response = $self->useragent->post_multipart(
		$endpoint => {
			file => {
				filename     => $filename,
				content      => $content,
				content_type => 'application/octet-stream',
			},
		},
	);
	
	return sprintf( '%s/%s', $endpoint, $self->_handle_response( $response ) );
}

sub share_text {
	my ( $self, $file, $opts ) = ( shift, @_ );
	$opts //= {};
	
	my $content;
	if ( $file eq '-' ) {
		local $/;
		$content = <STDIN>;
	}
	else {
		$content = path( $file )->slurp;
	}
	
	my $json = encode_json( {
		contents   => $content,
		highlight  => $opts->{highlight} ? \1 : \0,
	} );
	
	my $endpoint = $self->_get_endpoint( 'text' );
	my $response = $self->useragent->post(
		$endpoint => {
			content => $json,
			headers => { 'Content-Type' => 'application/json' },
		},
	);
	
	return sprintf( '%s/%s', $endpoint, $self->_handle_response( $response ) );
}

sub share_link {
	my ( $self, $file, $opts ) = ( shift, @_ );
	$opts //= {};
	
	my $forward;
	if ( $file eq '-' ) {
		local $/;
		$forward = <>;
	}
	else {
		$forward = $file;
	}
	
	chomp $forward;
	
	my $json = encode_json( {
		forward => $forward,
	} );
	
	my $endpoint = $self->_get_endpoint( 'link' );
	my $response = $self->useragent->post(
		$endpoint => {
			content => $json,
			headers => { 'Content-Type' => 'application/json' },
		},
	);
	
	return sprintf( '%s/%s', $endpoint, $self->_handle_response( $response ) );
}

1;

__END__

=pod

=encoding utf-8

=head1 NAME

App::Filite::Client - client library for Filite servers

=head1 SYNOPSIS

  my $client = App::Filite::Client->new_from_config;
  my $url = $client->share( 'path/to/file.txt' );
  print "Shared to $url\n";

=head1 DESCRIPTION

Share a file, chunk of text, or link via a Filite server.

Filite is a URL shortner, file sharer, and pastebin that you can self-host.

=head2 Config File

Configuration is via a JSON-formatted file usually named
F<< ~/.config/filite-client/config.json >> (see also L<File::XDG>),
though this can be overridden using the C<< FILITE_CLIENT_CONFIG >>
environment variable.

=head2 Constructors

=over

=item C<< new( %attrs ) >>

Moose-like constructor.

=item C<< new_from_config() >>

Load C<< %attrs >> from the config instead of as parameters.

=back

=head2 Attributes

All attributes are read-write.

=over

=item C<< server >> B<< Str >>

The filite server to share things via. This will typically be a URL
like "https://example.com/" or "http://example.net:8080".

=item C<< password >> B<< Str >>



( run in 3.022 seconds using v1.01-cache-2.11-cpan-63c85eba8c4 )