<commit_before>package EnsEMBL::Web::Command::UserData::UploadFile;

use strict;
use warnings;

use Class::Std;

use EnsEMBL::Web::RegObj;
use CGI qw(escape escapeHTML);
use base 'EnsEMBL::Web::Command';

{

sub BUILD {
}

sub process {
  my $self = shift;

  my $url = '/UserData/SelectFile'; ## Fallback on error
  my $param = {
    '_referer' => $self->object->param('_referer'),
    'x_requested_with' => $self->object->param('x_requested_with'),
  };
  my $method = $self->object->param('url') ? 'url' : 'file';

  if ($self->object->param($method)) {

    ## Get original path, so can save file name as default name for upload
    my $name = $self->object->param('name');
    unless ($name) {
      my @orig_path = split('/', $self->object->param($method));
      $name = $orig_path[-1];
    }

    ## Cache data (TmpFile::Text knows whether to use memcached or temp file)
    my $file = new EnsEMBL::Web::TmpFile::Text(
      prefix => 'user_upload',
      ($method eq 'url') ?
      (content      => get_url_content($self->object->param('url'))) :
      (tmp_filename => $self->object->[1]->{'_input'}->tmpFileName($self->object->param('file'))),
    );

    if ($file->save) {
      ## Identify format
      my $data = $file->retrieve;
      my $parser = EnsEMBL::Web::Text::FeatureParser->new();
      $parser = $parser->init($data);
      if ($parser->{'_info'} && $parser->{'_info'}->{'count'} && $parser->{'_info'}->{'count'} > 0) {
        my $format = $parser->{'_info'}->{'format'};

        $param->{'parser'} = $parser;
        $param->{'species'} = $self->object->param('species');
        ## Attach data species to session
        $self->object->get_session->add_data(
          type      => 'upload',
          filename  => $file->filename,
          code      => $file->md5,
          md5       => $file->md5,
          name      => $name,
          species   => $self->object->param('species'),
          format    => $format,
          assembly  => $self->object->param('assembly'),
        );

        $param->{'code'}    = $file->md5;
        if (!$format) {
          ## Get more input from user
          $url = '/UserData/MoreInput';
          $param->{'format'}  = 'none';
        }
        else {
          $url = '/UserData/UploadFeedback';
          $param->{'format'} = $format;
        }
      }
      else {
        $param->{'filter_module'} = 'Data';
        $param->{'filter_code'} = 'empty';
      }
    }
    else {
      $param->{'filter_module'} = 'Data';
      $param->{'filter_code'} = 'no_save';
    }
  }
  else {
    $param->{'filter_module'} = 'Data';
    $param->{'filter_code'} = 'no_data';
  }
  <commit_msg>add session id to code to make them unique<commit_after>package EnsEMBL::Web::Command::UserData::UploadFile;

use strict;
use warnings;

use Class::Std;

use EnsEMBL::Web::RegObj;
use CGI qw(escape escapeHTML);
use base 'EnsEMBL::Web::Command';

{

sub BUILD {
}

sub process {
  my $self = shift;

  my $url = '/UserData/SelectFile'; ## Fallback on error
  my $param = {
    '_referer' => $self->object->param('_referer'),
    'x_requested_with' => $self->object->param('x_requested_with'),
  };
  my $method = $self->object->param('url') ? 'url' : 'file';

  if ($self->object->param($method)) {

    ## Get original path, so can save file name as default name for upload
    my $name = $self->object->param('name');
    unless ($name) {
      my @orig_path = split('/', $self->object->param($method));
      $name = $orig_path[-1];
    }

    ## Cache data (TmpFile::Text knows whether to use memcached or temp file)
    my $file = new EnsEMBL::Web::TmpFile::Text(
      prefix => 'user_upload',
      ($method eq 'url') ?
      (content      => get_url_content($self->object->param('url'))) :
      (tmp_filename => $self->object->[1]->{'_input'}->tmpFileName($self->object->param('file'))),
    );
    
    if ($file->save) {
      ## Identify format
      my $data = $file->retrieve;
      my $parser = EnsEMBL::Web::Text::FeatureParser->new();
      $parser = $parser->init($data);
      
      my $code = $file->md5 . '_' . $self->object->get_session->get_session_id;
      
      if ($parser->{'_info'} && $parser->{'_info'}->{'count'} && $parser->{'_info'}->{'count'} > 0) {
        my $format = $parser->{'_info'}->{'format'};

        $param->{'parser'} = $parser;
        $param->{'species'} = $self->object->param('species');
        ## Attach data species to session
        $self->object->get_session->add_data(
          type      => 'upload',
          filename  => $file->filename,
          code      => $code,
          md5       => $file->md5,
          name      => $name,
          species   => $self->object->param('species'),
          format    => $format,
          assembly  => $self->object->param('assembly'),
        );

        $param->{'code'} = $code;
        if (!$format) {
          ## Get more input from user
          $url = '/UserData/MoreInput';
          $param->{'format'}  = 'none';
        }
        else {
          $url = '/UserData/UploadFeedback';
          $param->{'format'} = $format;
        }
      }
      else {
        $param->{'filter_module'} = 'Data';
        $param->{'filter_code'} = 'empty';
      }
    }
    else {
      $param->{'filter_module'} = 'Data';
      $param->{'filter_code'} = 'no_save';
    }
  }
  else {
    $param->{'filter_module'} = 'Data';
    $param->{'filter_code'} = 'no_data';
  }
  
{'fraction_non_alphanumeric': 0.20452871870397643, 'fraction_numerical': 0.0023932253313696614, 'mean_word_length': 2.434260429835651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 143, 'https://': 0, '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': '1210400', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1767, 'n_words': 352}