App-Filite-Client

 view release on metacpan or  search on metacpan

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

}

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} ) {

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

		$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 = <>;

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

	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



( run in 0.241 second using v1.01-cache-2.11-cpan-27979f6cc8f )