From 7a1bd9e7999202e910a30520fc4c8f0e1467d4bf Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 19 Sep 2012 17:08:20 +0200 Subject: [PATCH] Bug 6874: Change filename construction method. Use _. SHA-1 to avoid name clash, filename to make it easier to find a file by its name, when not using Koha (direct filesystem access). Signed-off-by: Kyle M Hall --- C4/UploadedFiles.pm | 24 +++++++++++++++++------- opac/opac-retrieve-file.pl | 4 +--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/C4/UploadedFiles.pm b/C4/UploadedFiles.pm index a14b406..246b02b 100644 --- a/C4/UploadedFiles.pm +++ b/C4/UploadedFiles.pm @@ -58,6 +58,16 @@ use Encode; use C4::Context; +sub _get_file_path { + my ($id, $dirname, $filename) = @_; + + my $uploadPath = C4::Context->preference('uploadPath'); + my $filepath = "$uploadPath/$dirname/${id}_$filename"; + $filepath =~ s|/+|/|g; + + return $filepath; +} + =head2 GetUploadedFile my $file = C4::UploadedFiles::GetUploadedFile($id); @@ -93,6 +103,10 @@ sub GetUploadedFile { my $sth = $dbh->prepare($query); $sth->execute($id); my $file = $sth->fetchrow_hashref; + if ($file) { + $file->{filepath} = _get_file_path($file->{id}, $file->{dir}, + $file->{filename}); + } return $file; } @@ -140,13 +154,11 @@ sub UploadFile { # Test if this id already exist my $file = GetUploadedFile($id); - if($file) { + if ($file) { return $file->{id}; } - my $upload_path = C4::Context->preference("uploadPath"); - my $file_path = "$upload_path/$dir/$filename"; - $file_path =~ s#/+#/#; + my $file_path = _get_file_path($id, $dir, $filename); my $out_fh; # Create the file only if it doesn't exist @@ -186,9 +198,7 @@ sub DelUploadedFile { my $file = GetUploadedFile($id); if($file) { - my $upload_path = C4::Context->preference("uploadPath"); - my $file_path = "$upload_path/$file->{dir}/$file->{filename}"; - $file_path =~ s#/+#/#; + my $file_path = $file->{filepath}; my $file_deleted = 0; unless( -f $file_path ) { warn "Id $file->{id} is in database but not in filesystem, removing id from database"; diff --git a/opac/opac-retrieve-file.pl b/opac/opac-retrieve-file.pl index 9904199..ddbf1ac 100755 --- a/opac/opac-retrieve-file.pl +++ b/opac/opac-retrieve-file.pl @@ -29,9 +29,7 @@ my $id = $input->param('id'); my $file = C4::UploadedFiles::GetUploadedFile($id); exit 1 if not $file; -my $upload_path = C4::Context->preference("uploadPath"); -my $file_path = "$upload_path/$file->{dir}/$file->{filename}"; -$file_path =~ s#/+#/#; +my $file_path = $file->{filepath}; if( -f $file_path ) { open FH, '<', $file_path or die "Can't open file: $!"; -- 1.7.2.5