1 | package Uploader::TransferWorker; |
---|
2 | use threads; |
---|
3 | use threads::shared; |
---|
4 | use strict; |
---|
5 | use base qw/Uploader::Object/; |
---|
6 | use Data::Dumper; |
---|
7 | use Digest::MD5::File qw/file_md5_hex md5_hex/; |
---|
8 | |
---|
9 | $|=1; |
---|
10 | |
---|
11 | __PACKAGE__->mk_accessors(qw/ |
---|
12 | preferences |
---|
13 | pwg |
---|
14 | event_handler |
---|
15 | progress_event |
---|
16 | done_event |
---|
17 | /); |
---|
18 | |
---|
19 | |
---|
20 | sub Init { |
---|
21 | my ( $self ) =@_; |
---|
22 | |
---|
23 | $self->pwg->log_progress_cbk( |
---|
24 | sub { $self->post_image_progress_event(@_) } |
---|
25 | ); |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | sub cancel_image { |
---|
30 | my ( $self, $image ) = @_; |
---|
31 | |
---|
32 | #must check if $image is a valid hash |
---|
33 | return unless 'HASH' eq ref $image; |
---|
34 | return unless $image->{image_id}; |
---|
35 | |
---|
36 | ( $image->{status}, $image->{progress} ) = ($self->localized_msg->{"cancelled"}, 0); |
---|
37 | $self->post_image_progress_event($image); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | sub process_image { |
---|
42 | my ( $self, $handler, $progress_event, $transfer_done_event, $image) = @_; |
---|
43 | |
---|
44 | $self->event_handler( |
---|
45 | $handler |
---|
46 | ); |
---|
47 | |
---|
48 | $self->progress_event( |
---|
49 | $progress_event |
---|
50 | ); |
---|
51 | |
---|
52 | $self->done_event( |
---|
53 | $transfer_done_event |
---|
54 | ); |
---|
55 | |
---|
56 | |
---|
57 | if(!$self->exists_image($image)){ |
---|
58 | $self->insert_image($image); |
---|
59 | } |
---|
60 | else { |
---|
61 | $self->update_image($image); |
---|
62 | } |
---|
63 | |
---|
64 | $self->post_image_done_event($image); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | sub exists_image { |
---|
69 | my ( $self, $image ) = @_; |
---|
70 | |
---|
71 | $self->pwg->exists_image( |
---|
72 | $image |
---|
73 | ); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | sub insert_image { |
---|
78 | my ( $self, $image ) = @_; |
---|
79 | |
---|
80 | ( $image->{status}, $image->{progress} ) = ($self->localized_msg->{"sending files"}, 0); |
---|
81 | $self->post_image_progress_event($image); |
---|
82 | |
---|
83 | $self->add_image_files($image); |
---|
84 | $self->add_image($image); |
---|
85 | |
---|
86 | $self->post_image_progress_event($image); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | sub add_image_files { |
---|
91 | my ( $self, $image ) = @_; |
---|
92 | |
---|
93 | $self->init_image_files_checksums($image); |
---|
94 | $self->send_image_files_chunks($image); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | sub init_image_files_checksums { |
---|
99 | my ( $self, $image ) = @_; |
---|
100 | |
---|
101 | |
---|
102 | ( $image->{status}, $image->{progress} ) = ($self->localized_msg->{"checking files"}, 0); |
---|
103 | $self->post_image_progress_event($image); |
---|
104 | |
---|
105 | my @file_types = ('site_resized_file', 'site_thumb_file'); |
---|
106 | push @file_types, 'site_high_file' if $image->{upload_high}; |
---|
107 | |
---|
108 | map { |
---|
109 | $self->init_image_file_type_checksum($image, $_) |
---|
110 | }@file_types; |
---|
111 | |
---|
112 | ( $image->{status}, $image->{progress} ) = ($self->localized_msg->{"files checked"}, 100); |
---|
113 | $self->post_image_progress_event($image); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | sub init_image_file_type_checksum { |
---|
118 | my ( $self, $image, $file_type ) = @_; |
---|
119 | |
---|
120 | my $file = $image->{$file_type}; |
---|
121 | $image->{"$file_type"."_checksum"} = $self->checksum($file); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | sub checksum { |
---|
126 | my ( $self, $file ) = @_; |
---|
127 | |
---|
128 | my $file_sum; |
---|
129 | |
---|
130 | eval { |
---|
131 | $file_sum = file_md5_hex( |
---|
132 | $file |
---|
133 | ); |
---|
134 | }; |
---|
135 | |
---|
136 | $file_sum; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | sub send_image_files_chunks { |
---|
141 | my ( $self, $image ) = @_; |
---|
142 | |
---|
143 | $self->pwg->send_image_files_chunks( |
---|
144 | $image |
---|
145 | ); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | sub add_image { |
---|
150 | my ( $self, $image ) = @_; |
---|
151 | |
---|
152 | my ( $is_success, $status_line ) = $self->pwg->add_image($image); |
---|
153 | |
---|
154 | $image->{transfer_successful} = $is_success; |
---|
155 | $image->{status} = $is_success ? $self->localized_msg->{"transfered"} : $status_line ; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | sub update_image { |
---|
160 | my ( $self, $image ) = @_; |
---|
161 | |
---|
162 | |
---|
163 | ( $image->{status}, $image->{progress} ) = ($self->localized_msg->{"updating"}, 0); |
---|
164 | $self->post_image_progress_event($image); |
---|
165 | |
---|
166 | $self->update_image_files($image); |
---|
167 | $self->post_image_progress_event($image); |
---|
168 | |
---|
169 | $self->update_image_properties($image); |
---|
170 | $self->post_image_progress_event($image); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | sub update_image_files { |
---|
175 | my ( $self, $image ) = @_; |
---|
176 | |
---|
177 | if($image->{preferences}{reupload_action_files}){ |
---|
178 | ( $image->{status}, $image->{progress} ) = ($self->localized_msg->{"updating files"}, 0); |
---|
179 | $self->post_image_progress_event($image); |
---|
180 | |
---|
181 | $self->init_image_files_checksums($image); |
---|
182 | $self->update_image_modified_files($image); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | sub update_image_modified_files { |
---|
188 | my ( $self, $image ) = @_; |
---|
189 | |
---|
190 | my ( $is_success, $status_line ) = $self->pwg->update_image_modified_files($image); |
---|
191 | $image->{transfer_successful} = $is_success; |
---|
192 | $image->{status} = $is_success ? $self->localized_msg->{"files updated"} : $status_line ; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | sub update_image_properties { |
---|
197 | my ( $self, $image ) = @_; |
---|
198 | |
---|
199 | ( $image->{status}, $image->{progress} ) = ($self->localized_msg->{"updating properties"}, 0); |
---|
200 | $self->post_image_progress_event($image); |
---|
201 | |
---|
202 | my ( $is_success, $status_line ) = $self->pwg->update_image_properties($image); |
---|
203 | |
---|
204 | $image->{transfer_successful} = $is_success; |
---|
205 | $image->{status} = $is_success ? $self->localized_msg->{"properties updated"} : $status_line ; |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | sub post_image_done_event { |
---|
210 | my ( $self, $image ) = @_; |
---|
211 | |
---|
212 | $image->{progress} = 100; |
---|
213 | |
---|
214 | $self->post_image_event( |
---|
215 | $self->done_event, |
---|
216 | $image |
---|
217 | ); |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | sub post_image_progress_event { |
---|
222 | my ( $self, $image ) = @_; |
---|
223 | |
---|
224 | $self->post_image_event( |
---|
225 | $self->progress_event, |
---|
226 | $image |
---|
227 | ); |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | sub post_image_event { |
---|
232 | my ( $self, $event_type, $image ) = @_; |
---|
233 | |
---|
234 | my $thread_event = Wx::PlThreadEvent->new( |
---|
235 | -1, |
---|
236 | $event_type, |
---|
237 | shared_clone($image) |
---|
238 | ); |
---|
239 | |
---|
240 | eval { |
---|
241 | Wx::PostEvent($self->event_handler, $thread_event); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | |
---|
248 | 1; |
---|