1 | # +-----------------------------------------------------------------------+ |
---|
2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
3 | # +-----------------------------------------------------------------------+ |
---|
4 | # | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
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 |
---|
28 | /; |
---|
29 | |
---|
30 | __PACKAGE__->mk_accessors(qw/ |
---|
31 | version |
---|
32 | storable_file |
---|
33 | /); |
---|
34 | |
---|
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 { |
---|
47 | $self->$_( |
---|
48 | $params->{$_} |
---|
49 | ); |
---|
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 | |
---|
64 | sub read_params { |
---|
65 | my( $self, $file, $key ) = @_ ; |
---|
66 | |
---|
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 " ; |
---|
74 | eval $expr ; |
---|
75 | } |
---|
76 | return unless 'ARRAY' eq ref $paramValues ; |
---|
77 | if($@){ |
---|
78 | die "Cannot load data $@"; |
---|
79 | } |
---|
80 | if(scalar(@$paramValues )){ |
---|
81 | my $params = $paramValues->[0] ; |
---|
82 | $self->set_key_values($params, $key); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | sub set_key_values { |
---|
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 { |
---|
107 | $_ => $self->{$_}, |
---|
108 | } |
---|
109 | @$keys |
---|
110 | }; |
---|
111 | |
---|
112 | $data; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | 1; |
---|