From d2f6e1c3fcdaec0dfed379de28944ec8bdfb5de2 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Fri, 25 Apr 2014 11:56:04 +0200
Subject: [PATCH] [SIGNED-OFF] Bug 11317: Follow up - Update ID to allow for
 permalinking

This follow up modifies the id parameter to use a digest of the
filename to enable permalinking to files even if the array order
changes due to new files being created.

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
---
 reports/report_files.pl |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/reports/report_files.pl b/reports/report_files.pl
index 06221ce..3976ecb 100755
--- a/reports/report_files.pl
+++ b/reports/report_files.pl
@@ -29,6 +29,7 @@ use C4::Context;
 use C4::Output;
 use C4::Koha;
 use File::stat;
+use Digest::MD5 qw(md5_hex);
 
 my $input      = new CGI;
 my $file_id = $input->param("id");
@@ -50,8 +51,8 @@ else {
     my @files_list;
 	opendir(DIR, $directory);
 
-    my $i=0;
     foreach my $filename (readdir(DIR)) {
+	my $id = md5_hex($filename);
         my $full_path = "$directory/$filename";
         next if ($filename =~ /^\./ or -d $full_path);
 
@@ -59,14 +60,14 @@ else {
         push(@files_list, {name => $filename,
                            date => scalar localtime($st->mtime),
                            size => $st->size,
-                           id   => $i});
-        $i++;
+                           id   => $id});
     }
     closedir(DIR);
 
+    my %files_hash = map { $_->{id} => $_ } @files_list;
     # If we received a file_id and it is valid, send the file to the browser
-    if(defined $file_id and $file_id >= 0 and $file_id < scalar @files_list){
-        my $filename = $files_list[$file_id]->{name};
+    if(defined $file_id and $file_id >= 0 and exists $files_hash{$file_id} ){
+        my $filename = $files_hash{$file_id}->{name};
         binmode STDOUT;
         # Open the selected file and send it to the browser
         print $input->header(-type => 'application/x-download',
-- 
1.7.9.5