diff options
author | Dave Arter <davea@mysociety.org> | 2018-09-26 16:37:40 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2018-09-28 16:19:47 +0100 |
commit | 561e01b9b51b62e2566d80cd63d308f9a4f82822 (patch) | |
tree | a5f5a9254e3c3f1536a7300b45582ad2ceae51d8 /perllib/FixMyStreet | |
parent | 9b2b8675f6dfa22c45d609349f97730a33c975d3 (diff) |
Factor out photo storage backend init checks
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r-- | perllib/FixMyStreet/App.pm | 17 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Model/PhotoSet.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/PhotoStorage.pm | 13 | ||||
-rw-r--r-- | perllib/FixMyStreet/PhotoStorage/FileSystem.pm | 16 |
4 files changed, 35 insertions, 18 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index afc8bd918..051308920 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -10,6 +10,7 @@ use Memcached; use FixMyStreet::Map; use FixMyStreet::Email; use FixMyStreet::Email::Sender; +use FixMyStreet::PhotoStorage; use Utils; use Path::Tiny 'path'; @@ -128,19 +129,9 @@ after 'prepare_headers' => sub { __PACKAGE__->log->disable('debug') # unless __PACKAGE__->debug; -# Check upload_dir -# TODO: Should this check be part of PhotoStorage::FileSystem? -if ( - FixMyStreet->config('PHOTO_STORAGE_BACKEND') eq 'FileSystem' || - !defined FixMyStreet->config('PHOTO_STORAGE_BACKEND') # Backwards compatibility - ) { - my $cache_dir = FixMyStreet->config('PHOTO_STORAGE_OPTIONS')->{UPLOAD_DIR} - || FixMyStreet->config('UPLOAD_DIR'); - $cache_dir = path($cache_dir)->absolute(FixMyStreet->path_to()); - $cache_dir->mkpath; - unless ( -d $cache_dir && -w $cache_dir ) { - warn "\x1b[31mCan't find/write to photo cache directory '$cache_dir'\x1b[0m\n"; - } +# Set up photo storage +unless ( FixMyStreet::PhotoStorage::backend->init() ) { + warn "\x1b[31mCan't set up photo storage backend\x1b[0m\n"; } =head1 NAME diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm index 58e1a135d..21bde52d8 100644 --- a/perllib/FixMyStreet/App/Model/PhotoSet.pm +++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm @@ -16,6 +16,8 @@ use IPC::Cmd qw(can_run); use IPC::Open3; use MIME::Base64; +use FixMyStreet::PhotoStorage; + has c => ( is => 'ro', ); @@ -65,10 +67,7 @@ has storage => ( is => 'ro', lazy => 1, default => sub { - my $class = 'FixMyStreet::PhotoStorage::'; - $class .= FixMyStreet->config('PHOTO_STORAGE_BACKEND') || 'FileSystem'; - eval "use $class"; - return $class->new(); + return FixMyStreet::PhotoStorage::backend; } ); diff --git a/perllib/FixMyStreet/PhotoStorage.pm b/perllib/FixMyStreet/PhotoStorage.pm index 99f0bdab6..a441fb718 100644 --- a/perllib/FixMyStreet/PhotoStorage.pm +++ b/perllib/FixMyStreet/PhotoStorage.pm @@ -2,7 +2,18 @@ package FixMyStreet::PhotoStorage; use Moose; use Digest::SHA qw(sha1_hex); - +use Module::Load; +use FixMyStreet; + +our $instance; # our, so tests can set to undef when testing different backends +sub backend { + return $instance if $instance; + my $class = 'FixMyStreet::PhotoStorage::'; + $class .= FixMyStreet->config('PHOTO_STORAGE_BACKEND') || 'FileSystem'; + load $class; + $instance = $class->new(); + return $instance; +} sub detect_type { my ($self, $photo) = @_; diff --git a/perllib/FixMyStreet/PhotoStorage/FileSystem.pm b/perllib/FixMyStreet/PhotoStorage/FileSystem.pm index 0600f867d..d61a26c7a 100644 --- a/perllib/FixMyStreet/PhotoStorage/FileSystem.pm +++ b/perllib/FixMyStreet/PhotoStorage/FileSystem.pm @@ -16,6 +16,22 @@ has upload_dir => ( }, ); +=head2 init + +Creates UPLOAD_DIR and checks it's writeable. + +=cut + +sub init { + my $self = shift; + my $cache_dir = $self->upload_dir; + $cache_dir->mkpath; + unless ( -d $cache_dir && -w $cache_dir ) { + warn "\x1b[31mCan't find/write to photo cache directory '$cache_dir'\x1b[0m\n"; + return; + } + return 1; +} =head2 get_file |