comparison MoinMoin/action/AttachFile.py @ 5901:b9450db6c129

Attachment handler: catch all Zip-related errors I had faced the problem with attached .ppt file: MoinMoin tried to check if the file is a Zip file, but zipfile module throwed BadZipfile("Bad magic number for central directory"). Since, according to http://docs.python.org/dev/library/zipfile.html zipfile doesn't have a common base exception we can try to handle, we can only enumerate all possible exceptions. Currently, they are zipfile.BadZipfile, zipfile.LargeZipFile and RuntimeError. With Python 3.2 there will be zipfile.BadZipFile. Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 08 Dec 2012 22:54:04 +0100
parents 78f1be3c8777
children 7e7e1cbb9d3f
comparison
equal deleted inserted replaced
5900:c98ec456e493 5901:b9450db6c129
392 elif (not is_package and mt.minor == 'zip' and 392 elif (not is_package and mt.minor == 'zip' and
393 may_read and may_write and may_delete): 393 may_read and may_write and may_delete):
394 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='unzip')) + 394 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='unzip')) +
395 fmt.text(label_unzip) + 395 fmt.text(label_unzip) +
396 fmt.url(0)) 396 fmt.url(0))
397 except RuntimeError: 397 except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile):
398 # We don't want to crash with a traceback here (an exception 398 # We don't want to crash with a traceback here (an exception
399 # here could be caused by an uploaded defective zip file - and 399 # here could be caused by an uploaded defective zip file - and
400 # if we crash here, the user does not get a UI to remove the 400 # if we crash here, the user does not get a UI to remove the
401 # defective zip file again). 401 # defective zip file again).
402 # RuntimeError is raised by zipfile stdlib module in case of 402 # RuntimeError is raised by zipfile stdlib module in case of
403 # problems (like inconsistent slash and backslash usage in the 403 # problems (like inconsistent slash and backslash usage in the
404 # archive). 404 # archive).
405 # BadZipfile/LargeZipFile are raised when there are some
406 # specific problems with the archive file.
405 logging.exception("An exception within zip file attachment handling occurred:") 407 logging.exception("An exception within zip file attachment handling occurred:")
406 408
407 html.append(fmt.listitem(1)) 409 html.append(fmt.listitem(1))
408 html.append("[%s]" % "&nbsp;| ".join(links)) 410 html.append("[%s]" % "&nbsp;| ".join(links))
409 html.append(" (%(fmtime)s, %(fsize)s KB) [[attachment:%(file)s]]" % parmdict) 411 html.append(" (%(fmtime)s, %(fsize)s KB) [[attachment:%(file)s]]" % parmdict)
987 msg = _("Attachment '%(filename)s' partially unzipped (did not overwrite: %(filelist)s).") % { 989 msg = _("Attachment '%(filename)s' partially unzipped (did not overwrite: %(filelist)s).") % {
988 'filename': filename, 990 'filename': filename,
989 'filelist': ', '.join(not_overwritten), } 991 'filelist': ', '.join(not_overwritten), }
990 else: 992 else:
991 msg = _("Attachment '%(filename)s' unzipped.") % {'filename': filename} 993 msg = _("Attachment '%(filename)s' unzipped.") % {'filename': filename}
992 except RuntimeError, err: 994 except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile), err:
993 # We don't want to crash with a traceback here (an exception 995 # We don't want to crash with a traceback here (an exception
994 # here could be caused by an uploaded defective zip file - and 996 # here could be caused by an uploaded defective zip file - and
995 # if we crash here, the user does not get a UI to remove the 997 # if we crash here, the user does not get a UI to remove the
996 # defective zip file again). 998 # defective zip file again).
997 # RuntimeError is raised by zipfile stdlib module in case of 999 # RuntimeError is raised by zipfile stdlib module in case of
998 # problems (like inconsistent slash and backslash usage in the 1000 # problems (like inconsistent slash and backslash usage in the
999 # archive). 1001 # archive).
1002 # BadZipfile/LargeZipFile are raised when there are some
1003 # specific problems with the archive file.
1000 logging.exception("An exception within zip file attachment handling occurred:") 1004 logging.exception("An exception within zip file attachment handling occurred:")
1001 msg = _("A severe error occurred:") + ' ' + str(err) 1005 msg = _("A severe error occurred:") + ' ' + str(err)
1002 1006
1003 upload_form(pagename, request, msg=msg) 1007 upload_form(pagename, request, msg=msg)
1004 1008
1066 for zinfo in zf.filelist: 1070 for zinfo in zf.filelist:
1067 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time 1071 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time
1068 request.write(wikiutil.escape("%-46s %s %12d\n" % (zinfo.filename, date, zinfo.file_size))) 1072 request.write(wikiutil.escape("%-46s %s %12d\n" % (zinfo.filename, date, zinfo.file_size)))
1069 request.write("</pre>") 1073 request.write("</pre>")
1070 return 1074 return
1071 except RuntimeError: 1075 except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile):
1072 # We don't want to crash with a traceback here (an exception 1076 # We don't want to crash with a traceback here (an exception
1073 # here could be caused by an uploaded defective zip file - and 1077 # here could be caused by an uploaded defective zip file - and
1074 # if we crash here, the user does not get a UI to remove the 1078 # if we crash here, the user does not get a UI to remove the
1075 # defective zip file again). 1079 # defective zip file again).
1076 # RuntimeError is raised by zipfile stdlib module in case of 1080 # RuntimeError is raised by zipfile stdlib module in case of
1077 # problems (like inconsistent slash and backslash usage in the 1081 # problems (like inconsistent slash and backslash usage in the
1078 # archive). 1082 # archive).
1083 # BadZipfile/LargeZipFile are raised when there are some
1084 # specific problems with the archive file.
1079 logging.exception("An exception within zip file attachment handling occurred:") 1085 logging.exception("An exception within zip file attachment handling occurred:")
1080 return 1086 return
1081 1087
1082 from MoinMoin import macro 1088 from MoinMoin import macro
1083 from MoinMoin.parser.text import Parser 1089 from MoinMoin.parser.text import Parser