Changeset 18319 for gsdl/trunk/perllib


Ignore:
Timestamp:
2009-01-06T18:40:53+13:00 (15 years ago)
Author:
ak19
Message:

Now plugins provide the option of base64 encoding or url encoding filenames that are to be renamed (when copied into the archives dir). Previously renamed files would always be url-encoded. URL-encoding is the default now for most plugins except MP3Plugin and OggVorbisPlugin, where the default is base64 encoding. Base64 encoding filenames upon renaming them was introduced so that more files that browsers try to open in external applications can open them, since url encoding does not seem to be implemented the same everywhere (for instance, windows media player is unable to handle url-encoded wmv filenames when such files are launched in it through the browser).

Location:
gsdl/trunk/perllib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/doc.pm

    r17057 r18319  
    4949sub new {
    5050    my $class = shift (@_);
    51     my ($source_filename, $doc_type) = @_;
     51    my ($source_filename, $doc_type, $rename_method) = @_;
    5252
    5353
     
    6767    if (defined $source_filename) {
    6868    $source_filename = &util::filename_within_collection($source_filename);
    69     $self->set_source_filename ($source_filename);
     69    print STDERR "******doc.pm::new(): no file rename method provided\n" unless $rename_method;
     70    $self->set_source_filename ($source_filename, $rename_method);
    7071    }
    7172
     
    144145sub set_source_filename {
    145146    my $self = shift (@_);
    146     my ($source_filename) = @_;
     147    my ($source_filename, $rename_method) = @_;
    147148
    148149    # Since the gsdlsourcefilename element goes into the doc.xml it has
    149150    # to be utf8. However, it should also *represent* the source filename
    150151    # (in the import directory) which may not be utf8 at all.
    151     # For instance, if this meta element (gsdlsourcefilename) will be
    152     # used by other applications that parse doc.xml in order to locate
    153     # gsdlsourcefilename. Therefore, the solution is to URLencode the real
    154     # filename as this is a binary-to-text encoding meaning that the
    155     # resulting string is ASCII (utf8).
    156    
    157 #    print STDERR "******URLencoding the gsdl_source_filename $source_filename ";
    158    
     152    # For instance, if this meta element (gsdlsourcefilename) will be used
     153    # by other applications that parse doc.xml in order to locate
     154    # gsdlsourcefilename. Therefore, the solution is to URLencode or base64
     155    # encode the real filename as this is a binary-to-text encoding meaning
     156    # that the resulting string is ASCII (utf8). Decoding will give the original.
     157   
     158#    print STDERR "******URL/base64 encoding the gsdl_source_filename $source_filename ";
     159
    159160    # URLencode just the gsdl_source_filename, not the directory. Then prepend dir
    160161    my ($srcfilename,$dirname,$suffix)
    161162    = &File::Basename::fileparse($source_filename, "\\.[^\\.]+\$");
    162163#    print STDERR "-> $srcfilename -> ";
    163     $srcfilename = &unicode::url_encode($srcfilename.$suffix);
     164    $srcfilename = &util::rename_file($srcfilename.$suffix, $rename_method);
    164165    $source_filename = &util::filename_cat($dirname, $srcfilename);
    165166#    print STDERR "$source_filename\n";
  • gsdl/trunk/perllib/strings.properties

    r17939 r18319  
    716716BasePlugin.stems:stems
    717717
     718BasePlugin.file_rename_method:The method to be used in renaming the copy of the imported file and associated files.
     719
     720BasePlugin.rename_method.url:Use url encoding in renaming files and associated files.
     721
     722BasePlugin.rename_method.base64:Use base64 encoding in renaming imported files and associated files.
     723
    718724BibTexPlugin.desc:BibTexPlugin reads bibliography files in BibTex format. BibTexPlugin creates a document object for every reference in the file. It is a subclass of SplitTextFile, so if there are multiple records, all are read.
    719725
  • gsdl/trunk/perllib/unicode.pm

    r18282 r18319  
    600600
    601601# When a filename on the filesystem is already URL-encoded, the
    602 # URL to it will have %25s in in place of every % sign, so that
     602# URL to it will have %25s in place of every % sign, so that
    603603# URLs in html pages can refer to the URL-encoded filename.
    604604# This method changes the URL reference back into the actual
     
    606606sub url_to_filename {
    607607    my ($text) =@_;
    608     $text =~ s/%25/%/g;
     608    $text =~ s/%25/%/g if is_url_encoded($text);
    609609    return $text;
    610610}
     
    619619# a url more than once this way.
    620620sub filename_to_url {
    621     my ($text) =@_;
    622    
    623     if($text !~ m/%25/) {
    624     $text =~ s/%/%25/g;
     621    my ($text, $rename_method) = @_;
     622   
     623    if(!defined $rename_method || $rename_method eq "url") {
     624    if($text !~ m/%25/) {
     625        $text =~ s/%/%25/g;
     626    }
    625627    }
    626628    return $text;
  • gsdl/trunk/perllib/util.pm

    r17714 r18319  
    2828use File::Copy;
    2929use File::Basename;
     30use MIME::Base64; # for base64 encoding
    3031
    3132use strict;
     
    994995}
    995996
     997# returns the given filename converted using either URL encoding or
     998# base64 encoding, as specified by $rename_method
     999sub rename_file {
     1000    my ($filename, $rename_method)  = @_;
     1001   
     1002    my ($tailname,$dirname,$suffix) = File::Basename::fileparse($filename, "\\.[^\\.]+\$");
     1003
     1004    if (!$rename_method) {
     1005    print STDERR "WARNING: no file renaming method specified. Defaulting to using URL encoding...\n";
     1006    # Debugging information
     1007    my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
     1008    print STDERR "Calling method; $cfilename:$cline $cpackage->$csubr\n";
     1009    }
     1010
     1011    if (!$rename_method || $rename_method eq "url") {
     1012    $tailname = &unicode::url_encode($tailname);
     1013    }
     1014    elsif ($rename_method eq "base64") {
     1015    $tailname = &MIME::Base64::encode_base64($tailname);
     1016    $tailname =~ s/\s*//sg;      # for some reason it adds spaces not just at end but also in middle
     1017    }
     1018       
     1019    $filename = "$tailname$suffix";
     1020    $filename = "$dirname$filename" if ($dirname ne "./");
     1021
     1022    return $filename;
     1023}
     1024
    99610251;
Note: See TracChangeset for help on using the changeset viewer.