<commit_before>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::Web::Command::UserData::TrackHubRedirect;

use strict;

use EnsEMBL::Web::File::AttachedFormat::DATAHUB;

use base qw(EnsEMBL::Web::Command::UserData);

sub process {
  my $self          = shift;
  my $hub           = $self->hub;
  my $species_defs  = $hub->species_defs;
  my $session       = $hub->session;
  my $url           = $hub->param('url');
     $url           =~ s/(^\s+|\s+$)//g; # Trim leading and trailing whitespace
  my $filename      = [split '/', $url]->[-1];
  my @bits          = split /\./, $filename;
  my $extension     = $bits[-1] eq 'gz' ? $bits[-2] : $bits[-1];
  my $pattern       = "^$extension\$";
  my $redirect      = $hub->species_path($hub->data_species) . '/UserData/';
  my $params        = {};


  if ($url) {
    my $trackhub = EnsEMBL::Web::File::AttachedFormat::DATAHUB->new('hub' => $self->hub, 'url' => $url);
    
    my $standard_redirect;
    ($standard_redirect, $params) = $self->attach($trackhub, $filename); 

    ## Override standard redirect with sample location
    my $species = $hub->param('species');
    if (!$species || !$hub->species_defs->valid_species($species)) {
      $species = $species_defs->ENSEMBL_PRIMARY_SPECIES;
    }
    $redirect           = sprintf('/%s/Location/View', $species);
    my $sample_links    = $species_defs->get_config($species, 'SAMPLE_DATA');
    $params->{'r'}      = $sample_links->{'LOCATION_PARAM'} if $sample_links;

  } else {
      $session->add_data(
        type     => 'message',
        code     => 'AttachURL',
        message  => 'No URL was provided',
        function => '_error'
      );
    $redirect .= 'SelectFile';
  }
  
  $self->ajax_redirect($redirect, $params);  
}

1;<commit_msg>Use new method to check existing attachments before trying to attach a hub<commit_after>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::Web::Command::UserData::TrackHubRedirect;

use strict;

use EnsEMBL::Web::File::AttachedFormat::DATAHUB;

use base qw(EnsEMBL::Web::Command::UserData);

sub process {
  my $self          = shift;
  my $hub           = $self->hub;
  my $species_defs  = $hub->species_defs;
  my $session       = $hub->session;
  my $url           = $hub->param('url');
     $url           =~ s/(^\s+|\s+$)//g; # Trim leading and trailing whitespace
  my $filename      = [split '/', $url]->[-1];
  my @bits          = split /\./, $filename;
  my $extension     = $bits[-1] eq 'gz' ? $bits[-2] : $bits[-1];
  my $pattern       = "^$extension\$";
  my $redirect      = $hub->species_path($hub->data_species) . '/UserData/';
  my $new_action    = '';
  my $params        = {};


  if ($url) {
    ## Is this file already attached?
    ($new_action, $params)  = $self->check_attachment($url);

    unless ($new_action) {
      my $trackhub = EnsEMBL::Web::File::AttachedFormat::DATAHUB->new('hub' => $self->hub, 'url' => $url);
    
      ($new_action, $params) = $self->attach($trackhub, $filename); 
    }

    ## Override standard redirect with sample location
    my $species = $hub->param('species');
    if (!$species || !$hub->species_defs->valid_species($species)) {
      $species = $species_defs->ENSEMBL_PRIMARY_SPECIES;
    }
    $redirect           = sprintf('/%s/Location/View', $species);
    my $sample_links    = $species_defs->get_config($species, 'SAMPLE_DATA');
    $params->{'r'}      = $sample_links->{'LOCATION_PARAM'} if $sample_links;
  } else {
      $session->add_data(
        type     => 'message',
        code     => 'AttachURL',
        message  => 'No URL was provided',
        function => '_error'
      );
    $redirect .= 'SelectFile';
  }
  
  $self->ajax_redirect($redirect, $params);  
}

1;
{'fraction_non_alphanumeric': 0.14820813929945334, 'fraction_numerical': 0.007288924883579672, 'mean_word_length': 3.1235392320534223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3015931', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1521, 'n_words': 433}