--- AttachFile_from_cvs.py Thu Feb 13 18:27:50 2003 +++ AttachFile.py Thu Feb 13 18:38:48 2003 @@ -24,7 +24,7 @@ $Id: AttachFile.py,v 1.48 2003/02/12 23:33:40 jhermann Exp $ """ -import cgi, os, mimetypes, string, sys, time, urllib +import cgi, os, mimetypes, string, sys, time, urllib, re from MoinMoin import config, user, util, wikiutil, webapi from MoinMoin.Page import Page from MoinMoin.util import filesys @@ -134,6 +134,25 @@ error_msg(pagename, request, error) return (None, None) +def _backup_file(fpath): + """Move the file `fpath` to `fpath,nnn` where `nnn` is one plus `mmm`, where + `mmm` is the highest number for which `fpath.mmm` exists, or 000 if no + such file exists.""" + + # find the right sequence number + dirname, basename = os.path.split(fpath) + backup_re = re.compile(r'^%s\,(?P\d\d\d)$' % basename) + backups = filter(backup_re.match, os.listdir(dirname)) + backups.sort() + if backups == []: + seq_no = 0 + else: + highest = backups[-1] + seq_no = int(backup_re.match(highest).group('seq')) + 1 + + # do the move + new_name = "%s,%03d" % (fpath, seq_no) + os.rename(fpath, new_name) def _get_filelist(request, pagename): _ = request.getText @@ -145,6 +164,10 @@ files = os.listdir(attach_dir) files.sort() + # filter out backup files + backup_re = re.compile(r'.*\,\d\d\d$') + files = filter(lambda x: not backup_re.match(x), files) + str = "" if files: str = str + _("

" @@ -260,9 +283,9 @@ print _("""

New Attachment

-

An upload will never overwrite an existing file. If there is a name -conflict, you have to rename the file that you want to upload. -Otherwise, if "Rename to" is left blank, the original filename will be used.

+

When uploading under a filename for which an attachment already exists, +the old version is stored - ask the administrator to recover it.

+

If "Rename to" is left blank, the original filename will be used.

""") print """
@@ -383,20 +406,20 @@ # save file fpath = os.path.join(attach_dir, target) if os.path.exists(fpath): - msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % locals() - else: - content = fileitem.file.read() - stream = open(fpath, 'wb') - try: - stream.write(content) - finally: - stream.close() - os.chmod(fpath, 0666 & config.umask) - - bytes = len(content) - msg = _("Attachment '%(target)s' (remote name '%(filename)s')" - " with %(bytes)d bytes saved.") % locals() - _addLogEntry(request, 'ATTNEW', pagename, target) + _backup_file(fpath) + + content = fileitem.file.read() + stream = open(fpath, 'wb') + try: + stream.write(content) + finally: + stream.close() + os.chmod(fpath, 0666 & config.umask) + + bytes = len(content) + msg = _("Attachment '%(target)s' (remote name '%(filename)s')" + " with %(bytes)d bytes saved.") % locals() + _addLogEntry(request, 'ATTNEW', pagename, target) # return attachment list upload_form(pagename, request, msg) @@ -433,6 +456,8 @@ _addLogEntry(request, 'ATTDRW', pagename, basename + ext) savepath = os.path.join(getAttachDir(pagename), basename + ext) + if os.path.exists(savepath): + _backup_file(savepath) file = open(savepath, 'wb') try: file.write(content) @@ -448,7 +473,9 @@ if not filename: return # error msg already sent in _access_file # delete file - os.remove(fpath) + if os.path.exists(fpath): + _backup_file(fpath) + _addLogEntry(request, 'ATTDEL', pagename, filename) upload_form(pagename, request, msg=_("Attachment '%(filename)s' deleted.") % locals())