diff MoinMoin/action/AttachFile.py @ 5279:2aa43685e17b

AttachFile: added remove_attachment(), made nuke_page use it (details below) AttachFile was missing a remove_attachment function (in the style of add_attachment), added it. It triggers an event FileRemovedEvent. FileRemovedEvent handling: * implemented for mail notifications * implemented for xapian index updates * not implemented for jabber notifications (this also needs some work in the jabber bot + testing) nuke_page was broken because it did not remove attachments from xapian index, fixed it by using remove_attachment (which triggers indexing via FileRemovedEvent).
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 15 Nov 2009 10:08:13 +0100
parents 195db0fdbb80
children ee5c26854fbc
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Sat Nov 14 13:57:18 2009 +0100
+++ b/MoinMoin/action/AttachFile.py	Sun Nov 15 10:08:13 2009 +0100
@@ -43,7 +43,7 @@
 from MoinMoin.Page import Page
 from MoinMoin.util import filesys, timefuncs
 from MoinMoin.security.textcha import TextCha
-from MoinMoin.events import FileAttachedEvent, send_event
+from MoinMoin.events import FileAttachedEvent, FileRemovedEvent, send_event
 from MoinMoin.support import tarfile
 
 action_name = __name__.split('.')[-1]
@@ -198,34 +198,56 @@
         filecontent can be either a str (in memory file content),
         or an open file object (file content in e.g. a tempfile).
     """
-    _ = request.getText
-
     # replace illegal chars
     target = wikiutil.taintfilename(target)
 
     # 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)
+
     exists = os.path.exists(fpath)
-    if exists and not overwrite:
-        raise AttachmentAlreadyExists
+    if exists:
+        if overwrite:
+            remove_attachment(request, pagename, target)
+        else:
+            raise AttachmentAlreadyExists
+
+    # save file
+    stream = open(fpath, 'wb')
+    try:
+        _write_stream(filecontent, stream)
+    finally:
+        stream.close()
+
+    _addLogEntry(request, 'ATTNEW', pagename, target)
+
+    filesize = os.path.getsize(fpath)
+    event = FileAttachedEvent(request, pagename, target, filesize)
+    send_event(event)
+
+    return target, filesize
+
+
+def remove_attachment(request, pagename, target):
+    """ remove attachment <target> of page <pagename>
+    """
+    # replace illegal chars
+    target = wikiutil.taintfilename(target)
+
+    # get directory, do not create it
+    attach_dir = getAttachDir(request, pagename, create=0)
+    # remove file
+    fpath = os.path.join(attach_dir, target).encode(config.charset)
+    try:
+        filesize = os.path.getsize(fpath)
+        os.remove(fpath)
+    except:
+        # either it is gone already or we have no rights - not much we can do about it
+        filesize = 0
     else:
-        if exists:
-            try:
-                os.remove(fpath)
-            except:
-                pass
-        stream = open(fpath, 'wb')
-        try:
-            _write_stream(filecontent, stream)
-        finally:
-            stream.close()
+        _addLogEntry(request, 'ATTDEL', pagename, target)
 
-        _addLogEntry(request, 'ATTNEW', pagename, target)
-
-        filesize = os.path.getsize(fpath)
-        event = FileAttachedEvent(request, pagename, target, filesize)
+        event = FileRemovedEvent(request, pagename, target, filesize)
         send_event(event)
 
     return target, filesize
@@ -619,15 +641,7 @@
     if not filename:
         return # error msg already sent in _access_file
 
-    # delete file
-    os.remove(fpath)
-    _addLogEntry(request, 'ATTDEL', pagename, filename)
-
-    if request.cfg.xapian_search:
-        from MoinMoin.search.Xapian import XapianIndex
-        index = XapianIndex(request)
-        if index.exists:
-            index.update_item(pagename, filename)
+    remove_attachment(request, pagename, filename)
 
     upload_form(pagename, request, msg=_("Attachment '%(filename)s' deleted.") % {'filename': filename})