[2597] | 1 | # +-----------------------------------------------------------------------+ |
---|
| 2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
| 3 | # +-----------------------------------------------------------------------+ |
---|
[4779] | 4 | # | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
[2597] | 5 | # +-----------------------------------------------------------------------+ |
---|
| 6 | # | This program is free software; you can redistribute it and/or modify | |
---|
| 7 | # | it under the terms of the GNU General Public License as published by | |
---|
| 8 | # | the Free Software Foundation | |
---|
| 9 | # | | |
---|
| 10 | # | This program is distributed in the hope that it will be useful, but | |
---|
| 11 | # | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 12 | # | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 13 | # | General Public License for more details. | |
---|
| 14 | # | | |
---|
| 15 | # | You should have received a copy of the GNU General Public License | |
---|
| 16 | # | along with this program; if not, write to the Free Software | |
---|
| 17 | # | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 18 | # | USA. | |
---|
| 19 | # +-----------------------------------------------------------------------+ |
---|
| 20 | package Uploader::Object; |
---|
| 21 | use strict; |
---|
| 22 | use Data::Dumper; |
---|
| 23 | use Storable; |
---|
| 24 | use File::Slurp ; |
---|
| 25 | use Data::Dumper; |
---|
| 26 | use base qw/ |
---|
| 27 | Class::Accessor::Fast |
---|
[6426] | 28 | /; |
---|
[2597] | 29 | |
---|
[6426] | 30 | __PACKAGE__->mk_accessors(qw/ |
---|
| 31 | version |
---|
| 32 | storable_file |
---|
| 33 | /); |
---|
| 34 | |
---|
[2597] | 35 | # $param is a hash with : |
---|
| 36 | # key : member name |
---|
| 37 | sub new { |
---|
| 38 | my ( $class, $params ) = @_; |
---|
| 39 | |
---|
| 40 | $params ||= {}; |
---|
| 41 | |
---|
| 42 | my $self = bless $params, $class ; |
---|
| 43 | |
---|
| 44 | # member accessors initialization |
---|
| 45 | eval { |
---|
| 46 | map { |
---|
[6426] | 47 | $self->$_( |
---|
| 48 | $params->{$_} |
---|
| 49 | ); |
---|
[2597] | 50 | } |
---|
| 51 | keys %$params; |
---|
| 52 | }; |
---|
| 53 | if($@){ |
---|
| 54 | warn $@; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | # must be defined in child class |
---|
| 58 | $self->Init(); |
---|
| 59 | |
---|
| 60 | return $self; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
[6426] | 64 | sub read_params { |
---|
| 65 | my( $self, $file, $key ) = @_ ; |
---|
[2597] | 66 | |
---|
[6426] | 67 | my $expr_params ; |
---|
| 68 | eval { $expr_params = read_file( $file ); } ; |
---|
| 69 | |
---|
| 70 | my $paramValues = [] ; |
---|
| 71 | if($expr_params){ |
---|
| 72 | my $expr = '$paramValues = ' ; |
---|
| 73 | $expr .= "$expr_params " ; |
---|
[2597] | 74 | eval $expr ; |
---|
| 75 | } |
---|
[6426] | 76 | return unless 'ARRAY' eq ref $paramValues ; |
---|
[2597] | 77 | if($@){ |
---|
| 78 | die "Cannot load data $@"; |
---|
| 79 | } |
---|
[6426] | 80 | if(scalar(@$paramValues )){ |
---|
| 81 | my $params = $paramValues->[0] ; |
---|
| 82 | $self->set_key_values($params, $key); |
---|
| 83 | } |
---|
[2597] | 84 | } |
---|
| 85 | |
---|
[6426] | 86 | |
---|
| 87 | sub set_key_values { |
---|
[2597] | 88 | my ( $self, $params, $key )= @_; |
---|
| 89 | if (defined $key) { |
---|
| 90 | foreach( keys %$params ) { |
---|
| 91 | $self->{$key}->{$_} = $params->{$_} ; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | else { |
---|
| 95 | foreach( keys %$params ) { |
---|
| 96 | $self->{$_} = $params->{$_} ; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | sub get_storable { |
---|
| 103 | my ( $self, $keys ) = @_; |
---|
| 104 | |
---|
| 105 | my $data = { |
---|
| 106 | map { |
---|
[6426] | 107 | $_ => $self->{$_}, |
---|
[2597] | 108 | } |
---|
| 109 | @$keys |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | $data; |
---|
| 113 | } |
---|
| 114 | |
---|
[6426] | 115 | |
---|
[2597] | 116 | 1; |
---|