<commit_before>package App::KSP_CKAN::Tools::IA;

use v5.010;
use strict;
use warnings;
use autodie;
use Method::Signatures 20140224;
use LWP::UserAgent;
use File::MimeInfo::Magic 'mimetype';
use Scalar::Util 'reftype';
use HTTP::Request::StreamingUpload;
use Encode;
use Moo;
use namespace::clean;

# ABSTRACT: An abstraction to the Internet Archive S3 like interface

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

  use App::KSP_CKAN::Tools::IA;

  my $IA = App::KSP_CKAN::Tools::IA->new( config => $config );

=head1 DESCRIPTION

Provides a light wrapper to the Internet Archives' S3 like interface.

=cut

my $Ref = sub {
    croak("auth isn't a 'App::KSP_CKAN::Tools::Config' object!") unless $_[0]->DOES("App::KSP_CKAN::Tools::Config");
};

has 'config'      => ( is => 'ro', required => 1, isa => $Ref );
has 'collection'  => ( is => 'ro', default => sub { 'test_collection' } );
has 'mediatype'   => ( is => 'ro', default => sub { 'software' } );
has '_ua'         => ( is => 'rw', lazy => 1, builder => 1 );
has '_ias3keys'   => ( is => 'ro', lazy => 1, builder => 1 );

method _build__ua {
  my $ua = LWP::UserAgent->new();
  $ua->agent('upload_via_KSP-CKAN/NetKAN-bot');
  $ua->timeout(20);
  $ua->env_proxy;
  $ua->default_headers->push_header( 'authorization' => "LOW ". $self->_ias3keys);
  return $ua;
}

method _build__ias3keys {
  my $config = $self->config;
  return $config->IA_access.":".$config->IA_secret;
}

method _description($ckan) {
  my $description = $ckan->abstract;
  $description .= "<br><br>Homepage: <a href=\"".$ckan->homepage."\">".$ckan->homepage."</a>" if $ckan->homepage;
  $description .= "<br>Repository: <a href=\"".$ckan->repository."\">".$ckan->repository."</a>" if $ckan->repository;
  $description .= "<br>License(s): @{$ckan->licenses}" if $ckan->license;
  return $description;
}

method _archive_header( $header, $value ) {
  # Credit for logic to: https://github.com/kngenie/ias3upload
  if (reftype \$value ne "SCALAR") {
    if ($#{$value} == 0) {
      return ('x-archive-meta-' . $header, encode('UTF-8', $value->[0]));
    } else {
      my $i = 1;
      return map((sprintf('x-archive-meta%02d-%s', $i++, $header), encode('UTF-8',$_)), @{$value});
    }
  } else {
    return ('x-archive-meta-' . $header, encode('UTF-8',$value));
  }
}

method _put_headers ( $file, $ckan ) {
  $self->logdie("\$ckan isn't a 'App::KSP_CKAN::Metadata::Ckan' object!") unless $ckan->DOES("App::KSP_CKAN::Metadata::Ckan");
  my $mimetype = mimetype( $file );<commit_msg>Add iaS3uri + method to produce item/file uri<commit_after>package App::KSP_CKAN::Tools::IA;

use v5.010;
use strict;
use warnings;
use autodie;
use Method::Signatures 20140224;
use LWP::UserAgent;
use File::MimeInfo::Magic 'mimetype';
use Scalar::Util 'reftype';
use HTTP::Request::StreamingUpload;
use Encode;
use Moo;
use namespace::clean;

# ABSTRACT: An abstraction to the Internet Archive S3 like interface

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

  use App::KSP_CKAN::Tools::IA;

  my $IA = App::KSP_CKAN::Tools::IA->new( config => $config );

=head1 DESCRIPTION

Provides a light wrapper to the Internet Archives' S3 like interface.

=cut

my $Ref = sub {
    croak("auth isn't a 'App::KSP_CKAN::Tools::Config' object!") unless $_[0]->DOES("App::KSP_CKAN::Tools::Config");
};

has 'config'      => ( is => 'ro', required => 1, isa => $Ref );
has 'collection'  => ( is => 'ro', default => sub { 'test_collection' } );
has 'mediatype'   => ( is => 'ro', default => sub { 'software' } );
has 'iaS3uri'     => ( is => 'rw', default => sub { 'https://s3.us.archive.org' } );
has '_ua'         => ( is => 'rw', lazy => 1, builder => 1 );
has '_ias3keys'   => ( is => 'ro', lazy => 1, builder => 1 );

method _build__ua {
  my $ua = LWP::UserAgent->new();
  $ua->agent('upload_via_KSP-CKAN/NetKAN-bot');
  $ua->timeout(20);
  $ua->env_proxy;
  $ua->default_headers->push_header( 'authorization' => "LOW ". $self->_ias3keys);
  return $ua;
}

method _build__ias3keys {
  my $config = $self->config;
  return $config->IA_access.":".$config->IA_secret;
}

method _uri($ckan) {
  $self->logdie("\$ckan isn't a 'App::KSP_CKAN::Metadata::Ckan' object!") unless $ckan->DOES("App::KSP_CKAN::Metadata::Ckan");
  return $self->iaS3uri."/".$ckan->mirror_item."/".$ckan->mirror_filename;
}

method _description($ckan) {
  my $description = $ckan->abstract;
  $description .= "<br><br>Homepage: <a href=\"".$ckan->homepage."\">".$ckan->homepage."</a>" if $ckan->homepage;
  $description .= "<br>Repository: <a href=\"".$ckan->repository."\">".$ckan->repository."</a>" if $ckan->repository;
  $description .= "<br>License(s): @{$ckan->licenses}" if $ckan->license;
  return $description;
}

method _archive_header( $header, $value ) {
  # Credit for logic to: https://github.com/kngenie/ias3upload
  if (reftype \$value ne "SCALAR") {
    if ($#{$value} == 0) {
      return ('x-archive-meta-' . $header, encode('UTF-8', $value->[0]));
    } else {
      my $i = 1;
      return map((sprintf('x-archive-meta%02d-%s', $i++, $header), encode('UTF-8',$_)), @{$value});
    }
  } else {
    return ('x-archive-meta-' . $header, encode('UTF-8',$value));
  }
}

method _put_headers ( $file, $ckan ) {
  $self->logdie("\$ckan isn't a 'App::KSP_CKAN::Metadata::Ckan' object!") unless $ckan->DOES("App::KSP_CKAN::Metadata::Ckan");
  my $mimetype = mimetype( $file );
{'fraction_non_alphanumeric': 0.21562442011504918, 'fraction_numerical': 0.01410280200408239, 'mean_word_length': 4.39, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 115, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1049288', 'n_tokens_mistral': 2078, 'n_tokens_neox': 2010, 'n_words': 446}