Attachment 'AttachFile-1.5.zipsubdir.patch'
Download 1 --- AttachFile.py.orig 2005-11-23 15:56:29.013407040 -0600
2 +++ AttachFile.py 2005-11-23 16:28:48.432570512 -0600
3 @@ -22,6 +22,7 @@
4 @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
5 @copyright: 2005 R. Bauer
6 @copyright: 2005 MoinMoin:AlexanderSchremmer
7 + @copyright: 2005 DiegoOngaro at ETSZONE (diego@etszone.com)
8 @license: GNU GPL, see COPYING for details.
9 """
10
11 @@ -271,6 +272,29 @@
12 def _get_filelist(request, pagename):
13 return _build_filelist(request, pagename, 1, 0)
14
15 +def _subdir_exception(zf):
16 + """
17 + Checks for the existance of one common subdirectory shared among
18 + all files in the zip file. If this is the case, returns a dict of
19 + original names to modified names so that such files can be unpacked
20 + as the user would expect.
21 + """
22 +
23 + b = zf.namelist()
24 + if not '/' in b[0]:
25 + return False #No directory
26 + slashoffset = b[0].index('/')
27 + directory = b[0][:slashoffset]
28 + for origname in b:
29 + if '/' in origname and origname.rindex('/') != slashoffset:
30 + return False #Multiple directories or different length directory
31 + for origname in b:
32 + if origname[:slashoffset] != directory:
33 + return False #One, same-length, but different directory
34 + names = {}
35 + for origname in b:
36 + names[origname] = origname[slashoffset+1:]
37 + return names #Returns dict of {origname: safename}
38
39 def error_msg(pagename, request, msg):
40 Page(request, pagename).send_page(request, msg=msg)
41 @@ -662,9 +686,15 @@
42 zf = zipfile.ZipFile(fpath)
43 sum_size_over_all_valid_files = 0.0
44 count_valid_files = 0
45 - for name in zf.namelist():
46 - if valid_pathname(name):
47 - sum_size_over_all_valid_files += zf.getinfo(name).file_size
48 + namelist = _subdir_exception(zf)
49 + if namelist == False: #if it's not handled by _subdir_exception()
50 + #Convert normal zf.namelist() to {origname:finalname} dict
51 + namelist = {}
52 + for name in zf.namelist():
53 + namelist[name] = name
54 + for (origname, finalname) in namelist.iteritems():
55 + if valid_pathname(finalname):
56 + sum_size_over_all_valid_files += zf.getinfo(origname).file_size
57 count_valid_files += 1
58
59 if sum_size_over_all_valid_files > available_attachments_file_space:
60 @@ -683,14 +713,14 @@
61 available_attachments_file_count) }
62 else:
63 valid_name = False
64 - for name in zf.namelist():
65 - if valid_pathname(name):
66 - zi = zf.getinfo(name)
67 + for (oirgname, finalname) in namelist.iteritems():
68 + if valid_pathname(finalname):
69 + zi = zf.getinfo(origname)
70 if zi.file_size < single_file_size:
71 - new_file = getFilename(request, pagename, name)
72 + new_file = getFilename(request, pagename, finalname)
73 if not os.path.exists(new_file):
74 outfile = open(new_file, 'wb')
75 - outfile.write(zf.read(name))
76 + outfile.write(zf.read(origname))
77 outfile.close()
78 # it's not allowed to zip a zip file so it is dropped
79 if zipfile.is_zipfile(new_file):
80 @@ -698,7 +728,7 @@
81 else:
82 valid_name = True
83 os.chmod(new_file, 0666 & config.umask)
84 - _addLogEntry(request, 'ATTNEW', pagename, name)
85 + _addLogEntry(request, 'ATTNEW', pagename, finalname)
86
87 if valid_name:
88 msg=_("Attachment '%(filename)s' unzipped.") % {'filename': filename}
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.