From 5c2150519fd7e44e8f05c9c329301c8bd8c4fdcb Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Tue, 12 Dec 2017 00:26:16 +0000 Subject: [PATCH] Bug 19795 Have it so you can upload files, and they are stored in swift Not ready for use in production yet --- Koha/Uploader.pm | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/Koha/Uploader.pm b/Koha/Uploader.pm index 8e6a2aa36f..87bcc50b4d 100644 --- a/Koha/Uploader.pm +++ b/Koha/Uploader.pm @@ -76,6 +76,14 @@ use C4::Koha; use Koha::UploadedFile; use Koha::UploadedFiles; + +BEGIN { + if (C4::Context->preference('SwiftStorage')){ + require Net::OpenStack::Swift; + import Net::OpenStack::Swift; + } +} + __PACKAGE__->mk_ro_accessors( qw|| ); =head1 INSTANCE METHODS @@ -204,6 +212,7 @@ sub _init { $self->{files} = {}; $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv; $self->{public} = $params->{public}? 1: undef; + $self->{swift} = C4::Context->preference('SwiftStorage'); } sub _fh { @@ -216,6 +225,45 @@ sub _fh { sub _create_file { my ( $self, $filename ) = @_; my $fh; + + if ($self->{swift}){ + # We are uploading in to a swift object store, not local files + # Authenticate to swift, bail out if we can't + # + # Maybe shift this to its own sub next + my $sw = Net::OpenStack::Swift->new( + auth_url => C4::Context->preference('SwiftStorageAuthUrl'), + user => C4::Context->preference('SwiftStorageUser'), + password => C4::Context->preference('SwiftStoragePassword'), + tenant_name => C4::Context->preference('SwiftStorageTenant'), + region => C4::Context->preference('SwiftStorageRegion') + ); + my ( $storage_url, $token ); + eval {($storage_url,$token) = $sw->get_auth();}; + if ($@){ + # we can't login, bail out + $self->{files}->{$filename}->{errcode} = 2; #not writable + return; + } + # get our container, if one doesn't exist, make it + my $container_name =C4::Context->config( 'database' ); + my $headers; + eval {$headers = $sw->header_container(container_name => $container_name);}; + if ($@){ + # container doesn't exist + eval {$headers = $sw->put_container(container_name => $container_name);}; + if ($@){ + # cant create a container bail out + $self->{files}->{$filename}->{errcode} = 2; #not writable + return; + } + } + # We have a container we can write to, we need to write the file to tmp + # Then put to the object store (as we buffer read the file) + $self->{sw} = $sw; + $self->{temporary} = 1; + } + if( $self->{files}->{$filename} && $self->{files}->{$filename}->{errcode} ) { #skip @@ -271,9 +319,23 @@ sub _done { $self->{done} = 1; foreach my $f ( keys %{ $self->{files} } ) { my $fh = $self->_fh($f); - $self->_register( $f, $fh? tell( $fh ): undef ) + my $size = undef; + if ($fh){ + $size = tell($fh); + } + $self->_register( $f, $size ) if !$self->{files}->{$f}->{errcode}; $fh->close if $fh; + if ($self->{swift}){ + my $dir = $self->_dir; + my $hashval = $self->{files}->{$f}->{hash}; + my $fn = $hashval. '_'. $f; + open $fh, '<', "$dir/$fn"; + # put the file in the object store + my $headers = $self->{sw}->put_object(container_name => C4::Context->config('database'), + object_name => $f , content => $fh, content_length => $size); + close $fh; + } } } -- 2.11.0