Attachment 'patch_AttachFile.py'
Download 1 --- OLD/AttachFile.py 2005-04-08 09:57:16.179409904 +0200
2 +++ NEW/AttachFile.py 2005-04-08 09:56:45.953585457 +0200
3 @@ -21,12 +21,14 @@
4 @copyright: 2001 by Ken Sugino (sugino@mediaone.net)
5 @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
6 @license: GNU GPL, see COPYING for details.
7 + 2005 April R.Bauer zipfile code added
8 """
9
10 import os, mimetypes, time, urllib
11 from MoinMoin import config, user, util, wikiutil
12 from MoinMoin.Page import Page
13 from MoinMoin.util import MoinMoinNoFooter, filesys, web
14 +import zipfile,string
15
16 action_name = __name__.split('.')[-1]
17
18 @@ -54,7 +56,7 @@
19 # direct file access via webserver, from public htdocs area
20 pagename = wikiutil.quoteWikinameFS(pagename)
21 attach_dir = os.path.join(request.cfg.attachments['dir'], pagename, "attachments")
22 - if create and not os.path.isdir(attach_dir):
23 + if create and not os.path.isdir(attach_dir):
24 filesys.makeDirs(attach_dir)
25 else:
26 # send file via CGI, from page storage area
27 @@ -203,6 +205,7 @@
28 label_get = _("get")
29 label_edit = _("edit")
30 label_view = _("view")
31 + label_unzip = _("unzip")
32
33 for file in files:
34 fsize = float(os.stat(os.path.join(attach_dir,file).encode(config.charset))[6]) # in byte
35 @@ -218,16 +221,19 @@
36 'urlfile': urlfile, 'label_del': label_del,
37 'base': base, 'label_edit': label_edit,
38 'label_view': label_view,
39 + 'label_unzip': label_unzip,
40 'get_url': get_url, 'label_get': label_get,
41 'file': file, 'fsize': fsize,
42 'pagename': pagename}
43 -
44 +
45 del_link = ''
46 if request.user.may.delete(pagename) and not readonly:
47 del_link = '<a href="%(baseurl)s/%(urlpagename)s' \
48 '?action=%(action)s&do=del&target=%(urlfile)s">%(label_del)s</a> | ' % parmdict
49 + if zipfile.is_zipfile(os.path.join(attach_dir,file))and request.user.may.read(pagename) and request.user.may.delete(pagename) and request.user.may.write(pagename):
50 + viewlink='<a href="%(baseurl)s/%(urlpagename)s?action=%(action)s&do=unzip&target=%(urlfile)s">%(label_unzip)s</a>' % parmdict
51
52 - if ext == '.draw':
53 + elif ext == '.draw':
54 viewlink = '<a href="%(baseurl)s/%(urlpagename)s?action=%(action)s&drawing=%(base)s">%(label_edit)s</a>' % parmdict
55 else:
56 viewlink = '<a href="%(baseurl)s/%(urlpagename)s?action=%(action)s&do=view&target=%(urlfile)s">%(label_view)s</a>' % parmdict
57 @@ -367,7 +373,7 @@
58 'pagename': wikiutil.quoteWikinameURL(pagename),
59 'action_name': action_name,
60 'upload_label_file': _('File to upload'),
61 - 'upload_label_rename': _('Save as'),
62 + 'upload_label_rename': _('Rename to'),
63 'rename': request.form.get('rename', [''])[0],
64 'upload_button': _('Upload'),
65 })
66 @@ -418,6 +424,11 @@
67 get_file(pagename, request)
68 else:
69 msg = _('You are not allowed to get attachments from this page.')
70 + elif request.form['do'][0] == 'unzip':
71 + if request.user.may.delete(pagename) and request.user.may.read(pagename) and request.user.may.write(pagename):
72 + unzip_file(pagename, request)
73 + else:
74 + msg = _('You are not allowed to unzip attachments of this page.')
75 elif request.form['do'][0] == 'view':
76 if request.user.may.read(pagename):
77 view_file(pagename, request)
78 @@ -442,7 +453,7 @@
79 request.write('</div>\n') # end content div
80 wikiutil.send_footer(request, pagename, showpage=1)
81
82 -
83 +
84 def do_upload(pagename, request):
85 _ = request.getText
86
87 @@ -457,7 +468,7 @@
88 # if we use twisted, "rename" field is NOT optional, because we
89 # can't access the client filename
90 if rename:
91 - filename = target = rename
92 + target = rename
93 elif filename:
94 target = filename
95 else:
96 @@ -466,7 +477,7 @@
97
98 # get file content
99 filecontent = request.form['file'][0]
100 -
101 +
102 target = wikiutil.taintfilename(target)
103
104 # set mimetype from extension, or from given mimetype
105 @@ -482,7 +493,7 @@
106 # else:
107 # ext = ''
108 # target = target + ext
109 -
110 +
111 # get directory, and possibly create it
112 attach_dir = getAttachDir(request, pagename, create=1)
113 # save file
114 @@ -509,7 +520,7 @@
115
116
117 def save_drawing(pagename, request):
118 -
119 +
120 filename = request.form['filename'][0]
121 filecontent = request.form['filepath'][0]
122
123 @@ -544,7 +555,7 @@
124
125 filename, fpath = _access_file(pagename, request)
126 if not filename: return # error msg already sent in _access_file
127 -
128 +
129 # delete file
130 os.remove(fpath)
131 _addLogEntry(request, 'ATTDEL', pagename, filename)
132 @@ -557,7 +568,7 @@
133
134 filename, fpath = _access_file(pagename, request)
135 if not filename: return # error msg already sent in _access_file
136 -
137 +
138 # get mimetype
139 type, enc = mimetypes.guess_type(filename)
140 if not type:
141 @@ -569,14 +580,84 @@
142 "Content-Length: %d" % os.path.getsize(fpath),
143 # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
144 # There is no solution that is compatible to IE except stripping non-ascii chars
145 - "Content-Disposition: inline; filename=\"%s\"" % filename.encode(config.charset),
146 + "Content-Disposition: inline; filename=\"%s\"" % filename.encode(config.charset),
147 ])
148 -
149 +
150 # send data
151 shutil.copyfileobj(open(fpath, 'rb'), request, 8192)
152
153 raise MoinMoinNoFooter
154
155 +def unzip_file(pagename, request):
156 + _ = request.getText
157 +
158 + filename, fpath = _access_file(pagename, request)
159 + if not filename: return # error msg already sent in _access_file
160 +
161 + if not request.user.may.delete(pagename):
162 + request.write('<p>%s</p>' % _('You are not allowed to unzip on this page.'))
163 + return
164 +
165 + attachment_path = getAttachDir(request, pagename)
166 + single_file_size=2.0*1024*1024
167 + attachments_file_space=200.0*1024*1024
168 +
169 + files = _get_files(request, pagename)
170 +
171 + msg=""
172 + if files:
173 + fsize =0.0
174 + for file in files:
175 + fsize += float(os.stat(os.path.join(attachment_path,file).encode(config.charset))[6]) # in byte
176 +
177 + avalaible_attachments_file_space=attachments_file_space-fsize
178 +
179 + target=request.form['target'][0]
180 + file=attachment_path+'/'+target
181 + if zipfile.is_zipfile(file):
182 + zf = zipfile.ZipFile(file)
183 + sum_size_over_all_valid_files=0.0
184 + for name in zf.namelist():
185 + if ((string.find(name,'/') == -1) and (string.find(name,'\\') == -1)):
186 + zi = zf.getinfo(name)
187 + sum_size_over_all_valid_files+=zi.file_size
188 +
189 + if (sum_size_over_all_valid_files < avalaible_attachments_file_space):
190 + valid_name=[]
191 + for name in zf.namelist():
192 + if ((string.find(name,'/') == -1) and (string.find(name,'\\') == -1)):
193 + zi = zf.getinfo(name)
194 + if (zi.file_size < (single_file_size)):
195 + new_file=os.path.join(attachment_path, name).encode(config.charset)
196 + if not os.path.exists(new_file):
197 + outfile = open(new_file, 'wb')
198 + outfile.write(zf.read(name))
199 + outfile.flush()
200 + outfile.close()
201 + # it's not allowed to zip a zip file so this is dropped
202 + if zipfile.is_zipfile(new_file):
203 + os.unlink(new_file)
204 + else:
205 + valid_name.append(name)
206 + os.chmod(new_file, 0666 & config.umask)
207 + _addLogEntry(request, 'ATTNEW', pagename, new_file)
208 +
209 + if (len(valid_name) > 0):
210 + upload_form(pagename, request, msg=_("Attachment '%(filename)s' unziped.") %
211 + {'filename': target})
212 + else:
213 + upload_form(pagename, request, msg=_("Attachment '%(filename)s' not unziped because result to big or included zip file or file exists.") %
214 + {'filename': target})
215 + else:
216 + upload_form(pagename, request, msg=_("Attachment '%(filename)s' could not be unziped because result to big (%(space)d kB missing).") %
217 + {'filename': target,
218 + 'space':(sum_size_over_all_valid_files-avalaible_attachments_file_space)/1024})
219 +
220 + else:
221 + msg = _('file '+target+' is no zip file!')
222 +
223 + if msg:
224 + error_msg(pagename, request, msg)
225
226 def send_viewfile(pagename, request):
227 _ = request.getText
228 @@ -585,7 +666,7 @@
229 if not filename: return
230
231 request.write('<h2>' + _("Attachment '%(filename)s'") % {'filename': filename} + '</h2>')
232 -
233 +
234 type, enc = mimetypes.guess_type(filename)
235 if type:
236 if type[:5] == 'image':
237 @@ -609,7 +690,7 @@
238 request.write('<a href="%s">%s</a>' % (
239 getAttachUrl(pagename, filename, request, escaped=1), wikiutil.escape(filename)))
240
241 -
242 +
243 def view_file(pagename, request):
244 _ = request.getText
245
246
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.