--- C:\download\pmswiki\gallery2\Gallery2-1.3.5-15.py	2006-07-24 11:14:42.336758000 +0200
+++ Gallery2.py	2006-07-27 20:19:22.463233600 +0200
@@ -315,7 +315,7 @@
        2005-11-12: Version 1.3.5-12 bug fixed for image_for_webnail=1            
                                     bug fixed at last cell table end tr instead of td
                                     bug fixed don't render a filename as WikiName
-                                    bug fixed " is allowed in alias name
+                                    bug fixed \" is allowed in alias name
                                     bug fixed ' is allowed in alias name
                                     bug fixed linebreak by a space in alias  
                                     not quite a bug but makes it very difficult to code in 
@@ -342,7 +342,8 @@
 from MoinMoin import wikiutil, config
 from MoinMoin.Page import Page
 from MoinMoin.parser import wiki
-
+from xml.sax.saxutils import quoteattr
+import sha
 
 def images2swf(request,images,swf_filename,swf_fps,description,exif_date):
 ## this code is based on 
@@ -401,6 +402,26 @@
     
   #  os.system(cmd)    
 
+def removeControlChar(text):
+    ret = ""
+    for c in text:
+        if ord(c) < 32:
+            continue
+        ret += c
+    return ret
+
+def saveImageProgressiveIfPossible(im, filename, img_type):
+    progressive = 0
+    if img_type == "JPEG":
+        progressive = 1
+    try:
+        im.save(filename, img_type, progressive=progressive)
+    except IOError, e:
+        if e.args[0] == 'encoder error -2 when writing image file':
+            im.save(filename, img_type)
+        else:
+            raise
+
 def server(request):
     if request.is_ssl:
         url_pattern = 'https'
@@ -412,19 +433,20 @@
                                "server_name":request.server_name, 
                                "server_port":str(request.server_port)}
 
-def show_tools_restricted(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):
+def show_tools_restricted(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request, pull_from_dir):
     if request.user.may.delete(pagename):
-        return tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request)
+        return tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request, pull_from_dir)
     else:
         return ''    
 
-def tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):
+def tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request, pull_from_dir):
     text = '''
                    <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                         <td>
                             <input type="hidden" name="action" value="gallery2image">
                             <input type="hidden" name="do" value="RL">
                             <input type="hidden" name="target" value="%(this_target)s">
+                            <input type="hidden" name="pull_from_dir" value="%(pull_from_dir)s">
                             <input type="image" value="submit" src="%(server)s%(wiki)s/modern/img/to_left.png" title="rotate to left">
                         </td>
                     </form>
@@ -433,6 +455,7 @@
                             <input type="hidden" name="action" value="gallery2image">
                             <input type="hidden" name="do" value="RR">
                             <input type="hidden" name="target" value="%(this_target)s">
+                            <input type="hidden" name="pull_from_dir" value="%(pull_from_dir)s">
                             <input type="image"  value="submit" src="%(server)s%(wiki)s/modern/img/to_right.png" title="rotate to right" >
                         </td>
                     </form>
@@ -441,6 +464,7 @@
                             <input type="hidden" name="action" value="gallery2image">
                             <input type="hidden" name="do" value="RM">
                             <input type="hidden" name="target" value="%(this_target)s">
+                            <input type="hidden" name="pull_from_dir" value="%(pull_from_dir)s">
                             <input type="image" value="submit" src="%(server)s%(wiki)s/modern/img/to_bak.png" title="move to bak" >
                         </td>
                     </form>''' %  {
@@ -448,13 +472,14 @@
             "wiki": request.cfg.url_prefix,  
             'baseurl': request.getScriptname(),
             "pagename": pagename,
-            "this_target": this_target}                   
+            "this_target": this_target,
+            "pull_from_dir":pull_from_dir,
+            }                   
     return text                
     
-def tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request):
-    text = '''
-            <TABLE align="center" width="%(thumbnail_width)s">
-                <TR>
+def tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request,pull_from_dir):
+    if not pull_from_dir:
+        actionAttachFile = '''
                     <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                         <td>
                             <input type="hidden" name="action" value="AttachFile">
@@ -463,12 +488,19 @@
                             <input type="image" value="submit" src="%(server)s%(wiki)s/modern/img/to_full.png" title="load image">
                         </td>
                     </form>
+'''
+    else:
+        actionAttachFile = ''
+    text = ('''
+            <TABLE align="center" width="%(thumbnail_width)s">
+                <TR>
+                    ''' + actionAttachFile + '''
                     <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                         <td>
                             <input type="hidden" name="action" value="gallery2image">
                             <input type="hidden" name="do" value="VS">
-                            <input type="hidden" name="full" value='%(full)s'>
-                            <input type="hidden" name="alias" value='%(alias)s'>
+                            <input type="hidden" name="full" value=%(full)s>
+                            <input type="hidden" name="alias" value=%(alias)s>
                             <input type="hidden" name="target" value='%(target)s'>
                             <input type="hidden" name="exif_date" value='%(exif_date)s'>
                             <input type="image" value="submit" src="%(server)s%(wiki)s/modern/img/to_slide.png" title="slide_show" >
@@ -476,18 +508,18 @@
                     </form>
                     %(show_tools_restricted)s
                 </TR>
-            </TABLE>'''   %  {
+            </TABLE>''')   %  {
             "server": server(request),
             "wiki": request.cfg.url_prefix,  
             'baseurl': request.getScriptname(),
             "pagename": pagename,
             "thumbnail_width": thumbnail_width,
-            "full": full,
-            "alias": alias,
+            "full": quoteattr(full),
+            "alias": quoteattr(alias),     # need to use quoteattr if text include character ' or "
             "exif_date": exif_date,
             "target": target,
             "this_target": this_image,
-            "show_tools_restricted": show_tools_restricted(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request)
+            "show_tools_restricted": show_tools_restricted(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request,pull_from_dir),
             } 
    
     return text
@@ -513,21 +545,21 @@
    else:
         return ''
                
-def show_tools_mode2(show_tools,pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):  
+def show_tools_mode2(show_tools,pagename,this_target,thumbnail_width,full,alias,target,exif_date,request,pull_from_dir):  
     if show_tools == '1' :
-        return "<td align=""center""> %s </td>" % tools_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request)
+        return "<td align=""center""> %s </td>" % tools_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request,pull_from_dir)
     else:
         return ''
 
-def mode2_html(pagename,border_thick,width,thumbnail_width,text_width,full,this_image,alias,this_alias,exif_date,this_exif_date,target,this_target,submit,show_tools,show_date,show_alias,request):
+def mode2_html(pagename,border_thick,width,thumbnail_width,text_width,full,this_image,alias,this_alias,exif_date,this_exif_date,target,this_target,submit,show_tools,show_date,show_alias,request,pull_from_dir):
     text = '''
     <tr valign="center">
         <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
             <td align="center" valign="center" width="%(thumbnail_width)s">
                 <input type="hidden" name="action" value="gallery2image">
                 <input type="hidden" name="do" value="VS">
-                <input type="hidden" name="full" value='%(full)s'>
-                <input type="hidden" name="alias" value='%(alias)s'>
+                <input type="hidden" name="full" value=%(full)s>
+                <input type="hidden" name="alias" value=%(alias)s>
                 <input type="hidden" name="exif_date" value='%(exif_date)s'>
                 <input type="hidden" name="target" value='%(target)s'>
                 <input type="image" value="submit" src="%(server)s%(submit)s">
@@ -540,21 +572,21 @@
      "baseurl": request.getScriptname(),
      "pagename": pagename,
      "thumbnail_width": thumbnail_width,
-     "full": full,
-     "alias": alias,
+     "full": quoteattr(full),
+     "alias": quoteattr(alias),
      "exif_date": exif_date,
      "target": target,
      "submit": submit,
-     "tools_html": show_tools_mode2(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request),
+     "tools_html": show_tools_mode2(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request,pull_from_dir),
      "date_html": show_date_mode2(show_date,this_exif_date),
      "alias_html": show_alias_mode2(show_alias,thumbnail_width,this_alias,text_width)
      }
     
     return text
 
-def show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request):  
-    if show_tools == '1' :
-        text="<tr><td align=""center"">%s </td></tr>" % tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request)
+def show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request,pull_from_dir):  
+    if show_tools == '1':
+        text="<tr><td align=""center"">%s </td></tr>" % tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request, pull_from_dir)
     else:
         text = ''
     return text        
@@ -581,7 +613,7 @@
        return ''     
        
 def mode1_html(pagename,border_thick,width,thumbnail_width,text_width,full,this_image,alias,this_alias,exif_date,this_exif_date,target,this_target,submit,
-               show_tools,show_date,show_alias,request):
+               show_tools,show_date,show_alias,request,pull_from_dir):
    text = '''
 <table width="%(thumbnail_width)s" align="center" valign="center">
     <TR align="center" valign="center">
@@ -589,10 +621,11 @@
             <td align="center" valign="middle" width="%(thumbnail_width)s">
                 <input type="hidden" name="action" value="gallery2image">
                 <input type="hidden" name="do" value="VS">
-                <input type="hidden" name="full" value='%(full)s'>
-                <input type="hidden" name="alias" value='%(alias)s'>
+                <input type="hidden" name="full" value=%(full)s>
+                <input type="hidden" name="alias" value=%(alias)s>
                 <input type="hidden" name="exif_date" value='%(exif_date)s'>
                 <input type="hidden" name="target" value='%(target)s'>
+                <input type="hidden" name="pull_from_dir" value='%(pull_from_dir)s'>
                 <input type="image" value="submit" src="%(server)s%(submit)s" >
             </td>
         </form>
@@ -604,20 +637,21 @@
      "server" : server(request), 
      "baseurl": request.getScriptname() ,
      "pagename":pagename,
-     "full":full,
-     "alias":alias,
+     "full":quoteattr(full),
+     "alias":quoteattr(alias),
      "exif_date":exif_date,
      "target":target,
+     "pull_from_dir":pull_from_dir,
      "submit":submit,
      "thumbnail_width":thumbnail_width,
-     "tools_html": show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request),  
+     "tools_html": show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request,pull_from_dir),  
      "date_html":show_date_mode1(show_date,this_exif_date),
      "alias_html": show_alias_mode1(show_alias,thumbnail_width,this_alias,text_width)
      }
 
    return text
 
-def get_files(kw,path,files,quotes,request):
+def get_files(kw,path,files,quotes,request,tmp_prefix):
     web = []
     full = []
     thumb = []
@@ -636,22 +670,21 @@
     source_type = ''
     for attfile in files:
         # only files not thumb or webnails
-        if attfile.find('thumbnail_') == -1 and attfile.find('webnail_') == -1:
+        if not os.path.split(attfile)[1].startswith('autogenerated-'):
             # only images
             if wikiutil.isPicture(attfile):
-                description.append(ddict.get(attfile, attfile))
                 full.append(attfile)
                 
                 fname, ext = os.path.splitext(attfile)
                 if ext in ('.gif', '.png'):
                     img_type.append('PNG')
-                    webnail = 'webnail_%s.png' % fname
-                    thumbfile = 'thumbnail_%s.png' % fname
+                    webnail = tmp_prefix + 'webnail_%s.png' % fname
+                    thumbfile = tmp_prefix + 'thumbnail_%s.png' % fname
                     source_type = ext[1:]
                 else:
                     img_type.append("JPEG")
-                    webnail = 'webnail_%s.jpg' % fname
-                    thumbfile = 'thumbnail_%s.jpg' % fname
+                    webnail = tmp_prefix + 'webnail_%s.jpg' % fname
+                    thumbfile = tmp_prefix + 'thumbnail_%s.jpg' % fname
                     source_type = 'jpg'    
                 
                     
@@ -668,6 +701,11 @@
                 else:
                     date = '--'
                 exif_date.append(date)
+                #
+                desc = ddict.get(attfile, attfile)
+                if tags.has_key('EXIF UserComment'):
+                    desc += " " + removeControlChar(str(tags['EXIF UserComment']))
+                description.append(desc)
                 f.close() 
           
     return thumb,web,full,video_type, exif_date,img_type,source_type,description  
@@ -747,6 +785,7 @@
             'thumbnail_width': '128',
             'webnail_width': '640',
             'text_width': '140',
+            'pull_from_dir': '',
             'sequence_name' : '',
             'sequence_fps'  : '1',
             'eo_info' : '0',
@@ -761,6 +800,7 @@
 
 
         self.kw['width']=str((int(self.kw['thumbnail_width'])+int(self.kw['text_width'])))
+        self.tmp_prefix = "autogenerated-" + sha.new(os.path.abspath(self.kw['pull_from_dir'])).hexdigest() + "-" # prefix for generated files
 
 
     def format(self, formatter):
@@ -768,7 +808,11 @@
         Dict = {}
         quotes=get_quotes(self,formatter)
         current_pagename=formatter.page.page_name
-        attachment_path = AttachFile.getAttachDir(self.request, current_pagename, create=1)
+        if kw['pull_from_dir']:
+            attachment_path = kw['pull_from_dir']
+        else:
+            attachment_path = AttachFile.getAttachDir(self.request,current_pagename,create=1)
+        out_path = AttachFile.getAttachDir(self.request,current_pagename,create=1)
  
         if kw['help'] == '1':
             self.request.write('''
@@ -818,7 +862,13 @@
                 Dict.clear()
            
         else:
-            all_files=os.listdir(attachment_path)
+            try:
+                all_files=os.listdir(attachment_path)
+            except WindowsError, e:
+                if e.errno == 3:
+                    all_files = []
+                else:
+                    raise
 
         result = []
 
@@ -828,7 +878,7 @@
         all_files=result
         
         if not all_files:
-            self.request.write("<br><br><h1>No matching image file found!</h1>")
+            self.request.write("<br><br><h1>No matching image file found in '%s'!</h1>" % attachment_path)
             return
 
         if kw['sort_by_name'] == '1' and kw['only_items'] == '0': 
@@ -851,7 +901,7 @@
         if kw['reverse_sort'] == '1': 
              all_files.reverse()   
 
-        thumb, web, full, video_type, exif_date, imgtype, source_type, description = get_files(kw, attachment_path, all_files, quotes, self.request)
+        thumb, web, full, video_type, exif_date, imgtype, source_type, description = get_files(kw, attachment_path, all_files, quotes, self.request, self.tmp_prefix)
         
         if kw['template_itemlist'] == '1':
             self.request.write('Copy the following listitems into the script. Replace alias with the label you want. Afterwards disable template_itemlist by setting it to 0:<BR>')
@@ -891,8 +941,8 @@
             this_webnail = web[i]
             this_imgtype = imgtype[i]
             this_thumbfile = thumb[i]
-            thumbf = os.path.join(attachment_path,this_thumbfile)
-            webf = os.path.join(attachment_path,this_webnail)            
+            thumbf = os.path.join(out_path,this_thumbfile)
+            webf = os.path.join(out_path,this_webnail)            
             
             if kw['renew'] == '1':
                 if os.path.exists(thumbf):
@@ -909,11 +959,11 @@
                     if kw['image_for_webnail'] == '1' :
                        os.link(os.path.join(attachment_path,attfile),webf)
                     else:
-                       im.save(webf, this_imgtype)
+                       saveImageProgressiveIfPossible(im, webf, this_imgtype)
                 if not os.path.exists(thumbf):
                     im.thumbnail(((int(kw['thumbnail_width'])),((int(kw['thumbnail_width'])))),
                                    Image.ANTIALIAS)
-                    im.save(thumbf, this_imgtype)       
+                    saveImageProgressiveIfPossible(im, thumbf, this_imgtype)
                     
             if kw['image_for_webnail'] == '1' :     
                  this_webnailimg = attfile
@@ -940,7 +990,8 @@
                         kw['show_tools'],
                         kw['show_date'],
                         kw['show_text'],
-                        self.request
+                        self.request,
+                        kw['pull_from_dir'],
                         )
                 self.request.write(''.join(text))    
                         
@@ -962,7 +1013,8 @@
                         kw['show_tools'],
                         kw['show_date'],
                         kw['show_text'],
-                        self.request
+                        self.request,
+                        kw['pull_from_dir'],
                         )
             
                 if cols > 1 : self.request.write('<table valign="bottom">')
@@ -1057,4 +1109,4 @@
                   
                 
         
-       
\ No newline at end of file
+       
--- C:\download\pmswiki\gallery2\gallery2image-1.5.1-12.py	2006-07-27 14:36:48.287780800 +0200
+++ gallery2image.py	2006-07-27 14:36:48.297795200 +0200
@@ -72,7 +72,10 @@
 from MoinMoin.action import AttachFile
 from MoinMoin.formatter.text_html import Formatter
 from MoinMoin.parser import wiki
-
+import pmsfile
+import shutil
+import subprocess
+import sha
 
 def option_list(this_image,pagename,text,request):
     txt = ''
@@ -401,6 +404,35 @@
 
 action_name = __name__.split('.')[-1]
 
+
+def quoteCommandArgument(a):
+    if '"' in a:
+        a = a.replace('"', '\\"')
+    if '"' in a or ' ' in a:
+        a = '"' + a + '"'       
+    return a
+    
+def updateExifInformation(img_type, source, destination):
+    if img_type != "JPEG":
+        return True
+    cmd = 'jhead -te %s -rgt 160 -norot %s' % (quoteCommandArgument(source), quoteCommandArgument(destination))
+    exitcode = subprocess.Popen(cmd).wait()
+    if exitcode == 0:
+        return True
+    return False
+
+def saveImageProgressiveIfPossible(im, filename, img_type):
+    progressive = 0
+    if img_type == "JPEG":
+        progressive = 1
+    try:
+        im.save(filename, img_type, progressive=progressive)
+    except IOError, e:
+        if e.args[0] == 'encoder error -2 when writing image file':
+            im.save(filename, img_type)
+        else:
+            raise
+
 def execute(pagename, request):
     """ Main dispatcher for the 'Gallery' action.
     """
@@ -413,7 +445,7 @@
         images = string.split(request.form['target'][0],',')
         target = images[0]
         images = (images[1:])
-        
+
         full_image = string.split(request.form['full'][0],',')
         full_target = full_image[0]
         full_image = (full_image[1:])
@@ -482,19 +514,24 @@
 
         target=request.form['target'][0]
         file, ext = os.path.splitext(target)
+        pull_from_dir = request.form['pull_from_dir'][0]
+        tmp_prefix = "autogenerated-" + sha.new(os.path.abspath(pull_from_dir)).hexdigest() + "-" # prefix for generated files
 
         if ext == '.gif'  or  ext == '.png' :
             img_type = 'PNG'
-            thumbfile = "thumbnail_%(file)s.png" % {"file" : file}
-            webnail = "webnail_%(file)s.png" % {"file" : file}
+            thumbfile = tmp_prefix + "thumbnail_%(file)s.png" % {"file" : file}
+            webnail = tmp_prefix + "webnail_%(file)s.png" % {"file" : file}
         else:
             img_type="JPEG"
-            thumbfile="thumbnail_%(file)s.jpg"  % {"file" : file}
-            webnail="webnail_%(file)s.jpg"  % {"file" : file}
+            thumbfile= tmp_prefix + "thumbnail_%(file)s.jpg"  % {"file" : file}
+            webnail= tmp_prefix + "webnail_%(file)s.jpg"  % {"file" : file}
  
         thumb = os.path.join(attachment_path,thumbfile)
         webf = os.path.join(attachment_path,webnail)
-        infile = os.path.join(attachment_path,target)
+        if pull_from_dir:
+            infile = os.path.join(pull_from_dir,target)
+        else:
+            infile = os.path.join(attachment_path,target)
 
         msg = None
         if version.release < '1.5.0' :
@@ -504,8 +541,9 @@
         elif request.form['do'][0] == 'RM' :
             if os.path.exists(infile + '.bak') :
                os.unlink("%(file)s.bak" % {"file" : infile})
-            os.link(infile,"%(file)s.bak" % {"file" : infile})
-            os.unlink(infile)
+            #os.link(infile,"%(file)s.bak" % {"file" : infile})
+            #os.unlink(infile)
+            os.rename(infile,"%(file)s.bak" % {"file" : infile})
             os.unlink(webf)
             os.unlink(thumb)
             msg = _('%(target)s deleted, backup in place' % {
@@ -514,31 +552,36 @@
         elif request.form['do'][0] == 'RL' :
             im = Image.open(infile)
             #os.unlink(infile)
-            im.rotate(90).save(infile,img_type)
+            tmpFilename = pmsfile.tempnam(extension=".jpeg")
+            im.rotate(90).save(tmpFilename,img_type)
+            if updateExifInformation(img_type, infile, tmpFilename):
+                shutil.copyfile(tmpFilename, infile)
             nim = Image.open(infile)
             nim.thumbnail((640,640), Image.ANTIALIAS)
             #os.unlink(webf)
-            nim.save(webf, img_type)
+            saveImageProgressiveIfPossible(nim, webf, img_type)
             nim.thumbnail((128, 128), Image.ANTIALIAS)
             #os.unlink(thumb)
-            nim.save(thumb, img_type)
+            saveImageProgressiveIfPossible(nim, thumb, img_type)
             msg= _('%(target)s rotated to left 90 degrees' % {
-                  'target':target})
+                'target':target})
 
         elif request.form['do'][0] == 'RR' :
             im = Image.open(infile)
             #os.unlink(infile)
-            im.rotate(270).save(infile,img_type)
-            
+            tmpFilename = pmsfile.tempnam(extension=".jpeg")
+            im.rotate(270).save(tmpFilename,img_type)
+            if updateExifInformation(img_type, infile, tmpFilename):
+                shutil.copyfile(tmpFilename, infile)
             nim = Image.open(infile)
             nim.thumbnail((640,640), Image.ANTIALIAS)
             #os.unlink(webf)
             saveImageProgressiveIfPossible(nim, webf, img_type)
             nim.thumbnail((128, 128), Image.ANTIALIAS)
             #os.unlink(thumb)
-            nim.save(thumb, img_type)
+            saveImageProgressiveIfPossible(nim, thumb, img_type)
             msg= _('%(target)s rotated to right 90 degrees' % {
-                  'target':target})
+                'target':target})
              
         else:
             msg = _('action not implemented: %s') % (request.form['do'][0],)
