2008-03-29T18:03:46+13:00 (14 years ago)

Moved code that checks for name collisions between generated html (and any assoc_folder) in tmp folder with existing files/folders in import folder before trying to move

1 edited


  • gsdl/trunk/bin/script/replace_srcdoc_with_html.pl

    r15121 r15153  
    5858    my $hashParsingResult = {};
    6061    # parse the options
    6162    my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
    100101    &PrintUsage::print_txt_usage($options, "{srcreplace.params}");
    101     print STDERR "You need to specify a filename";
     102    print STDERR "You need to specify a filename\n";
    102103    die "\n";
    103104    }
    140141    # or windows_scripting did the conversion of the Word doc to html.
    141142    my $output_filename = $plugobj->tmp_area_convert_file("html", $filename);   
    142     # my $output_filename = $plugobj->tmp_area_convert_file($output_ext, $input_filename, $textref);
    144     if(!defined $output_filename || (!-e $output_filename)) { # if no output html file was created, then error message
     145    # if something went wrong, then tmp_area_convert_file returns "", but can also check
     146    # for whether the output file exists or not
     147    if(!-e $output_filename || $output_filename eq "") {
     148    # if no output html file was created, then die so that GLI displays error message
    145149    print STDERR "***replace_srcdoc_with_html.pl: no output file created for $filename ***\n";
    146150    die "No html file created for $filename. Replacement did not take place\n"; # Program NEEDS to die here,
    155159    my ($tmp_name, $tmp_dir, $ext) = &File::Basename::fileparse($output_filename, "\\.[^\\.]+\$");
    157     if($import_dir ne $tmp_dir) { # output file is not in the same folder
    158     my $assoc_folder = &util::filename_cat($tmp_dir, $tmp_name."_files");
    160     # use util to move it all to the import directory where the original file is located
    161     #&util::mv($output_filename, &util::filename_cat($import_dir, $tmp_name.$ext)); # doesn't work
    162     #&util::mv($output_filename, $import_dir); # doesn't work
    163     &util::cp($output_filename, $import_dir);
     161    # the name of the folder of associated files (which may or may not exist) in the tmp dir
     162    my $assoc_folder = &util::filename_cat($tmp_dir, $tmp_name."_files");
     164    # Need to check for naming collisions: in case there is already a file or folder
     165    # in the import directory by the name of those we want to move there from the tmp folder
     166    # First need to work out the full paths to any assoc folder if it were copied into the
     167    # import directory, and the main html file if it were copied into the import folder:
     168    my $new_assoc_folder = &util::filename_cat($import_dir,  $tmp_name."_files");
     169    my $new_file = &util::filename_cat($import_dir,  $tmp_name.$ext);
     171    # If there is an image folder, any naming collisions now would mean that the links of
     172    # the html file to the image folder would break if we changed the assoc_folder's name.
     173    # Therefore, in such a case this process dies after deleting both the file and assoc_folder.
     174    if(-e $assoc_folder && -e $new_assoc_folder) {
     175    # so an associated folder was generated, AND a folder by that name already exists
     176    # in the import folder where we want to copy the generated folder to.
    164177    &util::rm($output_filename);
    165     if(defined $assoc_folder) { # move any associated folders too
    166         print STDERR "****Folder for associated files is $assoc_folder\n";
    167         #&util::mv($assoc_folder, $import_dir); # doesn't work for me
    168         &util::cp_r($assoc_folder, $import_dir);
    169         &util::rm_r($assoc_folder);
    170     }
    171     } # else output file was created in the same folder as the input file
    172     # this happens for txt files processed by TEXTPlug
    173     # in this case, there will be no image folder AND no moving of output html file is necessary
     178    &util::rm_r($assoc_folder); # we know directory exists, so remove dir
     179    die "Image folder $new_assoc_folder already exists.\nDeleting generated file and folder, else links to images will break.\n";
     180    }
     181    # Finally, check that no file already exists with the same name as the generated stand-alone
     182    # file. Have to do this *after* checking for name collisions with any assoc_folder, because
     183    # that also tries to remove any output files.
     184    if(-e $new_file) { # a file by that name already exists, delete the generated file
     185    &util::rm($output_filename);
     186    die "File $new_file already exists. Deleting generated file.\n";
     187    }
     189    # Now we know we have no file name collisions
     190    # use util to move it all to the import directory where the original file is located
     191    #&util::mv($output_filename, &util::filename_cat($import_dir, $tmp_name.$ext)); # doesn't work
     192    #&util::mv($output_filename, $import_dir); # doesn't work
     193    &util::cp($output_filename, $import_dir);
     194    &util::rm($output_filename);
     195    if(-e $assoc_folder) { # move any associated folders too
     196    #print STDERR "****Folder for associated files is $assoc_folder\n";
     197    #&util::mv($assoc_folder, $import_dir); # doesn't work for me
     198    &util::cp_r($assoc_folder, $import_dir);
     199    &util::rm_r($assoc_folder);
     200    }
    175202    # Now we can remove the source doc permanently (there are no assocdirs for source doc)
Note: See TracChangeset for help on using the changeset viewer.