Index: /extras/pLoader/trunk/src/Uploader/Image.pm
===================================================================
--- /extras/pLoader/trunk/src/Uploader/Image.pm	(revision 3225)
+++ /extras/pLoader/trunk/src/Uploader/Image.pm	(revision 3226)
@@ -39,4 +39,5 @@
         preview_w
         preview_h
+        file_sum
     /
 );
Index: /extras/pLoader/trunk/src/Uploader/ImageList.pm
===================================================================
--- /extras/pLoader/trunk/src/Uploader/ImageList.pm	(revision 3225)
+++ /extras/pLoader/trunk/src/Uploader/ImageList.pm	(revision 3226)
@@ -28,4 +28,7 @@
 use Data::Dumper;
 use Storable;
+use Digest::MD5::File qw/file_md5_hex md5_hex/;
+use Encode qw/encode decode is_utf8/;
+
 # this class implements a collection of image files with associated data
 $|=1;
@@ -68,4 +71,5 @@
                 progressbar_refresh
                 progress_endinfo_refresh
+                RescaleCallback
                 ResizeCallback
                 upload_rejects
@@ -78,4 +82,8 @@
                 create_resized
                 use_exif_preview
+                image_sums
+                sums
+                version
+                imagelist_version
      /
 );
@@ -96,4 +104,5 @@
     # If PreviewImage is available, we use it
     if(defined $imagedata ) {
+        print "_write_preview_image, use exif PreviewImage\n";
     	eval {
             open PREVIEW_FILE, ">", $self->current_image->preview_file ;
@@ -126,5 +135,5 @@
 }
 
-sub _set_current_image_filepaths {
+sub _set_current_image_filepaths__ {
     my ( $self ) = @_;
 
@@ -134,4 +143,5 @@
 
     my ( $filename, $ext ) = split /\./, $file ;
+    
 
     $self->current_image->wx_thumb_file( 
@@ -156,5 +166,7 @@
         )
     );
-
+    $self->current_image->preview_file( 
+        encode('iso-8859-1', $self->current_image->preview_file)
+    );
 
     $self->current_image->site_thumb_file( 
@@ -169,6 +181,42 @@
     );
 
-	
-}
+}
+
+sub _set_current_image_filepaths {
+    my ( $self ) = @_;
+
+    my $filename = sprintf(
+        "%s.%s",
+        $self->current_image->file_sum,
+        $self->type,
+    );
+
+
+    $self->current_image->wx_thumb_file( 
+        File::Spec->catfile(
+            $self->wx_thumb_dir,
+            $filename
+        )
+    );
+
+    $self->current_image->preview_file( 
+        File::Spec->catfile(
+            $self->preview_dir,
+            $filename
+        )
+    );
+    $self->current_image->preview_file( 
+        encode('iso-8859-1', $self->current_image->preview_file)
+    );
+
+    $self->current_image->site_thumb_file( 
+        File::Spec->catfile(
+            $self->site_thumb_dir,
+            $filename
+        )
+    );
+
+}
+
 
 sub SetCurrentImage {
@@ -176,5 +224,5 @@
 
     $self->current_image(
-        $self->images->[$indx]
+        $self->GetImage($indx)
     );
 }
@@ -187,99 +235,36 @@
 
     # if some files have been previously selected 
-    my $i = scalar @{$self->images};
+    my $i = scalar @{$self->sums};
     my $count = 0;
     $self->count($count);
     my $errors = 0;
+
     map {
-        # read exif metadata
-        my $info;
-
-
-        eval {
-            $info = ImageInfo( $_ );
-        };
-
-        $info = {} if($@);
-
-       	$self->_add_image($_, $info, $i);	
+    	my $info = $self->_read_exif_metatdata($_);
+       	my $is_new_image = $self->_add_image($_, $info, $i);	
         $self->SetCurrentImage($i);
     	$self->_set_current_image_filepaths();
 
-        my $create_gui_preview;
-        if($self->use_exif_preview){        
-            # an exif preview is available. we try to use it
-            if(defined $info->{PreviewImage} ){
-                printf("use preview\n");
-                $self->_write_preview_image( $info->{PreviewImage} );
-                my $image = new Image::Magick;
-                eval {
-                	$create_gui_preview = $image->ReadImage(
-                        $self->current_image->preview_file
-                    );
-                };
-                unlink $self->current_image->preview_file if $create_gui_preview;
+        if($is_new_image){
+    	    my $use_wx_resize = $self->_create_gui_preview($info);
+    	    $self->_create_gui_thumbnail($use_wx_resize);
+
+            # ok
+            if(!$@){
+                $self->progress_msg("Thumbnail and preview created for %s");
             }
             else {
-                $create_gui_preview = 1;
+                $self->progress_msg("An error has occured when processing %s\n$@");
+                # remove from list
+                splice @{$self->sums}, $i, 1;
+                $errors++;
             }
+        
+            $self->SetNewFilesProgressCallback->();
         }
-        else {
-            $create_gui_preview = 1;
-        }
-        print "create gui preview", $create_gui_preview, "\n";
-        # have to create a preview file
-        if($create_gui_preview) {
-            eval {
-                if(!$self->CreateGUIPreview()){
-                    # use method provided by the caller
-                    # source, target, type, ratio
-                    $self->ResizeCallback->(
-                        $self->current_image->file,
-                        $self->current_image->preview_file,
-                        $self->type,
-                        $self->preview_ratio,
-                        undef,
-                        undef,
-                        $self->quality,
-                    );
-                }
-            };# create a preview file
-        }    
-
-        $self->RotateImage(
-            $self->current_image->preview_file,
-        ) if $self->auto_rotate;
-
-        $self->_set_exif_tag(
-            $self->current_image->preview_file,
-            'Orientation',
-            'Horizontal (normal)',
-        ) if $self->auto_rotate;
-
-
-        # Now, we should have a valid preview image.
-        # try to thumbnail it
-        eval {
-            printf("create gui thumbnail\n");
-            # use the preview image to create a gui display thumbnail
-            $self->CreateGUIThumbnail();
-        };
-        # ok
-        if(!$@){
-            $self->progress_msg("Thumbnail and preview created for %s");
-            $i++;
-            $count++;
-            $self->count($count);
-        }
-        else {
-            $self->progress_msg("An error has occured when processing %s\n$@");
-            # remove from list
-            splice @{$self->images}, $i, 1;
-            $errors++;
-        }
-        
-        $self->SetNewFilesProgressCallback->();
+        $i++;
+        $count++;
+        $self->count($count);
         $self->SetNewFilesViewerRefreshCallback->();
-
     }
     @{$files};
@@ -297,35 +282,154 @@
 }
 
+sub _read_exif_metatdata {
+    my ( $self, $file ) = @_;
+    
+    # read exif metadata
+    my $info;
+    eval {
+        $info = ImageInfo( $file );
+    };
+    $info = {} if($@);
+    
+    $info;
+}
+
 # key is file path
 sub _add_image {
     my ( $self, $file, $info, $i ) = @_;	
 
-    my $image = Uploader::Image->new(
+    my $is_new_image;
+
+    # for legacy imagelist that do not have image_sums property
+    $self->image_sums(
+        {}
+    ) if !$self->image_sums;
+
+    my $sum = file_md5_hex($file);
+
+    my $image;
+    if ( !exists $self->image_sums->{$sum} ){
+        # append to image list
+        $image = Uploader::Image->new(
         {
-            file              => $_,
-            site_name         => sprintf(
-        	                     "%s %s", 
-        	                     $self->default_name_prefix, 
-        	                     $i,
-        	                 ),
-            site_author       => $self->author,
-            exif_metadata     => $self->_select_exif_data($info),
-            add_rank          => $i,
-            site_categories   => [],
-            site_tags         => [],
-            site_high_file    => $_,
+                file              => $_,
+                file_sum          => $sum,
+                site_name         => sprintf(
+        	                         "%s %s", 
+        	                         $self->default_name_prefix, 
+        	                         $i,
+        	                     ),
+                site_author       => $self->author,
+                exif_metadata     => $self->_select_exif_data($info),
+                add_rank          => $i,
+                site_categories   => [],
+                site_tags         => [],
+                site_high_file    => $_,
+            }
+        );
+
+        $self->image_sums->{$sum} = $image ;
+        $is_new_image = 1;
+    } else {
+        $image = $self->image_sums->{$sum};
+    }
+
+        
+    #$self->images->[$i] = $image ;
+    $self->sums->[$i] = $sum ;
+
+    $is_new_image;
+}
+
+sub _create_gui_preview {
+    my ( $self, $info ) = @_;
+
+    my $create_gui_preview;
+    my $use_wx_resize;
+    if($self->use_exif_preview){        
+        # an exif preview is available. we try to use it
+        if(defined $info->{PreviewImage} ){
+            printf("use preview\n");
+            $self->_write_preview_image( $info->{PreviewImage} );
+            my $image = new Image::Magick;
+            eval {
+                $create_gui_preview = $image->ReadImage(
+                    $self->current_image->preview_file
+                );
+            };
+            unlink $self->current_image->preview_file if $create_gui_preview;
         }
-    );
-
-    # append to image list
-    $self->images->[$i] = $image ;
-
-}
-
+        else {
+            $create_gui_preview = 1;
+        }
+    }
+    else {
+        $create_gui_preview = 1;
+    }
+    print "create gui preview", $create_gui_preview, "\n";
+    # have to create a preview file
+    if($create_gui_preview) {
+        eval {
+            if(!$self->CreateGUIPreview()){
+                # use method provided by the caller
+                # source, target, type, ratio
+                print "CreateGUIPreview failed, use callback\n";
+                $self->RescaleCallback->(
+                    $self->current_image->file,
+                    $self->current_image->preview_file,
+                    $self->type,
+                    $self->preview_ratio,
+                    undef,
+                    undef,
+                    $self->quality,
+                );
+                $use_wx_resize = 1;
+            }
+        };# create a preview file
+    }    
+
+    $self->RotateImage(
+        $self->current_image->preview_file,
+    ) if $self->auto_rotate;
+
+    $self->_set_exif_tag(
+        $self->current_image->preview_file,
+        'Orientation',
+        'Horizontal (normal)',
+    ) if $self->auto_rotate;
+    
+    $use_wx_resize;    	
+}
+
+sub _create_gui_thumbnail {
+    my ( $self, $use_wx_resize ) = @_;
+
+    # Now, we should have a valid preview image.
+    # try to thumbnail it
+     eval {
+        printf("create gui thumbnail\n");
+        # use the preview image to create a gui display thumbnail
+        if($use_wx_resize)
+        {
+                print "CreateGUIThumbnail failed, use callback\n";
+                $self->ResizeCallback->(
+                    $self->current_image->preview_file,
+                    $self->current_image->wx_thumb_file,
+                    $self->type,
+                    $self->wx_thumb_size,
+                    $self->wx_thumb_size,
+                    $self->wx_quality,
+                );
+        	
+        } else {
+            $self->CreateGUIThumbnail();            	
+        }
+     };
+}
 
 sub RemoveImageSelection {
     my ( $self ) = @_;
     
-    return if (! scalar @{$self->images} );
+    return if (! scalar @{$self->sums} );
     return if (! defined $self->image_selection );
     
@@ -333,6 +437,6 @@
     my @images = reverse @{$self->image_selection};     
     map {
-        my $image = $self->images->[$_]->file;
-        splice @{$self->images}, $_, 1 ;
+        my $image = $self->GetImage($_)->file;
+        splice @{$self->sums}, $_, 1 ;
         $self->wx_thumb_imglist->Remove($_);
         shift @images;
@@ -350,5 +454,5 @@
     return 1 if( -e $self->current_image->wx_thumb_file );
     my $rval = 0;
-print "CreateGUIThumbnail ", $self->current_image->wx_thumb_file, "\n";
+    print "CreateGUIThumbnail ", $self->current_image->wx_thumb_file, "\n";
     my $image = new Image::Magick;
 
@@ -401,5 +505,5 @@
             "%s:%s",
             $self->type,
-            $self->current_image->wx_thumb_file,
+            encode('iso-8859-1', $self->current_image->wx_thumb_file),
         )
     );
@@ -459,5 +563,5 @@
             "%s:%s",
             $self->type,
-            $self->current_image->preview_file,
+            encode('iso-8859-1', $self->current_image->preview_file),
         )
     );
@@ -537,5 +641,5 @@
             "%s:%s",
             $self->type,
-            $self->current_image->site_resized_file,
+            encode('iso-8859-1', $self->current_image->site_resized_file),
         )
     );
@@ -561,5 +665,5 @@
 
     my $status = $image->ReadImage(
-        $self->current_image->site_resized_file
+        encode('iso-8859-1', $self->current_image->site_resized_file)
     );
     warn "$status" if $status ;
@@ -589,5 +693,5 @@
             "%s:%s",
             $self->type,
-            $self->current_image->site_thumb_file,
+            encode('iso-8859-1', $self->current_image->site_thumb_file),
         )
     );
@@ -652,5 +756,4 @@
        	        wx_thumb_size
                 current_image
-                images
                 exif_metadata
                 wx_thumb_dir
@@ -666,4 +769,8 @@
             	create_resized
             	use_exif_preview
+            	image_sums
+            	sums
+                version
+                imagelist_version
             /
         ] 
@@ -702,5 +809,5 @@
         # current image object    	
     	$self->current_image(
-    	    $self->images->[$_]
+    	    $self->GetImage($_)
     	);
 
@@ -711,5 +818,6 @@
         my $site_name = $self->current_image->site_name;
     
-        my ( $filename, $ext ) = split /\./, $file ;
+#        my ( $filename, $ext ) = split /\./, $file ;
+        my $filename = $self->current_image->file_sum ;
 
         # lately defined to make sure we have the last global properties ( resize_w, resize_h )
@@ -733,5 +841,5 @@
         ); 
 
-    	eval {
+#    	eval {
             # set current image thumbnail
             $self->progress_thumbnail_refresh->();
@@ -741,10 +849,10 @@
       	    # update upload progress dialog
             $self->progressbar_refresh->(0.25);
-        };
+#        };
         # user cancelled : dialog box is destroyed
-        croak "Upload cancelled." if $@ ;
-
+        croak "Upload cancelled. ", $@ if $@ ;
+print "1\n";
         if( $self->create_resized ){
-            eval {
+#            eval {
                 if(!$self->CreateResized()){
                     printf("CreateResized failed %s. Use ResizeCallback\n", $@);
@@ -765,5 +873,5 @@
                     ) if $self->auto_rotate;
                 }
-            };
+#            };
             $self->_set_exif_tag(
                 $self->current_image->site_resized_file,
@@ -778,4 +886,5 @@
             );
         }
+print "2\n";
 
 
@@ -792,12 +901,13 @@
         );
 
-    	eval {
+#    	eval {
             $self->progress_msg_refresh->($msg);
-        };
-        croak "Upload cancelled." if $@ ;
-
-    	eval {
+#        };
+        croak "Upload cancelled. ", $@ if $@ ;
+print "3\n";
+
+#    	eval {
             $self->CreateThumbnail();
-        };
+#        };
 
         if($@){
@@ -815,13 +925,16 @@
             );
         }
-    	eval {
+#    	eval {
             $self->progress_msg_refresh->($msg);
        	    $self->progressbar_refresh->(0.50);
-        };
-        croak "Upload cancelled." if $@ ;
+#        };
+        croak "Upload cancelled. ", $@ if $@ ;
+print "4\n";
 
         # photo metadata
         $self->_prepare_upload_properties();    	
+print "5\n";
         my ( $status, $status_msg ) = $self->pwg->UploadImage();
+print "6\n";
 
         if ( $status ){
@@ -847,9 +960,10 @@
         );
     	# update upload progress dialog
-    	eval {
+#    	eval {
             $self->progress_msg_refresh->($msg);
        	    $self->progressbar_refresh->(1);
-        };
-        croak "Upload cancelled." if $@ ;
+#        };
+        croak "Upload cancelled. ", $@ if $@ ;
+print "7\n";
         
     }
@@ -907,5 +1021,5 @@
         
         $image->Write(
-            filename=>$self->current_image->site_high_file
+            encode('iso-8859-1', filename=>$self->current_image->site_high_file)
         );
         warn "$status ", $self->current_image->site_high_file, "\n" if $status ;
@@ -1013,5 +1127,5 @@
         # write resizd file
         $image->Write(
-            filename=>$file
+            encode('iso-8859-1', filename=>$file)
         );
         warn "$status ", $file, "\n" if $status ;
@@ -1024,3 +1138,11 @@
 }
 
+sub GetImage {
+    my ( $self, $indx ) = @_;
+    
+    my $sum = $self->sums->[$indx];
+
+    $self->image_sums->{$sum};
+}
+
 1;
