<commit_before>use strict;
use Bio::KBase::InvocationService::InvocationServiceImpl;

use Bio::KBase::InvocationService::Service;

use Bio::KBase::AuthToken;

use Data::Dumper;
use File::Path qw(make_path);
use Plack::Request;
use URI::Dispatch;
use Bio::KBase::DeploymentConfig;
use Cwd 'abs_path';

my @dispatch;

my $tmpdir = abs_path("/tmp");

my $cfg = Bio::KBase::DeploymentConfig->new("InvocationService", {
    'authenticated-storage' => "$tmpdir/InvocationService/authenticated-storage",
    'nonauthenticated-storage' => "$tmpdir/InvocationService/nonauthenticated-storage",
});

my $auth_storage = $cfg->setting('authenticated-storage');
my $nonauth_storage = $cfg->setting('nonauthenticated-storage');

make_path($auth_storage);
make_path($nonauth_storage);

print STDERR "auth=$auth_storage nonauth=$nonauth_storage\n";

my $obj = Bio::KBase::InvocationService::InvocationServiceImpl->new($cfg);
push(@dispatch, 'InvocationService' => $obj);

my $server = Bio::KBase::InvocationService::Service->new(instance_dispatch => { @dispatch },
				allow_get => 0,
			       );


my $dispatch = URI::Dispatch->new();
$dispatch->add('/', 'handle_service');
$dispatch->add('/invoke', 'handle_invoke');
$dispatch->add('/upload', 'handle_upload');
$dispatch->add('/download/#*', 'handle_download');
$dispatch->add('//download/#*', 'handle_download');

{
    package handle_download;
    use strict;
    use Data::Dumper;

    sub get
    {
	my($req, $args) = @_;
	my $session = $req->param("session_id");

my $dlobj = Bio::KBase::InvocationService::InvocationServiceImpl->new($cfg);
my @dldispatch = 'InvocationService' => $dlobj;

my $dlserver = Bio::KBase::InvocationService::Service->new(instance_dispatch => { @dldispatch },
				allow_get => 0,
			       );


    my $token = $dlserver->_plack_req->header("Authorization");
	my $auth_token = Bio::KBase::AuthToken->new(token => $token, ignore_authrc => 1);
	my $valid = $auth_token->validate();

    my $ctx = undef;
    if ($valid) {
        $ctx = Bio::KBase::InvocationService::ServiceContext->new(client_ip => $dlserver->_plack_req->address);
	    $ctx->authenticated(1);
	    $ctx->user_id($auth_token->user_id);
	    $ctx->token( $token);
    }

	my $us = Bio::KBase::InvocationService::UserSession->new($dlobj, $session, $ctx);

	my $dir = $us->_session_dir();

	if (! -d $dir)
	{
	    return [500, [], ["Invalid session id\n"]];
	}

	#
	# Validate path given.
	#
	my $file = $args->[0];
	my @comps = split(/\//, $file);
	if (grep { $_ eq '..' } @comps)
	{
	    return [404, [], ["File not found\n"]];
	}

	my $path = "$dir/$file";
	my $fh;
	if (!open($fh, "<", $path))
	{
	    return [404, [], ["File not found\n"]];
	}

    (my $fileName = $file) =~ s!^(?:.*)/([^/]+)$!$1!g;

	return [
		200,
		[
			'Content-type' => 'application/force-download',
			'Content-Disposition' => "attachment; filename=\"$fileName\"",
			'Content-Length' => -s $path,
		],
		$fh
	];
    }
}

{
    package handle_upload;
    use strict;
    use Data::Dumper;

    sub post
    {
	my($req, $args) = @_;

	my @origin_hdr = ('Access-Control-Allow-Origin', $req->env->{HTTP_ORIGIN});

	my $session = $req->param("session_id");
	my $us = Bio::KBase::InvocationService::UserSession->new($ulobj, $session, undef);

	my $dir = $ulobj->_session_dir();
	if (!-d $dir)
	{
	    return [500, \@origin_hdr, ["Invalid session id\n"]];
	}

	#
	# Validate path given.
	#
	my $file = $req->param('qqfile');
	my @comps = split(/\//, $file);
	if (grep { $_ eq '..' } @comps)
	{
	    return [404, \@origin_hdr, ["File not found\n"]];
	}

	my $path = "$dir/$file";
	my $fh;<commit_msg>attempted hack to try and fix the download glitches<commit_after>use strict;
use Bio::KBase::InvocationService::InvocationServiceImpl;

use Bio::KBase::InvocationService::Service;

use Bio::KBase::AuthToken;

use Data::Dumper;
use File::Path qw(make_path);
use Plack::Request;
use URI::Dispatch;
use Bio::KBase::DeploymentConfig;
use Cwd 'abs_path';

my @dispatch;

my $tmpdir = abs_path("/tmp");

my $cfg = Bio::KBase::DeploymentConfig->new("InvocationService", {
    'authenticated-storage' => "$tmpdir/InvocationService/authenticated-storage",
    'nonauthenticated-storage' => "$tmpdir/InvocationService/nonauthenticated-storage",
});

my $auth_storage = $cfg->setting('authenticated-storage');
my $nonauth_storage = $cfg->setting('nonauthenticated-storage');

make_path($auth_storage);
make_path($nonauth_storage);

print STDERR "auth=$auth_storage nonauth=$nonauth_storage\n";

my $obj = Bio::KBase::InvocationService::InvocationServiceImpl->new($cfg);
push(@dispatch, 'InvocationService' => $obj);

my $server = Bio::KBase::InvocationService::Service->new(instance_dispatch => { @dispatch },
				allow_get => 0,
			       );


my $dispatch = URI::Dispatch->new();
$dispatch->add('/', 'handle_service');
$dispatch->add('/invoke', 'handle_invoke');
$dispatch->add('/upload', 'handle_upload');
$dispatch->add('/download/#*', 'handle_download');
$dispatch->add('//download/#*', 'handle_download');

{
    package handle_download;
    use strict;
    use Data::Dumper;

    sub get
    {
	my($req, $args) = @_;
	my $session = $req->param("session_id");

my $dlobj = Bio::KBase::InvocationService::InvocationServiceImpl->new($cfg);
my @dldispatch = 'InvocationService' => $dlobj;

my $dlserver = Bio::KBase::InvocationService::Service->new(instance_dispatch => { @dldispatch },
				allow_get => 0,
			       );


    my $token = $dlserver->_plack_req->header("Authorization");
	my $auth_token = Bio::KBase::AuthToken->new(token => $token, ignore_authrc => 1);
	my $valid = $auth_token->validate();

    my $ctx = undef;
    if ($valid) {
        $ctx = Bio::KBase::InvocationService::ServiceContext->new(client_ip => $dlserver->_plack_req->address);
	    $ctx->authenticated(1);
	    $ctx->user_id($auth_token->user_id);
	    $ctx->token( $token);
    }

	my $us = Bio::KBase::InvocationService::UserSession->new($dlobj, $session, $ctx);

	my $dir = $us->_session_dir();

	if (! -d $dir)
	{
	    return [500, [], ["Invalid session id\n"]];
	}

	#
	# Validate path given.
	#
	my $file = $args->[0];
	my @comps = split(/\//, $file);
	if (grep { $_ eq '..' } @comps)
	{
	    return [404, [], ["File not found\n"]];
	}

	my $path = "$dir/$file";
	my $fh;
	if (!open($fh, "<", $path))
	{
	    return [404, [], ["File not found\n"]];
	}

    (my $fileName = $file) =~ s!^(?:.*)/([^/]+)$!$1!g;

	return [
		200,
		[
			'Content-type' => 'application/force-download',
			'Content-Disposition' => "attachment; filename=\"$fileName\"",
			'Content-Length' => -s $path,
		],
		$fh
	];
    }
}

{
    package handle_upload;
    use strict;
    use Data::Dumper;

    sub post
    {
	my($req, $args) = @_;

my $ulobj = Bio::KBase::InvocationService::InvocationServiceImpl->new($cfg);
my @uldispatch = 'InvocationService' => $ulobj;

my $ulserver = Bio::KBase::InvocationService::Service->new(instance_dispatch => { @uldispatch },
				allow_get => 0,
			       );


	my @origin_hdr = ('Access-Control-Allow-Origin', $req->env->{HTTP_ORIGIN});

	my $session = $req->param("session_id");
	my $us = Bio::KBase::InvocationService::UserSession->new($ulobj, $session, undef);

	my $dir = $ulobj->_session_dir();
	if (!-d $dir)
	{
	    return [500, \@origin_hdr, ["Invalid session id\n"]];
	}

	#
	# Validate path given.
	#
	my $file = $req->param('qqfile');
	my @comps = split(/\//, $file);
	if (grep { $_ eq '..' } @comps)
	{
	    return [404, \@origin_hdr, ["File not found\n"]];
	}

	my $path = "$dir/$file";
	my $fh;
{'fraction_non_alphanumeric': 0.20730082600586197, 'fraction_numerical': 0.006528110844657607, 'mean_word_length': 4.3775071633237825, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1974005', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2678, 'n_words': 506}