[Bps-public-commit] rt-extension-externalstorage branch, support-box-dot-com, created. 0.61-1-g9c570e0

Dave Goehrig dave at bestpractical.com
Fri Sep 9 13:26:42 EDT 2016


The branch, support-box-dot-com has been created
        at  9c570e03f69ef778037999006f6e02ff796fd571 (commit)

- Log -----------------------------------------------------------------
commit 9c570e03f69ef778037999006f6e02ff796fd571
Author: Dave Goehrig <dave at bestpractical.com>
Date:   Fri Sep 9 13:26:33 2016 -0400

    return backend specific keys

diff --git a/lib/RT/Extension/ExternalStorage.pm b/lib/RT/Extension/ExternalStorage.pm
index cfac2d8..67eb786 100644
--- a/lib/RT/Extension/ExternalStorage.pm
+++ b/lib/RT/Extension/ExternalStorage.pm
@@ -180,10 +180,9 @@ sub Store {
     my $class = shift;
     my $content = shift;
 
-    my $key = Digest::SHA::sha256_hex( $content );
-    my ($ok, $msg) = $BACKEND->Store( $key => $content );
-    return ($ok, $msg) unless defined $ok;
-
+    my $sha = Digest::SHA::sha256_hex( $content );
+    my ($key, $msg) = $BACKEND->Store( $sha => $content );
+    return ($key, $msg) unless defined $key;
     return ($key);
 }
 
@@ -199,6 +198,9 @@ my $__DecodeLOB = __PACKAGE__->can('_DecodeLOB');
     my $Content         = shift;
     my $Filename        = @_;
 
+
+    RT->Logger->info("determinging what to do with $Filename, $ContentEncoding");
+
     return $__DecodeLOB->($self, $ContentType, $ContentEncoding, $Content, $Filename)
         unless $ContentEncoding eq "external";
 
diff --git a/lib/RT/Extension/ExternalStorage/AmazonS3.pm b/lib/RT/Extension/ExternalStorage/AmazonS3.pm
index 773ab4d..3bec29f 100644
--- a/lib/RT/Extension/ExternalStorage/AmazonS3.pm
+++ b/lib/RT/Extension/ExternalStorage/AmazonS3.pm
@@ -115,13 +115,13 @@ sub Store {
     my ($sha, $content) = @_;
 
     # No-op if the path exists already
-    return (1) if $S3->bucket($self->{Bucket})->head_key( $sha );
+    return ($sha) if $S3->bucket($self->{Bucket})->head_key( $sha );
 
     $S3->bucket($self->{Bucket})->add_key(
         $sha => $content
     ) or return (undef, "Failed to write to AmazonS3: " . $S3->errstr);
 
-    return (1);
+    return ($sha);
 }
 
 =head1 NAME
diff --git a/lib/RT/Extension/ExternalStorage/Box.pm b/lib/RT/Extension/ExternalStorage/Box.pm
new file mode 100644
index 0000000..5509202
--- /dev/null
+++ b/lib/RT/Extension/ExternalStorage/Box.pm
@@ -0,0 +1,147 @@
+# vim: ai ts=4 sts=4 et sw=4 ft=perl
+# BEGIN BPS TAGGED BLOCK {{{
+#
+# COPYRIGHT:
+#
+# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
+#                                          <sales at bestpractical.com>
+#
+# (Except where explicitly superseded by other copyright notices)
+#
+#
+# LICENSE:
+#
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+#
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
+# CONTRIBUTION SUBMISSION POLICY:
+#
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+#
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+#
+# END BPS TAGGED BLOCK }}}
+
+use 5.008003;
+use warnings;
+use strict;
+
+package RT::Extension::ExternalStorage::Box;
+
+use Role::Basic qw/with/;
+with 'RT::Extension::ExternalStorage::Backend';
+
+our ( $Box );
+
+sub Init {
+    my $self = shift;
+    my %self = %{$self};
+    
+    if (not Storage::Box->require) {
+        RT->Logger->error("Required module Storage::Box is not installed");
+        return;
+    } elsif (not $self{KeyId}) {
+        RT->Logger->error("KeyId not provided for Box.com");
+        return;
+    } elsif (not $self{EnterpriseId}) {
+        RT->Logger->error("EnterpriseId not provided for Box.com");
+        return;
+    } elsif (not $self{PrivateKey}) {
+        RT->Logger->error("PrivateKey not provided for Box.com");
+        return;
+    } elsif (not $self{Password}) {
+        RT->Logger->error("Password not provided for Box.com");
+        return;
+    } elsif (not $self{ClientId}) {
+        RT->Logger->error("ClientId not provided for Box.com");
+        return;
+    } elsif (not $self{ClientSecret}) {
+        RT->Logger->error("ClientSecret not provided for Box.com");
+        return;
+    }
+
+    $Box = Storage::Box->new( 
+        key_id => $self{KeyId},
+        enterprise_id => $self{EnterpriseId},
+        private_key => $self{PrivateKey},
+        password => $self{Password},
+        client_id => $self{ClientId},
+        client_secret => $self{ClientSecret}
+    );
+
+    # the first time we run, we create a 'rt' app user at Box.com
+    # this user id get stored for all future file access
+    # if this id is lost, it will create a new rt user but
+    # that user won't have access to any of the files!!!
+    my $BoxUserId = RT->System->FirstAttribute("BoxUserId");
+    my $user_id = $BoxUserId ? $BoxUserId->Content || '' : '';
+    if ($user_id eq '') {
+        $user_id = $Box->create_user('rt');
+	RT->Logger->info("Box.com rt user id $user_id");
+        RT->System->SetAttribute(
+            Name => "BoxUserId",
+            Description => "User ID for the Box.com rt user",
+            Content => $user_id
+        );
+    }
+    $Box->user_id($user_id);
+    
+
+    return $self;
+}
+
+sub Get {
+    my ($self,$key) = @_;
+    RT->Logger->info("Downloading $key");
+    my $contents = $Box->download_file($key);
+    $contents; 
+}
+
+sub Store {
+    my ($self,$key,$content) = @_;
+
+    RT->Logger->info("Box Storing $key");
+    # we need to store the file locally for libcurl to be able to upload it
+    # this is a limitation of WWW::Curl::Form module, as it lacks support
+    # for the CURLFORM_BUFFER and CURLFORM_BUFFERPTR options, rather it only
+    # supports CURLFORM_FILE and CURLFORM_FILENAME.  But atleast it will upload
+    # large files.
+    open( my $fh, ">:raw", $key ) or return (undef, "Cannot write file to disk: $!");
+    print $fh $content or return (undef, "Cannot write file to disk: $!");
+    close $fh or return (undef, "Cannot write file to disk: $!");
+    
+    RT->Logger->info("Created file $key");
+    my $file = $Box->create_file($key);
+    RT->Logger->info("Created file " . $file);
+
+    unlink $key;        # delete the file so we don't litter 
+    return ($file); # we return the file_id for get to get it
+}
+
+1;
+
diff --git a/lib/RT/Extension/ExternalStorage/Disk.pm b/lib/RT/Extension/ExternalStorage/Disk.pm
index e93fb84..abfe7f4 100644
--- a/lib/RT/Extension/ExternalStorage/Disk.pm
+++ b/lib/RT/Extension/ExternalStorage/Disk.pm
@@ -100,7 +100,7 @@ sub Store {
     my $dir  = $self->{Path} . "/$1/$2";
     my $path = "$dir/$3";
 
-    return (1) if -f $path;
+    return ($sha) if -f $path;
 
     File::Path::make_path($dir, {error => \my $err});
     return (undef, "Making directory failed") if @{$err};
@@ -109,7 +109,7 @@ sub Store {
     print $fh $content or return (undef, "Cannot write file to disk: $!");
     close $fh or return (undef, "Cannot write file to disk: $!");
 
-    return (1);
+    return ($sha);
 }
 
 =head1 NAME
diff --git a/lib/RT/Extension/ExternalStorage/Dropbox.pm b/lib/RT/Extension/ExternalStorage/Dropbox.pm
index d57bf6b..6e8bf46 100644
--- a/lib/RT/Extension/ExternalStorage/Dropbox.pm
+++ b/lib/RT/Extension/ExternalStorage/Dropbox.pm
@@ -97,7 +97,7 @@ sub Store {
     my ($sha, $content) = @_;
 
     # No-op if the path exists already.  This forces a metadata read.
-    return (1) if open( $DROPBOX, "<", $sha);
+    return ($sha) if open( $DROPBOX, "<", $sha);
 
     open( $DROPBOX, ">", $sha )
         or return (undef, "Open for write on dropbox failed: $!");
@@ -106,7 +106,7 @@ sub Store {
     close $DROPBOX
         or return (undef, "Flush to dropbox failed: $!");
 
-    return (1);
+    return ($sha);
 }
 
 =head1 NAME

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list