IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10545


Ignore:
Timestamp:
Dec 7, 2006, 5:09:11 PM (20 years ago)
Author:
Paul Price
Message:

Fixing convert_filename functions to use URI and File::Spec.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-Config/lib/PS/IPP/Config.pm

    r10531 r10545  
    11# Copyright (c) 2006  Paul Price, Joshua Hoblitt
    22#
    3 # $Id: Config.pm,v 1.12 2006-12-07 22:47:12 price Exp $
     3# $Id: Config.pm,v 1.13 2006-12-08 03:09:11 price Exp $
    44
    55package PS::IPP::Config;
     
    1414use PS::IPP::Metadata::Config 0.07;
    1515use Getopt::Long qw( GetOptions :config gnu_getopt pass_through ); # Set pass_through so we don't kill @ARGV
     16use URI;
    1617
    1718use base qw( Class::Accessor::Fast );
     
    108109    my $self = shift;           # Configuration object
    109110    my $name = shift;           # raw name
    110     my $base;
    111 
    112     # strip off file:// prefix
    113     $name =~ s|^file://||;
    114    
    115     # replace path://PATH with datapath lookup
    116     if ($name =~ m|^path://|) {
    117         ($base) = $name =~ m|^path://(.*)|;
    118         my @list = split ("/", $base);
    119         my $path = shift @list;
    120         my $realpath = $self->datapath($path);
    121         unshift @list, $realpath;
    122         $name = join ("/", @list);
    123     }
    124 
    125     $name =~ s|/{2,}|/|g;               # Remove double slashes
    126 
    127     return $name;
     111
     112    my $uri = URI->new($name) or die "Unable to parse URI: $name\n"; # URI
     113    my $scheme = $uri->scheme(); # Scheme, e.g., file, path
     114    if (lc($scheme) eq 'file') {
     115        return File::Spec->canonpath( $uri->path() );
     116    }
     117
     118    if (lc($scheme) eq 'path') {
     119        my @segments = $uri->path_segments();
     120        my $source = shift @segments;
     121        my $path = $self->datapath($path);
     122        my $rel = File::Spec->catfile( @segments );
     123        return File::Spec->rel2abs( $rel, $path );
     124    }
     125
     126    return;
    128127}
    129128
     
    133132    my $name = shift;           # raw name
    134133
    135     $name =~ s|/{2,}|/|g;               # Remove double slashes
     134    $name = File::Spec->canonpath( $name); # Clean up
     135    my ($vol, $dir, $file) = File::Spec->splitpath( $name );
     136    my @dirs = File::Spec->splitdir( $dir );
    136137
    137138    my $path_list = _mdLookupMD($self->{_ipprc}, 'DATAPATH'); # List of paths
    138139    my $best_path;
    139140    my $best_name;
    140     my $best_score = length $name;
    141     foreach my $path_item (@$path_list) {
    142         my $path_name = $path_item->{name}; # Name of the path
    143         my $path = $path_item->{value}; # The path
    144         if ($path !~ m|/$|) {
    145             $path .= '/';
    146         }
    147         my ($rest) = $name =~ m|^$path(.*)|; # Rest of the string
    148         next if not defined $rest;
    149         my $score = length $rest;
    150         if ($score < $best_score) {
    151             $best_path = $path;
    152             $best_score = $score;
    153             $best_name = $path_name;
    154         }
    155     }
    156     if ($best_score < length $name) {
    157         $name =~ s|^$best_path|path://$best_name/|;
     141    my $best_score;
     142  PATH_CHECK: foreach my $path_item (@$path_list) {
     143      my $path_name = $path_item->{name}; # Name of the path
     144      my $path = $path_item->{value}; # The path
     145      my @path_dirs = File::Spec->splitdir( $path );
     146
     147      # Check if the path is suitable
     148      next if scalar @path_dirs > scalar @dirs;
     149      for (my $i = 0; $i < scalar @path_dirs; $i++) {
     150          next PATH_CHECK if $path_dirs[$i] ne $dirs[$i];
     151      }
     152      # It is suitable; see if it is 'best' (minimizes the number of directories)
     153      my $score = scalar @path_dirs;
     154      if ($score > $best_score) {
     155          $best_path = $path;
     156          $best_score = $score;
     157          $best_name = $path_name;
     158      }
     159  }
     160    my $uri = URI->new();       # URI to return
     161    if (defined $best_score) {
     162        my $rel = File::Spec->abs2rel( $name, $best_path );
     163        $uri->scheme( 'path' );
     164        $uri->path_segments( $best_name, $rel );
    158165    } else {
    159         # Worst case scenario: can't find anything, so put in the absolute path
    160         $name = 'file://' . $name;
    161     }
    162 
    163     return $name;
     166        $uri->scheme( 'file' );
     167        $uri->path( $name );
     168    }
     169
     170    return $uri->as_string();
    164171}
    165172
Note: See TracChangeset for help on using the changeset viewer.