Index: extensions/pLoader/trunk/src/Uploader/PWG/WebServices.pm
===================================================================
--- extensions/pLoader/trunk/src/Uploader/PWG/WebServices.pm	(revision 4422)
+++ extensions/pLoader/trunk/src/Uploader/PWG/WebServices.pm	(revision 4467)
@@ -61,8 +61,12 @@
            upload_high
            chunk_size
-           sum_high_file
-           sum_resized_file
-           sum_thumb_file
-           sum_original_file
+           sum
+           image_id
+           typecode
+		   reupload_action_files
+		   reupload_action_properties
+		   reupload_action_properties_m
+		   single_value_mode
+		   multiple_value_mode
       / 
 );
@@ -72,5 +76,28 @@
 sub Init {
     my ( $self, $version ) = @_ ;
- 
+
+    # to transform a type_code into a typename
+    $self->typecode(
+        {
+             file => 'file',
+             thumbnail => 'thumb',
+             high => 'high',	
+        }
+    );
+	
+	$self->single_value_mode(
+	    {
+		    1 => 'fill_if_empty',
+			2 => 'replace',
+		}
+	);
+
+	$self->multiple_value_mode(
+	    {
+		    1 => 'append',
+			2 => 'replace',
+		}
+	);
+	
     $self->uagent(
         LWP::UserAgent->new(
@@ -125,40 +152,50 @@
     };
  
-    $self->uagent_response(
-        $self->uagent->post(
-            $self->urlbase.'/ws.php?format=json',
-            $form
-        )
-    );
-    
+    $self->_execute_post(
+        $form
+    );    
+
+    $self->login_result(
+        $self->_json_response_content
+    );
+
+}
+
+sub _json_response_content {
+    my ( $self ) = @_;
+
     my $hresult = {} ;
 
     if($self->uagent_response->is_success){
+        # when server response has warnings, the content response is not a valid json string
+		# find the json response
+		#printf("%s\n", $self->uagent_response->content);
+        $self->uagent_response->content =~ /(\{.+\})/;
+		#printf("json response %s\n", $1);
         eval {
             $hresult = from_json(
-                $self->uagent_response->content
+                $1
             );
         };
+		
     }
     else{
-        $hresult = {
+	$hresult = {
             'message' => $self->uagent_response->message,
             'stat'    => 'fail',
         };
     }
-
-    $self->login_result(
-        $hresult
-    );
-
-}
-
-sub GetCategories {
-    my ( $self ) = @_;
-
-    my $result;
+	
+	$hresult;
+}
+
+sub _execute_get {
+    my ( $self, $query ) = @_;
+
     eval {
-        $result = $self->uagent->get(
-            $self->qry_list_categories
+        $self->uagent_response(
+            $self->uagent->get(
+                $query
+		    )
         );
     };
@@ -166,50 +203,49 @@
     if($@){
         printf("An error occured in query execution %s\n%s",
-            $self->qry_list_categories,
+            $query,
             $@,
         );	
     }
-    my $hresult;
-    
-    eval {
-    	$hresult = from_json(
-            $result->content
-        );
-    };
-    
-    $hresult ||= {};
-
-    my $categories = $hresult->{result}{categories};
-
-    $categories;	
-}
-
-sub GetTags {
-    my ( $self ) = @_;
+}
+
+sub _execute_post {
+    my ( $self, $form ) = @_;
 
     my $result;
     eval {
-        $result = $self->uagent->get(
-            $self->qry_list_tags
+        $result = $self->uagent_response(
+            $self->uagent->post(
+                $self->urlbase.'/ws.php?format=json',
+                $form
+            )
         );
-    };
+	};
 
     if($@){
-        printf("An error occured in query execution %s\n%s",
-            $self->qry_list_tags,
+        printf("An error occured in post execution %s\n%s",
+            $form->{method},
             $@,
         );	
     }
-    my $hresult;
-    
-    eval {
-    	$hresult = from_json(
-            $result->content
-        );
-    };
-    
-    $hresult ||= {};
-
-    $hresult->{result}{tags};
+	
+	return ( $result->is_success, $result->status_line );
+}
+sub GetCategories {
+    my ( $self ) = @_;
+
+ 
+    $self->_execute_get(
+	    $self->qry_list_categories
+	);
+    $self->_json_response_content->{result}{categories};
+}
+
+sub GetTags {
+    my ( $self ) = @_;
+
+    $self->_execute_get(
+        $self->qry_list_tags	
+	);
+    $self->_json_response_content->{result}{tags};
 }
 
@@ -223,18 +259,5 @@
     };
 
-    my $result = $self->uagent->post(
-        $self->urlbase.'/ws.php?format=json',
-        $form
-    );
-
-    my $content = {};
-        eval {
-            $content = from_json(
-                $result->content
-            );
-        };
-
-    return ( $result->is_success, $result->status_line, $content );
-	
+    return ( $self->_execute_post($form), $self->_json_response_content );
 } 
 
@@ -243,57 +266,34 @@
     my ( $self, $progress ) = @_;
 
-    $self->sum_resized_file(
-        $self->_checksum(
-            $self->site_resized_file,
-            $progress
-        )
-    );
-
-    $self->sum_thumb_file(
-        $self->_checksum(
-            $self->site_thumb_file,
-            $progress
-        )
-    );
- 
-    $self->sum_high_file(
-        $self->_checksum(
-            $self->site_high_file,
-            $progress
-        )
-    );
-    # is the same in pLoader but may be different 
-    $self->sum_original_file(
-        $self->sum_high_file
-    );
-
-
-    my $site_image_name = $self->site_image_name; 
-
-    my $image_id = $self->_exists($self->sum_original_file);;
-    my $status = 0;
-    my $status_line = "An error has occured during upload.";
+    $self->image_id(
+        $self->_exists($progress->{original_sum})
+    );
+
+    my $status = 1;
+    my $status_line ="OK";
     my $content = {};
     my $doubleCheck;
     my $form;
-    while(1){
-        if(!defined($image_id)){
-        	
-            my $resized_params = $self->_send_chunks('file', $progress);
-            $status_line = $resized_params->{message};
-            last if !$resized_params->{ok};
+    UPLOAD: while(1){
+        # first upload
+        if(!defined($self->image_id)){ 
+            $doubleCheck = 1;
+            $self->_checksum_files($progress);
+            my @types = ('file', 'thumb');
+            push @types, 'high' if $self->upload_high;
+            map{
+                my $rval = $self->_send_chunks($_, $progress);
+                $status_line = $rval->{message};
+                last UPLOAD if !$rval->{ok};
+            } @types;
           
-            my $thumb_params = $self->_send_chunks('thumb', $progress);
-            $status_line = $thumb_params->{message};
-            last if !$thumb_params->{ok};
        	
             $form = {
                 method            => 'pwg.images.add',
-                original_sum      => $self->sum_original_file,
-                file_sum          => $self->sum_resized_file,
-                thumbnail_sum     => $self->sum_thumb_file,
+                original_sum      => $self->sum->{original},
+                file_sum          => $self->sum->{file},
+                thumbnail_sum     => $self->sum->{thumb},
                 categories        => $self->categories,
-                name              => $site_image_name,
-                rank              => $self->rank,
+                name              => $self->site_image_name,
                 author            => $self->site_author,
                 comment           => $self->site_comment,
@@ -302,28 +302,45 @@
                 
             };
-    
-            if($self->upload_high){
-                my $high_params = $self->_send_chunks('high', $progress);
-                $status_line = $high_params->{message};
-                last if !$high_params->{ok};
-				# high is a HD resized copy of original
-				$form->{high_sum} = $self->sum_high_file;
+
+           $form->{high_sum} = $self->sum->{high} if $self->upload_high;
+		   
+           $progress->{yield}->();
+           ( $status, $status_line ) = $self->_execute_post($form); 
+		}
+        # re-upload
+        else{
+		    #printf("reupload, files %s, properties %s\n", $self->reupload_action_files, $self->reupload_action_properties);
+            # need to check if files have changed
+            # and update image info
+            if($self->reupload_action_files){
+                $self->_checksum_files($progress);
+                my $files = $self->_check_files();
+                if(defined($files)){
+                    $self->_add_files($files, $progress);	
+                }
             }
-            $doubleCheck = 1;
-        }
-        else {
+
             $form = {
-                method            => 'pwg.images.setInfo',
-                image_id          => $image_id,
-                categories        => $self->categories,
-                name              => $site_image_name,
-                rank              => $self->rank,
-                author            => $self->site_author,
-                comment           => $self->site_comment,
-                date_creation     => $self->site_img_date_creation,
-                tag_ids           => $self->site_tags,
+                    method        => 'pwg.images.setInfo',
+                    image_id      => $self->image_id,
+		    };
+            # update metadata info
+            if($self->reupload_action_properties){  	
+			    #printf("reupload_action_properties %s\n", $self->reupload_action_properties);
+                $form->{name} = $self->site_image_name;
+                $form->{author}        = $self->site_author;
+                $form->{comment}       = $self->site_comment;
+                $form->{date_creation} = $self->site_img_date_creation;
+				$form->{single_value_mode}  = $self->single_value_mode->{$self->reupload_action_properties};
+		    }
+            if($self->reupload_action_properties_m){  	
+			    #printf("reupload_action_properties_m %s\n", $self->reupload_action_properties_m);
+                $form->{tag_ids} = $self->site_tags if $self->site_tags;
+                $form->{categories} = $self->categories;
+				$form->{multiple_value_mode} = $self->multiple_value_mode->{$self->reupload_action_properties_m};
             };
             $progress->{yield}->();
-        }
+            ( $status, $status_line ) = $self->_execute_post($form); 
+	    }
 
         delete $form->{tag_ids} unless defined $self->site_tags;
@@ -331,26 +348,118 @@
 
         $progress->{yield}->();
-        my $result = $self->uagent->post(
-            $self->urlbase.'/ws.php?format=json',
-            $form
-        );
-        eval {
-            $content = from_json(
-                $result->content
-            );
-        };
-
-    
-        $status = $result->is_success;
-        $status_line = $result->status_line; 
-
-        if($doubleCheck){
-            $content->{stat} = 'fail' if !$self->_exists($self->sum_original_file); 
+	    #print "form ", Dumper $form;
+		#print "status ", $status, "\n";
+		#print "status_line ", $status_line, "\n";
+	    # basic check, should perform a checkfile
+        $content->{stat} = !$self->_exists($progress->{original_sum}) ? 'fail' : 'ok'; 
+
+		last UPLOAD;
+    }# UPLOAD
+    
+    return ( $status,  $status_line, $content);
+}
+
+sub _checksum_files {
+    my ( $self, $progress ) = @_;
+
+    $self->sum(
+        {
+            file => $self->_checksum(
+                        $self->site_resized_file,
+                        $progress
+                    ),
+            thumb => $self->_checksum(
+                             $self->site_thumb_file,
+                             $progress
+                         ),
+            high => $self->_checksum(
+                        $self->site_high_file,
+                        $progress
+                    ),
+            original => $progress->{original_sum}
         }
-
-        last;
+    ); 
+}
+
+sub _check_files {
+    my ( $self ) = @_;
+
+    my $form = {
+        method   => 'pwg.images.checkFiles',
+        image_id => $self->image_id,
+    };
+    
+    @$form{'thumbnail_sum', 'file_sum' } = (
+        $self->sum->{thumb},
+        $self->sum->{file},
+    );
+
+    if($self->upload_high){
+        $form->{high_sum} = $self->sum->{high};
     }
-    
-    return ( $status,  $status_line, $content);
+
+    $self->_execute_post($form);    
+    my $hresult = $self->_json_response_content;
+
+    my $rval = 'ok' eq $hresult->{stat} ? $hresult->{result} : undef;
+    
+    $rval;
+}
+
+# $files is returned by _check_files
+# {
+#     thumbnail => 'equals', 'differs', 'missing'
+#     file => 'equals', 'differs', 'missing'
+#     high => 'equals', 'differs', 'missing'
+#}
+
+sub _add_files {
+    my ( $self, $files, $progress ) = @_;
+
+    map{
+        $self->_add_file($_, $progress);
+    }
+    map{
+        $self->typecode->{$_};
+    } grep { 'equals' ne $files->{$_} } keys %$files ;
+    	
+}
+
+sub _add_file {
+    my ( $self, $type_code, $progress ) = @_;
+
+    $self->_send_chunks(
+        $type_code,
+        $progress,
+    );
+
+    my $form = {
+        method => 'pwg.images.addFile',
+        image_id => $self->image_id,
+        type     => $type_code,
+        sum      => $self->sum->{$type_code},    	
+    };	
+
+    $self->_execute_post($form);    
+    my $hresult = $self->_json_response_content;
+print Dumper $hresult;
+    my $rval = 'ok' eq $hresult->{stat} ? $hresult->{result} : undef;
+    
+    $rval;
+}
+
+sub IsAlreadyUploaded {
+    my ( $self, $md5_sums ) = @_;
+	
+	# md5_sums is an array ref
+	$self->_execute_post({
+        method      => 'pwg.images.exist',
+        md5sum_list => join(',', @$md5_sums)
+	    }
+	);
+
+	my $sums = $self->_json_response_content->{result};
+
+    $sums;
 }
 
@@ -363,18 +472,10 @@
     };
 
-    my $result = $self->uagent->post(
-        $self->urlbase.'/ws.php?format=json',
-        $form
-    );
-
-    my $hresult = {};
-    eval {
-        $hresult = from_json(
-            $result->{_content}
-        );
-    };
-    my $id;
-    $id = $hresult->{result}{$self->sum_original_file} if 'ok' eq $hresult->{stat};
-    
+    $self->_execute_post($form);    
+    my $hresult = $self->_json_response_content;
+
+	$hresult->{result} = {} if 'HASH' ne ref $hresult->{result};
+    my $id = 'ok' eq $hresult->{stat} ? $hresult->{result}{$md5_sum} : undef ;
+
     $id;
 } 
@@ -405,5 +506,5 @@
 
 sub _send_chunks {
-    my ( $self, $type, $progress ) = @_;
+    my ( $self, $type_code, $progress ) = @_;
 
     my $msg = {
@@ -419,13 +520,13 @@
     };
 
-    $progress->{current_msg} = $progress->{$msg->{$type}};
+    $progress->{current_msg} = $progress->{$msg->{$type_code}};
     $progress->{yield}->();
 
     my $params = {
-         filepath      => $filepath->{$type},
-         type          => $type,
-         original_sum  => $self->sum_original_file,
-    };
-
+         filepath      => $filepath->{$type_code},
+         type          => $type_code,
+         original_sum  => $self->sum->{original},
+    };
+print Dumper $params;
     $self->send_chunks(
        $params,
@@ -446,19 +547,6 @@
         
     };
-
-    my $result = $self->uagent->post(
-        $self->urlbase.'/ws.php?format=json',
-        $form
-    );
-
-    my $content = {};
-        eval {
-            $content = from_json(
-                $result->content
-            );
-        };
-
-    return ( $result->is_success, $result->status_line, $content );
 	
+    return ( $self->_execute_post($form), $self->_json_response_content );
 }
 
@@ -474,18 +562,5 @@
     };
 
-    my $result = $self->uagent->post(
-        $self->urlbase.'/ws.php?format=json',
-        $form
-    );
-
-    my $content = {};
-        eval {
-            $content = from_json(
-                $result->content
-            );
-        };
-
-    return ( $result->is_success, $result->status_line, $content );
-	
+    return ( $self->_execute_post($form), $self->_json_response_content );
 }
 
