comparison MoinMoin/action/AttachFile.py @ 117:4775d5ff5f40

attachment unzipper: handle file count quota-like as it was done with size imported from: moin--main--1.5--patch-119
author Thomas Waldmann <tw@waldmann-edv.de>
date Mon, 17 Oct 2005 23:49:48 +0000
parents de898247fbff
children 139cb901b81c
comparison
equal deleted inserted replaced
116:de898247fbff 117:4775d5ff5f40
641 return # error msg already sent in _access_file 641 return # error msg already sent in _access_file
642 642
643 attachment_path = getAttachDir(request, pagename) 643 attachment_path = getAttachDir(request, pagename)
644 single_file_size = request.cfg.unzip_single_file_size 644 single_file_size = request.cfg.unzip_single_file_size
645 attachments_file_space = request.cfg.unzip_attachments_space 645 attachments_file_space = request.cfg.unzip_attachments_space
646 max_file_count = request.cfg.unzip_file_count 646 attachments_file_count = request.cfg.unzip_attachments_count
647 647
648 files = _get_files(request, pagename) 648 files = _get_files(request, pagename)
649 649
650 msg = "" 650 msg = ""
651 if files: 651 if files:
652 fsize = 0.0 652 fsize = 0.0
653 fcount = 0
653 for file in files: 654 for file in files:
654 fsize += float(os.stat(getFilename(request, pagename, file))[6]) # in byte 655 fsize += float(os.stat(getFilename(request, pagename, file))[6]) # in byte
656 fcount += 1
655 657
656 available_attachments_file_space = attachments_file_space - fsize 658 available_attachments_file_space = attachments_file_space - fsize
657 659 available_attachments_file_count = attachments_file_count - fcount
660
658 if zipfile.is_zipfile(fpath): 661 if zipfile.is_zipfile(fpath):
659 zf = zipfile.ZipFile(fpath) 662 zf = zipfile.ZipFile(fpath)
660 sum_size_over_all_valid_files = 0.0 663 sum_size_over_all_valid_files = 0.0
661 count_valid_files = 0 664 count_valid_files = 0
662 for name in zf.namelist(): 665 for name in zf.namelist():
663 if valid_pathname(name): 666 if valid_pathname(name):
664 sum_size_over_all_valid_files += zf.getinfo(name).file_size 667 sum_size_over_all_valid_files += zf.getinfo(name).file_size
665 count_valid_files += 1 668 count_valid_files += 1
666 669
667 if (sum_size_over_all_valid_files < available_attachments_file_space and 670 if sum_size_over_all_valid_files > available_attachments_file_space:
668 count_valid_files < max_file_count): 671 msg=_("Attachment '%(filename)s' could not be unzipped because"
672 " the resulting files would be too large (%(space)d kB"
673 " missing).") % {
674 'filename': filename,
675 'space': (sum_size_over_all_valid_files -
676 available_attachments_file_space) / 1000 }
677 elif count_valid_files > available_attachments_file_count:
678 msg=_("Attachment '%(filename)s' could not be unzipped because"
679 " the resulting files would be too many (%(count)d "
680 "missing).") % {
681 'filename': filename,
682 'count': (count_valid_files -
683 available_attachments_file_count) }
684 else:
669 valid_name = False 685 valid_name = False
670 for name in zf.namelist(): 686 for name in zf.namelist():
671 if valid_pathname(name): 687 if valid_pathname(name):
672 zi = zf.getinfo(name) 688 zi = zf.getinfo(name)
673 if zi.file_size < single_file_size: 689 if zi.file_size < single_file_size:
688 msg=_("Attachment '%(filename)s' unzipped.") % {'filename': filename} 704 msg=_("Attachment '%(filename)s' unzipped.") % {'filename': filename}
689 else: 705 else:
690 msg=_("Attachment '%(filename)s' not unzipped because the " 706 msg=_("Attachment '%(filename)s' not unzipped because the "
691 "files are too big, .zip files only, exist already or " 707 "files are too big, .zip files only, exist already or "
692 "reside in folders.") % {'filename': filename} 708 "reside in folders.") % {'filename': filename}
693 else:
694 msg=_("Attachment '%(filename)s' could not be unzipped because"
695 " the resulting files would be too large (%(space)d kB"
696 " missing) or more than %(count)d files.") % {
697 'filename': filename,
698 'space': (sum_size_over_all_valid_files -
699 available_attachments_file_space) / 1000,
700 'count': max_file_count }
701 else: 709 else:
702 msg = _('The file %(target)s is not a .zip file.' % target) 710 msg = _('The file %(target)s is not a .zip file.' % target)
703 711
704 upload_form(pagename, request, msg=wikiutil.escape(msg)) 712 upload_form(pagename, request, msg=wikiutil.escape(msg))
705 713