diff MoinMoin/action/AttachFile.py @ 802:7b2e550c9660

merge moin/1.6
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 25 May 2006 13:33:19 +0200
parents 1f8976e01c3a f18b06c790d4
children 0152984f76ee
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Fri May 19 20:18:45 2006 +0200
+++ b/MoinMoin/action/AttachFile.py	Thu May 25 13:33:19 2006 +0200
@@ -41,6 +41,9 @@
 ### External interface - these are called from the core code
 #############################################################################
 
+class AttachmentAlreadyExists(Exception):
+    pass
+
 def getBasePath(request):
     """ Get base path where page dirs for attachments are stored.
     """
@@ -155,6 +158,42 @@
         }
     return "\n<p>\n%s\n</p>\n" % attach_info
 
+def add_attachment(request, pagename, target, filecontent):
+    # replace illegal chars
+    target = wikiutil.taintfilename(target)
+
+    # set mimetype from extension, or from given mimetype
+    #type, encoding = wikiutil.guess_type(target)
+    #if not type:
+    #    ext = None
+    #    if request.form.has_key('mime'):
+    #        ext = wikiutil.guess_extension(request.form['mime'][0])
+    #    if not ext:
+    #        type, encoding = wikiutil.guess_type(filename)
+    #        if type:
+    #            ext = wikiutil.guess_extension(type)
+    #        else:
+    #            ext = ''
+    #    target = target + ext
+
+    # get directory, and possibly create it
+    attach_dir = getAttachDir(request, pagename, create=1)
+    # save file
+    fpath = os.path.join(attach_dir, target).encode(config.charset)
+    if os.path.exists(fpath):
+        raise AttachmentAlreadyExists
+    else:
+        stream = open(fpath, 'wb')
+        try:
+            stream.write(filecontent)
+        finally:
+            stream.close()
+        os.chmod(fpath, 0666 & config.umask)
+
+        _addLogEntry(request, 'ATTNEW', pagename, target)
+        
+        return target
+
 
 #############################################################################
 ### Internal helpers
@@ -536,49 +575,23 @@
     filecontent = request.form['file'][0]
 
     # preprocess the filename
-    # 1. strip leading drive and path (IE misbehaviour)
+    # strip leading drive and path (IE misbehaviour)
     if len(target) > 1 and (target[1] == ':' or target[0] == '\\'): # C:.... or \path... or \\server\...
         bsindex = target.rfind('\\')
         if bsindex >= 0:
             target = target[bsindex+1:]
-        
-    # 2. replace illegal chars
-    target = wikiutil.taintfilename(target)
-
-    # set mimetype from extension, or from given mimetype
-    #type, encoding = wikiutil.guess_type(target)
-    #if not type:
-    #    ext = None
-    #    if request.form.has_key('mime'):
-    #        ext = wikiutil.guess_extension(request.form['mime'][0])
-    #    if not ext:
-    #        type, encoding = wikiutil.guess_type(filename)
-    #        if type:
-    #            ext = wikiutil.guess_extension(type)
-    #        else:
-    #            ext = ''
-    #    target = target + ext
-
-    # get directory, and possibly create it
-    attach_dir = getAttachDir(request, pagename, create=1)
-    # save file
-    fpath = os.path.join(attach_dir, target).encode(config.charset)
-    if os.path.exists(fpath):
-        msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % {
-            'target': target, 'filename': filename}
-    else:
-        stream = open(fpath, 'wb')
-        try:
-            stream.write(filecontent)
-        finally:
-            stream.close()
-        os.chmod(fpath, 0666 & config.umask)
+    
+    # add the attachment
+    try:
+        add_attachment(request, pagename, target, filecontent)
 
         bytes = len(filecontent)
         msg = _("Attachment '%(target)s' (remote name '%(filename)s')"
                 " with %(bytes)d bytes saved.") % {
                 'target': target, 'filename': filename, 'bytes': bytes}
-        _addLogEntry(request, 'ATTNEW', pagename, target)
+    except AttachmentAlreadyExists:
+        msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % {
+            'target': target, 'filename': filename}
 
     # return attachment list
     upload_form(pagename, request, msg)