annotate MoinMoin/action/AttachFile.py @ 4539:5c4043e651b3

AttachFile: fix escaping problems (invalid html, but likely no XSS)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 29 Jan 2009 21:36:30 +0100
parents 8cb4d34ccbc1
children 0d03855518a4 ca61dedd6fda
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 MoinMoin - AttachFile action
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5 This action lets a page have multiple attachment files.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6 It creates a folder <data>/pages/<pagename>/attachments
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
7 and keeps everything in there.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 Form values: action=Attachment
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
10 1. with no 'do' key: returns file upload form
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
11 2. do=attach: accept file upload and saves the file in
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12 ../attachment/pagename/
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
13 3. /pagename/fname?action=Attachment&do=get[&mimetype=type]:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
14 return contents of the attachment file with the name fname.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
15 4. /pathname/fname, do=view[&mimetype=type]:create a page
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
16 to view the content of the file
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
17
3073
78c516164e01 attachment links now require double brackets
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3040
diff changeset
18 To link to an attachment, use [[attachment:file.txt]],
78c516164e01 attachment links now require double brackets
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3040
diff changeset
19 to embed an attachment, use {{attachment:file.png}}.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
20
1911
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
21 @copyright: 2001 by Ken Sugino (sugino@mediaone.net),
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1911
diff changeset
22 2001-2004 by Juergen Hermann <jh@web.de>,
1911
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
23 2005 MoinMoin:AlexanderSchremmer,
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
24 2005 DiegoOngaro at ETSZONE (diego@etszone.com),
1952
8ab85e3711dc AttachFile:send_viewfile extended for mimetypes presentable by EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1920
diff changeset
25 2005-2007 MoinMoin:ReimarBauer,
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
26 2007-2008 MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
27 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
28 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
29
2846
4825f1a252dc catch error when saving a drawing
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
30 import os, time, zipfile, mimetypes, errno
2490
111868e5cb21 Regenerate xapian index on added attachment, fix PageDeletedEvent, fix header
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2356
diff changeset
31
3591
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
32 from MoinMoin import log
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
33 logging = log.getLogger(__name__)
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
34
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1777
diff changeset
35 from MoinMoin import config, wikiutil, packages
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
36 from MoinMoin.Page import Page
1115
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
37 from MoinMoin.util import filesys, timefuncs
2983
7b0aadb97d01 new antispam stuff: textchas (text CAPTCHAs), cleanup AttachFile handler (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2966
diff changeset
38 from MoinMoin.security.textcha import TextCha
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2017
diff changeset
39 from MoinMoin.events import FileAttachedEvent, send_event
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
40
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
41 action_name = __name__.split('.')[-1]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
42
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
43 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
44 ### External interface - these are called from the core code
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
45 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
47 class AttachmentAlreadyExists(Exception):
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
48 pass
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
49
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
50
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
51 def getBasePath(request):
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
52 """ Get base path where page dirs for attachments are stored. """
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
53 return request.rootpage.getPagePath('pages')
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
54
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
55
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
56 def getAttachDir(request, pagename, create=0):
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
57 """ Get directory where attachments for page `pagename` are stored. """
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
58 if request.page and pagename == request.page.page_name:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
59 page = request.page # reusing existing page obj is faster
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
60 else:
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
61 page = Page(request, pagename)
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
62 return page.getPagePath("attachments", check_create=create)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
64
143
11df1156d432 fixed MoinMoinBugs/BrokenAttachmentPaths
Florian Festi <Florian.Festi@trick.informatik.uni-stuttgart.de>
parents: 118
diff changeset
65 def absoluteName(url, pagename):
281
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
66 """ Get (pagename, filename) of an attachment: link
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
67 @param url: PageName/filename.ext or filename.ext (unicode)
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
68 @param pagename: name of the currently processed page (unicode)
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
69 @rtype: tuple of unicode
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
70 @return: PageName, filename.ext
143
11df1156d432 fixed MoinMoinBugs/BrokenAttachmentPaths
Florian Festi <Florian.Festi@trick.informatik.uni-stuttgart.de>
parents: 118
diff changeset
71 """
3084
a6aaae4bded0 fix relative attachment targets
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3073
diff changeset
72 url = wikiutil.AbsPageName(pagename, url)
281
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
73 pieces = url.split(u'/')
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
74 if len(pieces) == 1:
143
11df1156d432 fixed MoinMoinBugs/BrokenAttachmentPaths
Florian Festi <Florian.Festi@trick.informatik.uni-stuttgart.de>
parents: 118
diff changeset
75 return pagename, pieces[0]
11df1156d432 fixed MoinMoinBugs/BrokenAttachmentPaths
Florian Festi <Florian.Festi@trick.informatik.uni-stuttgart.de>
parents: 118
diff changeset
76 else:
145
b11bb396654e make Attachment fix run with Py 2.3
Florian Festi <Florian.Festi@trick.informatik.uni-stuttgart.de>
parents: 143
diff changeset
77 return u"/".join(pieces[:-1]), pieces[-1]
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
78
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
79
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
80 def attachUrl(request, pagename, filename=None, **kw):
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
81 # filename is not used yet, but should be used later to make a sub-item url
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
82 if kw:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
83 qs = '?%s' % wikiutil.makeQueryString(kw, want_unicode=False)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
84 else:
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
85 qs = ''
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
86 return "%s/%s%s" % (request.getScriptname(), wikiutil.quoteWikinameURL(pagename), qs)
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
87
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
88
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
89 def getAttachUrl(pagename, filename, request, addts=0, escaped=0, do='get', drawing='', upload=False):
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
90 """ Get URL that points to attachment `filename` of page `pagename`. """
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
91 if upload:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
92 if not drawing:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
93 url = attachUrl(request, pagename, filename,
3267
65be8803b8df Attachfile.getAttachUrl: fixed upload tainting of rename
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3266
diff changeset
94 rename=wikiutil.taintfilename(filename), action=action_name)
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
95 else:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
96 url = attachUrl(request, pagename, filename,
3267
65be8803b8df Attachfile.getAttachUrl: fixed upload tainting of rename
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3266
diff changeset
97 rename=wikiutil.taintfilename(filename), drawing=drawing, action=action_name)
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
98 else:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
99 if not drawing:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
100 url = attachUrl(request, pagename, filename,
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
101 target=filename, action=action_name, do=do)
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
102 else:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
103 url = attachUrl(request, pagename, filename,
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
104 drawing=drawing, action=action_name)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
105 if escaped:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
106 url = wikiutil.escape(url)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
107 return url
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
108
2701
398af77c7ede fixed attachment_link api - it now has a 'on' parameter like most of the other methods
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2601
diff changeset
109
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
110 def getIndicator(request, pagename):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
111 """ Get an attachment indicator for a page (linked clip image) or
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
112 an empty string if not attachments exist.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
113 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
114 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
115 attach_dir = getAttachDir(request, pagename)
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
116 if not os.path.exists(attach_dir):
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
117 return ''
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
118
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
119 files = os.listdir(attach_dir)
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
120 if not files:
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
121 return ''
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
122
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
123 fmt = request.formatter
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
124 attach_count = _('[%d attachments]') % len(files)
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 869
diff changeset
125 attach_icon = request.theme.make_icon('attach', vars={'attach_count': attach_count})
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
126 attach_link = (fmt.url(1, attachUrl(request, pagename, action=action_name), rel='nofollow') +
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
127 attach_icon +
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
128 fmt.url(0))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
129 return attach_link
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
130
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
131
281
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
132 def getFilename(request, pagename, filename):
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
133 """ make complete pathfilename of file "name" attached to some page "pagename"
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
134 @param request: request object
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
135 @param pagename: name of page where the file is attached to (unicode)
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
136 @param filename: filename of attached file (unicode)
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
137 @rtype: string (in config.charset encoding)
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
138 @return: complete path/filename of attached file
8155e50e94ca fixed crash with non-ASCII attachment filename
Thomas Waldmann <tw@waldmann-edv.de>
parents: 280
diff changeset
139 """
1911
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
140 if isinstance(filename, unicode):
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
141 filename = filename.encode(config.charset)
2885
48a1f6b6b6c5 AttachFile.getFilename: creates attachment dir on requesting filename
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2846
diff changeset
142 return os.path.join(getAttachDir(request, pagename, create=1), filename)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
143
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
144
1910
5c3dffe2abf1 new function AttachFile.exists to reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1904
diff changeset
145 def exists(request, pagename, filename):
5c3dffe2abf1 new function AttachFile.exists to reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1904
diff changeset
146 """ check if page <pagename> has a file <filename> attached """
5c3dffe2abf1 new function AttachFile.exists to reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1904
diff changeset
147 fpath = getFilename(request, pagename, filename)
5c3dffe2abf1 new function AttachFile.exists to reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1904
diff changeset
148 return os.path.exists(fpath)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
149
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
150
1911
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
151 def size(request, pagename, filename):
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
152 """ return file size of file attachment """
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
153 fpath = getFilename(request, pagename, filename)
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
154 return os.path.getsize(fpath)
0b2ad3099fcf AttachFile.size to reduce code duplication, fixed docstring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1910
diff changeset
155
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
156
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
157 def info(pagename, request):
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
158 """ Generate snippet with info on the attachment for page `pagename`. """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
159 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
160
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
161 attach_dir = getAttachDir(request, pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
162 files = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
163 if os.path.isdir(attach_dir):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
164 files = os.listdir(attach_dir)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
165 page = Page(request, pagename)
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
166 link = page.url(request, {'action': action_name})
3122
a1322262398a refactored _() getText calls to match new api
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3084
diff changeset
167 attach_info = _('There are <a href="%(link)s">%(count)s attachment(s)</a> stored for this page.') % {
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
168 'count': len(files),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
169 'link': wikiutil.escape(link)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
170 }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
171 return "\n<p>\n%s\n</p>\n" % attach_info
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
172
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
173
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
174 def _write_stream(content, stream, bufsize=8192):
3603
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
175 if hasattr(content, 'read'): # looks file-like
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
176 import shutil
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
177 shutil.copyfileobj(content, stream, bufsize)
3603
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
178 elif isinstance(content, str):
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
179 stream.write(content)
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
180 else:
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
181 logging.error("unsupported content object: %r" % content)
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
182 raise
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
183
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1777
diff changeset
184 def add_attachment(request, pagename, target, filecontent, overwrite=0):
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
185 """ save <filecontent> to an attachment <target> of page <pagename>
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
186
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
187 filecontent can be either a str (in memory file content),
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
188 or an open file object (file content in e.g. a tempfile).
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
189 """
1797
25c93ded258b AttachFile: getText of add_attachment fixed, Load:only PageEditor restricts uploading of pages
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1796
diff changeset
190 _ = request.getText
25c93ded258b AttachFile: getText of add_attachment fixed, Load:only PageEditor restricts uploading of pages
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1796
diff changeset
191
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
192 # replace illegal chars
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
193 target = wikiutil.taintfilename(target)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
194
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
195 # get directory, and possibly create it
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
196 attach_dir = getAttachDir(request, pagename, create=1)
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
197 # save file
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
198 fpath = os.path.join(attach_dir, target).encode(config.charset)
1765
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
199 exists = os.path.exists(fpath)
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
200 if exists and not overwrite:
3591
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
201 raise AttachmentAlreadyExists
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
202 else:
1765
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
203 if exists:
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
204 try:
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
205 os.remove(fpath)
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
206 except:
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
207 pass
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
208 stream = open(fpath, 'wb')
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
209 try:
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
210 _write_stream(filecontent, stream)
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
211 finally:
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
212 stream.close()
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
213
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
214 _addLogEntry(request, 'ATTNEW', pagename, target)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2210
diff changeset
215
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
216 filesize = os.path.getsize(fpath)
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
217 event = FileAttachedEvent(request, pagename, target, filesize)
2490
111868e5cb21 Regenerate xapian index on added attachment, fix PageDeletedEvent, fix header
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2356
diff changeset
218 send_event(event)
1473
b5864c9492fb ensure new attachments trigger an index update, doc update for MoinMoin.search.Xapian
Franz Pletz <fpletz AT franz-pletz DOT org>
parents: 1318
diff changeset
219
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
220 return target, filesize
802
7b2e550c9660 merge moin/1.6
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 801 691
diff changeset
221
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
222
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
223 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
224 ### Internal helpers
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
225 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
226
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
227 def _addLogEntry(request, action, pagename, filename):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
228 """ Add an entry to the edit log on uploads and deletes.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
229
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
230 `action` should be "ATTNEW" or "ATTDEL"
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
231 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
232 from MoinMoin.logfile import editlog
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
233 t = wikiutil.timestamp2version(time.time())
101
2202f548cbb0 use own urllib wrapper, see wikiutil
Thomas Waldmann <tw@waldmann-edv.de>
parents: 80
diff changeset
234 fname = wikiutil.url_quote(filename, want_unicode=True)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
235
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
236 # Write to global log
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
237 log = editlog.EditLog(request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
238 log.add(request, t, 99999999, action, pagename, request.remote_addr, fname)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
239
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
240 # Write to local log
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
241 log = editlog.EditLog(request, rootpagename=pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
242 log.add(request, t, 99999999, action, pagename, request.remote_addr, fname)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
243
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
244
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
245 def _access_file(pagename, request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
246 """ Check form parameter `target` and return a tuple of
3169
a654cf294f4e AttachFile: fix _access_file return parameter count
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3164
diff changeset
247 `(pagename, filename, filepath)` for an existing attachment.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
248
3169
a654cf294f4e AttachFile: fix _access_file return parameter count
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3164
diff changeset
249 Return `(pagename, None, None)` if an error occurs.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
250 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
251 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
252
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
253 error = None
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
254 if not request.form.get('target', [''])[0]:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
255 error = _("Filename of attachment not specified!")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
256 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
257 filename = wikiutil.taintfilename(request.form['target'][0])
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
258 fpath = getFilename(request, pagename, filename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
259
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
260 if os.path.isfile(fpath):
3169
a654cf294f4e AttachFile: fix _access_file return parameter count
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3164
diff changeset
261 return (pagename, filename, fpath)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
262 error = _("Attachment '%(filename)s' does not exist!") % {'filename': filename}
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
263
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
264 error_msg(pagename, request, error)
3169
a654cf294f4e AttachFile: fix _access_file return parameter count
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3164
diff changeset
265 return (pagename, None, None)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
266
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
267
1653
5b15b6e010cf AttachList can list files of given mimetype now (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1635
diff changeset
268 def _build_filelist(request, pagename, showheader, readonly, mime_type='*'):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
269 _ = request.getText
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
270 fmt = request.html_formatter
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
271
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
272 # access directory
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
273 attach_dir = getAttachDir(request, pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
274 files = _get_files(request, pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
275
1653
5b15b6e010cf AttachList can list files of given mimetype now (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1635
diff changeset
276 if mime_type != '*':
5b15b6e010cf AttachList can list files of given mimetype now (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1635
diff changeset
277 files = [fname for fname in files if mime_type == mimetypes.guess_type(fname)[0]]
5b15b6e010cf AttachList can list files of given mimetype now (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1635
diff changeset
278
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
279 html = []
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
280 if files:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
281 if showheader:
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
282 html.append(fmt.rawHTML(_(
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
283 "To refer to attachments on a page, use '''{{{attachment:filename}}}''', \n"
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
284 "as shown below in the list of files. \n"
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
285 "Do '''NOT''' use the URL of the {{{[get]}}} link, \n"
3122
a1322262398a refactored _() getText calls to match new api
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3084
diff changeset
286 "since this is subject to change and can break easily.",
a1322262398a refactored _() getText calls to match new api
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3084
diff changeset
287 wiki=True
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
288 )))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
289
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
290 label_del = _("del")
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
291 label_move = _("move")
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
292 label_get = _("get")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
293 label_edit = _("edit")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
294 label_view = _("view")
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
295 label_unzip = _("unzip")
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
296 label_install = _("install")
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
297
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
298 html.append(fmt.bullet_list(1))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
299 for file in files:
1985
eb5328be394e AttachFile:destinguish between zip and other fileformats using zip archives
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1967
diff changeset
300 mt = wikiutil.MimeType(filename=file)
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
301 fullpath = os.path.join(attach_dir, file).encode(config.charset)
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
302 st = os.stat(fullpath)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
303 base, ext = os.path.splitext(file)
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
304 parmdict = {'file': wikiutil.escape(file),
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
305 'fsize': "%.1f" % (float(st.st_size) / 1024),
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
306 'fmtime': request.user.getFormattedDateTime(st.st_mtime),
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
307 }
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
308
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
309 links = []
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
310 may_delete = request.user.may.delete(pagename)
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
311 if may_delete and not readonly:
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
312 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='del')) +
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
313 fmt.text(label_del) +
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
314 fmt.url(0))
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
315
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
316 if may_delete and not readonly:
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
317 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='move')) +
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
318 fmt.text(label_move) +
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
319 fmt.url(0))
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
320
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
321 links.append(fmt.url(1, getAttachUrl(pagename, file, request)) +
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
322 fmt.text(label_get) +
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
323 fmt.url(0))
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
324
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
325 if ext == '.draw':
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
326 links.append(fmt.url(1, getAttachUrl(pagename, file, request, drawing=base)) +
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
327 fmt.text(label_edit) +
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
328 fmt.url(0))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
329 else:
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
330 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='view')) +
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
331 fmt.text(label_view) +
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
332 fmt.url(0))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
333
3705
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
334 try:
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
335 is_zipfile = zipfile.is_zipfile(fullpath)
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
336 if is_zipfile:
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
337 is_package = packages.ZipPackage(request, fullpath).isPackage()
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
338 if is_package and request.user.isSuperUser():
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
339 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='install')) +
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
340 fmt.text(label_install) +
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
341 fmt.url(0))
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
342 elif (not is_package and mt.minor == 'zip' and
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
343 may_delete and
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
344 request.user.may.read(pagename) and
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
345 request.user.may.write(pagename)):
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
346 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='unzip')) +
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
347 fmt.text(label_unzip) +
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
348 fmt.url(0))
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
349 except RuntimeError:
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
350 # We don't want to crash with a traceback here (an exception
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
351 # here could be caused by an uploaded defective zip file - and
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
352 # if we crash here, the user does not get a UI to remove the
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
353 # defective zip file again).
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
354 # RuntimeError is raised by zipfile stdlib module in case of
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
355 # problems (like inconsistent slash and backslash usage in the
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
356 # archive).
ac48fad2e117 AttachFile action: catch runtime error raised by zipfile to not make attachments list unusable if someone attached a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3700
diff changeset
357 logging.exception("An exception within zip file attachment handling occurred:")
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
358
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
359 html.append(fmt.listitem(1))
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
360 html.append("[%s]" % "&nbsp;| ".join(links))
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
361 html.append(" (%(fmtime)s, %(fsize)s KB) [[attachment:%(file)s]]" % parmdict)
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
362 html.append(fmt.listitem(0))
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
363 html.append(fmt.bullet_list(0))
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
364
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
365 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
366 if showheader:
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
367 html.append(fmt.paragraph(1))
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
368 html.append(fmt.text(_("No attachments stored for %(pagename)s") % {
3270
e06e15e90ba7 fix some wrong wikiutil.escape usage causing double-escaping
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3253
diff changeset
369 'pagename': pagename}))
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
370 html.append(fmt.paragraph(0))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
371
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
372 return ''.join(html)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
373
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
374
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
375 def _get_files(request, pagename):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
376 attach_dir = getAttachDir(request, pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
377 if os.path.isdir(attach_dir):
1866
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1864
diff changeset
378 files = [fn.decode(config.charset) for fn in os.listdir(attach_dir)]
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
379 files.sort()
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
380 else:
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
381 files = []
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
382 return files
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
383
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
384
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
385 def _get_filelist(request, pagename):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
386 return _build_filelist(request, pagename, 1, 0)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
387
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
388
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
389 def error_msg(pagename, request, msg):
2966
ba14d391c2ba Refactor all modules to use the new add_msg interface in 1.7 (done by Frederico Lorenzi). Should not be backported to 1.6 but
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2885
diff changeset
390 request.theme.add_msg(msg, "error")
ba14d391c2ba Refactor all modules to use the new add_msg interface in 1.7 (done by Frederico Lorenzi). Should not be backported to 1.6 but
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2885
diff changeset
391 Page(request, pagename).send_page()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
392
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
393
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
394 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
395 ### Create parts of the Web interface
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
396 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
397
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
398 def send_link_rel(request, pagename):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
399 files = _get_files(request, pagename)
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
400 for fname in files:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
401 url = getAttachUrl(pagename, fname, request, do='view', escaped=1)
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
402 request.write(u'<link rel="Appendix" title="%s" href="%s">\n' % (
4539
5c4043e651b3 AttachFile: fix escaping problems (invalid html, but likely no XSS)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4484
diff changeset
403 wikiutil.escape(fname, 1), url))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
404
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
405
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
406 def send_hotdraw(pagename, request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
407 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
408
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
409 now = time.time()
1318
760002b5ffa3 use url_prefix_static = '/moin_static160' instead of url_prefix = '/wiki' to avoid user confusion and stale caches
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1317
diff changeset
410 pubpath = request.cfg.url_prefix_static + "/applets/TWikiDrawPlugin"
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
411 basename = request.form['drawing'][0]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
412 drawpath = getAttachUrl(pagename, basename + '.draw', request, escaped=1)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
413 pngpath = getAttachUrl(pagename, basename + '.png', request, escaped=1)
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
414 pagelink = attachUrl(request, pagename, '', action=action_name, ts=now)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
415 helplink = Page(request, "HelpOnActions/AttachFile").url(request)
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
416 savelink = attachUrl(request, pagename, '', action=action_name, do='savedrawing')
623
088282ae5cf3 move twikidraw saving stuff from request to AttachFile, ok for CGI, needs tests for Twisted and WSGI
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 617
diff changeset
417 #savelink = Page(request, pagename).url(request) # XXX include target filename param here for twisted
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
418 # request, {'savename': request.form['drawing'][0]+'.draw'}
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
419 #savelink = '/cgi-bin/dumpform.bat'
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
420
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
421 timestamp = '&amp;ts=%s' % now
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
422
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
423 request.write('<h2>' + _("Edit drawing") + '</h2>')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
424 request.write("""
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
425 <p>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
426 <img src="%(pngpath)s%(timestamp)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
427 <applet code="CH.ifa.draw.twiki.TWikiDraw.class"
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
428 archive="%(pubpath)s/twikidraw.jar" width="640" height="480">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
429 <param name="drawpath" value="%(drawpath)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
430 <param name="pngpath" value="%(pngpath)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
431 <param name="savepath" value="%(savelink)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
432 <param name="basename" value="%(basename)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
433 <param name="viewpath" value="%(pagelink)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
434 <param name="helppath" value="%(helplink)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
435 <strong>NOTE:</strong> You need a Java enabled browser to edit the drawing example.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
436 </applet>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
437 </p>""" % {
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
438 'pngpath': pngpath, 'timestamp': timestamp,
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
439 'pubpath': pubpath, 'drawpath': drawpath,
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
440 'savelink': savelink, 'pagelink': pagelink, 'helplink': helplink,
4539
5c4043e651b3 AttachFile: fix escaping problems (invalid html, but likely no XSS)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4484
diff changeset
441 'basename': wikiutil.escape(basename, 1),
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
442 })
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
443
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
444
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
445 def send_uploadform(pagename, request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
446 """ Send the HTML code for the list of already stored attachments and
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
447 the file upload form.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
448 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
449 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
450
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
451 if not request.user.may.read(pagename):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
452 request.write('<p>%s</p>' % _('You are not allowed to view this page.'))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
453 return
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
454
1635
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
455 writeable = request.user.may.write(pagename)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
456
1635
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
457 # First send out the upload new attachment form on top of everything else.
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
458 # This avoids usability issues if you have to scroll down a lot to upload
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
459 # a new file when the page already has lots of attachments:
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
460 if writeable:
3586
35366465d4fe AttachFile/Load: remove misleading/outdated text, Load: fix UI cosmetics, reuse i18n texts
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3572
diff changeset
461 request.write('<h2>' + _("New Attachment") + '</h2>')
1635
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
462 request.write("""
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
463 <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
464 <dl>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
465 <dt>%(upload_label_file)s</dt>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
466 <dd><input type="file" name="file" size="50"></dd>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
467 <dt>%(upload_label_rename)s</dt>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
468 <dd><input type="text" name="rename" size="50" value="%(rename)s"></dd>
1765
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
469 <dt>%(upload_label_overwrite)s</dt>
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
470 <dd><input type="checkbox" name="overwrite" value="1" %(overwrite_checked)s></dd>
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
471 </dl>
2983
7b0aadb97d01 new antispam stuff: textchas (text CAPTCHAs), cleanup AttachFile handler (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2966
diff changeset
472 %(textcha)s
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
473 <p>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
474 <input type="hidden" name="action" value="%(action_name)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
475 <input type="hidden" name="do" value="upload">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
476 <input type="submit" value="%(upload_button)s">
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
477 </p>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
478 </form>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
479 """ % {
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
480 'baseurl': request.getScriptname(),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
481 'pagename': wikiutil.quoteWikinameURL(pagename),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
482 'action_name': action_name,
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
483 'upload_label_file': _('File to upload'),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
484 'upload_label_rename': _('Rename to'),
4484
8cb4d34ccbc1 fix AttachFile XSS issues
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3712
diff changeset
485 'rename': wikiutil.escape(request.form.get('rename', [''])[0], 1),
1765
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
486 'upload_label_overwrite': _('Overwrite existing attachment of same name'),
6e438de156d9 AttachFile: allow overwriting files when user chooses that option (ported from 1.5-802)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1662
diff changeset
487 'overwrite_checked': ('', 'checked')[request.form.get('overwrite', ['0'])[0] == '1'],
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
488 'upload_button': _('Upload'),
2983
7b0aadb97d01 new antispam stuff: textchas (text CAPTCHAs), cleanup AttachFile handler (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2966
diff changeset
489 'textcha': TextCha(request).render(),
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
490 })
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
491
1635
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
492 request.write('<h2>' + _("Attached Files") + '</h2>')
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
493 request.write(_get_filelist(request, pagename))
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
494
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
495 if not writeable:
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
496 request.write('<p>%s</p>' % _('You are not allowed to attach a file to this page.'))
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
497
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
498 if writeable and request.form.get('drawing', [None])[0]:
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
499 send_hotdraw(pagename, request)
a41c60e53ddd display upload attachment form at top, avoids lots of scrolling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1611
diff changeset
500
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
501
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
502 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
503 ### Web interface for file upload, viewing and deletion
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
504 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
505
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
506 def execute(pagename, request):
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
507 """ Main dispatcher for the 'AttachFile' action. """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
508 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
509
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
510 do = request.form.get('do', ['upload_form'])
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
511 handler = globals().get('_do_%s' % do[0])
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
512 if handler:
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
513 msg = handler(pagename, request)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
514 else:
3164
c25bc6e9ad61 i18n.getText: removing wrong (old) kw arguments from _() calls
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3152
diff changeset
515 msg = _('Unsupported AttachFile sub-action: %s') % (wikiutil.escape(do[0]), )
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
516 if msg:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
517 error_msg(pagename, request, msg)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
518
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
519
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
520 def _do_upload_form(pagename, request):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
521 upload_form(pagename, request)
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
522
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
523
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
524 def upload_form(pagename, request, msg=''):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
525 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
526
1068
ecece5db5288 use emit_http_headers
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1006
diff changeset
527 request.emit_http_headers()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
528 # Use user interface language for this generated page
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
529 request.setContentLanguage(request.lang)
2966
ba14d391c2ba Refactor all modules to use the new add_msg interface in 1.7 (done by Frederico Lorenzi). Should not be backported to 1.6 but
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2885
diff changeset
530 request.theme.add_msg(msg, "dialog")
ba14d391c2ba Refactor all modules to use the new add_msg interface in 1.7 (done by Frederico Lorenzi). Should not be backported to 1.6 but
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2885
diff changeset
531 request.theme.send_title(_('Attachments for "%(pagename)s"') % {'pagename': pagename}, pagename=pagename)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
532 request.write('<div id="content">\n') # start content div
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
533 send_uploadform(pagename, request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
534 request.write('</div>\n') # end content div
616
3b08d9413589 move send_title/footer from wikiutil to theme.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 520
diff changeset
535 request.theme.send_footer(pagename)
617
cf420addd95c removed MoinMoinNoFooter at many places, added call to theme.send_closing_html() where needed
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 616
diff changeset
536 request.theme.send_closing_html()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
537
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
538
3591
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
539 def preprocess_filename(filename):
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
540 """ preprocess the filename we got from upload form,
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
541 strip leading drive and path (IE misbehaviour)
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
542 """
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
543 if filename and len(filename) > 1 and (filename[1] == ':' or filename[0] == '\\'): # C:.... or \path... or \\server\...
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
544 bsindex = filename.rfind('\\')
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
545 if bsindex >= 0:
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
546 filename = filename[bsindex+1:]
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
547 return filename
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
548
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
549
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
550 def _do_upload(pagename, request):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
551 _ = request.getText
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
552 # Currently we only check TextCha for upload (this is what spammers ususally do),
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
553 # but it could be extended to more/all attachment write access
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
554 if not TextCha(request).check_answer_from_form():
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
555 return _('TextCha: Wrong answer! Go back and try again...')
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
556
3591
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
557 form = request.form
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
558 overwrite = form.get('overwrite', [u'0'])[0]
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
559 try:
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
560 overwrite = int(overwrite)
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
561 except:
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
562 overwrite = 0
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
563
3595
1dcf6a261c2a AttachFile: make error msg less confusing when trying to overwrite a file attachment without having acl delete rights
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3591
diff changeset
564 if not request.user.may.write(pagename):
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
565 return _('You are not allowed to attach a file to this page.')
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
566
3595
1dcf6a261c2a AttachFile: make error msg less confusing when trying to overwrite a file attachment without having acl delete rights
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3591
diff changeset
567 if overwrite and not request.user.may.delete(pagename):
1dcf6a261c2a AttachFile: make error msg less confusing when trying to overwrite a file attachment without having acl delete rights
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3591
diff changeset
568 return _('You are not allowed to overwrite a file attachment of this page.')
1dcf6a261c2a AttachFile: make error msg less confusing when trying to overwrite a file attachment without having acl delete rights
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3591
diff changeset
569
3591
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
570 filename = form.get('file__filename__')
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
571 rename = form.get('rename', [u''])[0].strip()
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
572 if rename:
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
573 target = rename
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
574 else:
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
575 target = filename
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
576
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
577 target = preprocess_filename(target)
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
578 target = wikiutil.clean_input(target)
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
579
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
580 if not target:
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
581 return _("Filename of attachment not specified!")
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
582
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
583 # get file content
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
584 filecontent = request.form.get('file', [None])[0]
15e5ca7240ab Load action: remove duplicated code, add comment field, refactor. AttachFile action: refactor. Fix bugs.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3586
diff changeset
585 if filecontent is None:
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
586 # This might happen when trying to upload file names
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
587 # with non-ascii characters on Safari.
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
588 return _("No file content. Delete non ASCII characters from the file name and try again.")
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
589
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
590 # add the attachment
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
591 try:
3568
6fe1ea4d9d1a use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3553
diff changeset
592 target, bytes = add_attachment(request, pagename, target, filecontent, overwrite=overwrite)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
593 msg = _("Attachment '%(target)s' (remote name '%(filename)s')"
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
594 " with %(bytes)d bytes saved.") % {
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
595 'target': target, 'filename': filename, 'bytes': bytes}
691
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
596 except AttachmentAlreadyExists:
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
597 msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % {
f18b06c790d4 Refactored adding attachments.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 623
diff changeset
598 'target': target, 'filename': filename}
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
599
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
600 # return attachment list
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
601 upload_form(pagename, request, msg)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
602
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
603
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
604 def _do_savedrawing(pagename, request):
3180
6ad1f133b66d AttachFile action: fix AttributeError
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3175
diff changeset
605 _ = request.getText
6ad1f133b66d AttachFile action: fix AttributeError
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3175
diff changeset
606
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
607 if not request.user.may.write(pagename):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
608 return _('You are not allowed to save a drawing on this page.')
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
609
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
610 filename = request.form['filename'][0]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
611 filecontent = request.form['filepath'][0]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
612
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
613 basepath, basename = os.path.split(filename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
614 basename, ext = os.path.splitext(basename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
615
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
616 # get directory, and possibly create it
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
617 attach_dir = getAttachDir(request, pagename, create=1)
3572
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
618 savepath = os.path.join(attach_dir, basename + ext)
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
619
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
620 if ext == '.draw':
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
621 _addLogEntry(request, 'ATTDRW', pagename, basename + ext)
3603
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
622 filecontent = filecontent.read() # read file completely into memory
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 869
diff changeset
623 filecontent = filecontent.replace("\r", "")
3572
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
624 elif ext == '.map':
3603
43aa566b0ec7 AttachFile: fix uploading of < 1K files (FieldStorage seems to behave different for that case)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3595
diff changeset
625 filecontent = filecontent.read() # read file completely into memory
3572
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
626 filecontent = filecontent.strip()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
627
3572
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
628 if filecontent:
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
629 # filecontent is either a file or a non-empty string
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
630 stream = open(savepath, 'wb')
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
631 try:
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
632 _write_stream(filecontent, stream)
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
633 finally:
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
634 stream.close()
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
635 else:
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
636 # filecontent is empty string (e.g. empty map file), delete the target file
2846
4825f1a252dc catch error when saving a drawing
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
637 try:
4825f1a252dc catch error when saving a drawing
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
638 os.unlink(savepath)
4825f1a252dc catch error when saving a drawing
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
639 except OSError, err:
4825f1a252dc catch error when saving a drawing
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
640 if err.errno != errno.ENOENT: # no such file
4825f1a252dc catch error when saving a drawing
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
641 raise
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
642
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
643 # touch attachment directory to invalidate cache if new map is saved
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
644 if ext == '.map':
3572
870cc4c47705 AttachFile: fix saving of drawing files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3568
diff changeset
645 os.utime(attach_dir, None)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
646
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
647 request.emit_http_headers()
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
648 request.write("OK")
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
649
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
650
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
651 def _do_del(pagename, request):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
652 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
653
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
654 pagename, filename, fpath = _access_file(pagename, request)
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
655 if not request.user.may.delete(pagename):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
656 return _('You are not allowed to delete attachments on this page.')
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
657 if not filename:
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
658 return # error msg already sent in _access_file
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
659
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
660 # delete file
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
661 os.remove(fpath)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
662 _addLogEntry(request, 'ATTDEL', pagename, filename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
663
1479
b1562b232683 fix for deleting from xapian index
Franz Pletz <fpletz AT franz-pletz DOT org>
parents: 1473
diff changeset
664 if request.cfg.xapian_search:
b1562b232683 fix for deleting from xapian index
Franz Pletz <fpletz AT franz-pletz DOT org>
parents: 1473
diff changeset
665 from MoinMoin.search.Xapian import Index
b1562b232683 fix for deleting from xapian index
Franz Pletz <fpletz AT franz-pletz DOT org>
parents: 1473
diff changeset
666 index = Index(request)
b1562b232683 fix for deleting from xapian index
Franz Pletz <fpletz AT franz-pletz DOT org>
parents: 1473
diff changeset
667 if index.exists:
b1562b232683 fix for deleting from xapian index
Franz Pletz <fpletz AT franz-pletz DOT org>
parents: 1473
diff changeset
668 index.remove_item(pagename, filename)
b1562b232683 fix for deleting from xapian index
Franz Pletz <fpletz AT franz-pletz DOT org>
parents: 1473
diff changeset
669
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
670 upload_form(pagename, request, msg=_("Attachment '%(filename)s' deleted.") % {'filename': filename})
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
671
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
672
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
673 def move_file(request, pagename, new_pagename, attachment, new_attachment):
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
674 _ = request.getText
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
675
1609
7f73dafb525d move attachments: remove unneeded imports
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1608
diff changeset
676 newpage = Page(request, new_pagename)
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
677 if newpage.exists(includeDeleted=1) and request.user.may.write(new_pagename) and request.user.may.delete(pagename):
1609
7f73dafb525d move attachments: remove unneeded imports
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1608
diff changeset
678 new_attachment_path = os.path.join(getAttachDir(request, new_pagename,
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
679 create=1), new_attachment).encode(config.charset)
1609
7f73dafb525d move attachments: remove unneeded imports
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1608
diff changeset
680 attachment_path = os.path.join(getAttachDir(request, pagename),
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
681 attachment).encode(config.charset)
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
682
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
683 if os.path.exists(new_attachment_path):
3171
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
684 upload_form(pagename, request,
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
685 msg=_("Attachment '%(new_pagename)s/%(new_filename)s' already exists.") % {
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
686 'new_pagename': new_pagename,
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
687 'new_filename': new_attachment})
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
688 return
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
689
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
690 if new_attachment_path != attachment_path:
3171
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
691 # move file
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
692 filesys.rename(attachment_path, new_attachment_path)
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
693 _addLogEntry(request, 'ATTDEL', pagename, attachment)
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
694 _addLogEntry(request, 'ATTNEW', new_pagename, new_attachment)
3171
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
695 upload_form(pagename, request,
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
696 msg=_("Attachment '%(pagename)s/%(filename)s' moved to '%(new_pagename)s/%(new_filename)s'.") % {
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
697 'pagename': pagename,
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
698 'filename': attachment,
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
699 'new_pagename': new_pagename,
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
700 'new_filename': new_attachment})
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
701 else:
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
702 upload_form(pagename, request, msg=_("Nothing changed"))
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
703 else:
3171
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
704 upload_form(pagename, request, msg=_("Page '%(new_pagename)s' does not exist or you don't have enough rights.") % {
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
705 'new_pagename': new_pagename})
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
706
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
707
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
708 def _do_attachment_move(pagename, request):
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
709 _ = request.getText
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
710
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
711 if 'cancel' in request.form:
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
712 return _('Move aborted!')
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
713 if not wikiutil.checkTicket(request, request.form['ticket'][0]):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
714 return _('Please use the interactive user interface to move attachments!')
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
715 if not request.user.may.delete(pagename):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
716 return _('You are not allowed to move attachments from this page.')
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
717
1868
64507f46beb2 reduce usage of has_key()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1866
diff changeset
718 if 'newpagename' in request.form:
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
719 new_pagename = request.form.get('newpagename')[0]
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
720 else:
3171
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
721 upload_form(pagename, request, msg=_("Move aborted because new page name is empty."))
1868
64507f46beb2 reduce usage of has_key()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1866
diff changeset
722 if 'newattachmentname' in request.form:
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
723 new_attachment = request.form.get('newattachmentname')[0]
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
724 if new_attachment != wikiutil.taintfilename(new_attachment):
1662
3338af3c3867 update i18n, fix i18n tools
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1653
diff changeset
725 upload_form(pagename, request, msg=_("Please use a valid filename for attachment '%(filename)s'.") % {
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
726 'filename': new_attachment})
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
727 return
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
728 else:
3171
b23656c17a0b AttachFile: fix messages, remove unused code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3169
diff changeset
729 upload_form(pagename, request, msg=_("Move aborted because new attachment name is empty."))
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
730
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
731 attachment = request.form.get('oldattachmentname')[0]
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
732 move_file(request, pagename, new_pagename, attachment, new_attachment)
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
733
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
734
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
735 def _do_move(pagename, request):
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
736 _ = request.getText
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
737
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
738 pagename, filename, fpath = _access_file(pagename, request)
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
739 if not request.user.may.delete(pagename):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
740 return _('You are not allowed to move attachments from this page.')
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
741 if not filename:
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
742 return # error msg already sent in _access_file
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
743
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
744 # move file
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
745 d = {'action': action_name,
3040
8608b258f8e6 bug fix of MoinMoinBugs/MoveAttachmentNotWorkingWithModPython (thanks to Boleslaw Kulbabinski) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3023
diff changeset
746 'baseurl': request.getScriptname(),
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
747 'do': 'attachment_move',
1610
48194fc011e5 move attachments: fix ticket calls
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1609
diff changeset
748 'ticket': wikiutil.createTicket(request),
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
749 'pagename': pagename,
3040
8608b258f8e6 bug fix of MoinMoinBugs/MoveAttachmentNotWorkingWithModPython (thanks to Boleslaw Kulbabinski) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3023
diff changeset
750 'pagename_quoted': wikiutil.quoteWikinameURL(pagename),
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
751 'attachment_name': filename,
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
752 'move': _('Move'),
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
753 'cancel': _('Cancel'),
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
754 'newname_label': _("New page name"),
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
755 'attachment_label': _("New attachment name"),
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
756 }
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
757 formhtml = '''
3040
8608b258f8e6 bug fix of MoinMoinBugs/MoveAttachmentNotWorkingWithModPython (thanks to Boleslaw Kulbabinski) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3023
diff changeset
758 <form action="%(baseurl)s/%(pagename_quoted)s" method="POST">
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
759 <input type="hidden" name="action" value="%(action)s">
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
760 <input type="hidden" name="do" value="%(do)s">
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
761 <input type="hidden" name="ticket" value="%(ticket)s">
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
762 <table>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
763 <tr>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
764 <td class="label"><label>%(newname_label)s</label></td>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
765 <td class="content">
2708
5092edd82058 bug fix for non css browsers like w3m: for input fields set size of textarea and text to cols="80"
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2701
diff changeset
766 <input type="text" name="newpagename" value="%(pagename)s" size="80">
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
767 </td>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
768 </tr>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
769 <tr>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
770 <td class="label"><label>%(attachment_label)s</label></td>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
771 <td class="content">
2708
5092edd82058 bug fix for non css browsers like w3m: for input fields set size of textarea and text to cols="80"
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2701
diff changeset
772 <input type="text" name="newattachmentname" value="%(attachment_name)s" size="80">
1608
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
773 </td>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
774 </tr>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
775 <tr>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
776 <td></td>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
777 <td class="buttons">
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
778 <input type="hidden" name="oldattachmentname" value="%(attachment_name)s">
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
779 <input type="submit" name="move" value="%(move)s">
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
780 <input type="submit" name="cancel" value="%(cancel)s">
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
781 </td>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
782 </tr>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
783 </table>
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
784 </form>''' % d
7a26b4c769f3 attachment move
ReimarBauer <R.Bauer@fz-juelich.de>
parents: 1548
diff changeset
785 thispage = Page(request, pagename)
2966
ba14d391c2ba Refactor all modules to use the new add_msg interface in 1.7 (done by Frederico Lorenzi). Should not be backported to 1.6 but
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2885
diff changeset
786 request.theme.add_msg(formhtml, "dialog")
ba14d391c2ba Refactor all modules to use the new add_msg interface in 1.7 (done by Frederico Lorenzi). Should not be backported to 1.6 but
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2885
diff changeset
787 return thispage.send_page()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
788
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
789
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
790 def _do_get(pagename, request):
3180
6ad1f133b66d AttachFile action: fix AttributeError
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3175
diff changeset
791 _ = request.getText
6ad1f133b66d AttachFile action: fix AttributeError
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3175
diff changeset
792
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
793 pagename, filename, fpath = _access_file(pagename, request)
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
794 if not request.user.may.read(pagename):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
795 return _('You are not allowed to get attachments from this page.')
801
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
796 if not filename:
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
797 return # error msg already sent in _access_file
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
798
1115
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
799 timestamp = timefuncs.formathttpdate(int(os.path.getmtime(fpath)))
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
800 if request.if_modified_since == timestamp:
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
801 request.emit_http_headers(["Status: 304 Not modified"])
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
802 else:
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
803 mt = wikiutil.MimeType(filename=filename)
1548
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
804 content_type = mt.content_type()
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
805 mime_type = mt.mime_type()
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
806
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
807 # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
808 # There is no solution that is compatible to IE except stripping non-ascii chars
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
809 filename_enc = filename.encode(config.charset)
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
810
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
811 # for dangerous files (like .html), when we are in danger of cross-site-scripting attacks,
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
812 # we just let the user store them to disk ('attachment').
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
813 # For safe files, we directly show them inline (this also works better for IE).
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
814 dangerous = mime_type in request.cfg.mimetypes_xss_protect
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
815 content_dispo = dangerous and 'attachment' or 'inline'
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
816
1115
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
817 request.emit_http_headers([
1548
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
818 'Content-Type: %s' % content_type,
1798
cabab80d9597 AttachFile:merged with 1796:57f12932915f, EXIF:bug fixed in test/debug function
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1797
diff changeset
819 'Last-Modified: %s' % timestamp,
1548
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
820 'Content-Length: %d' % os.path.getsize(fpath),
2eb5117aa7de content-disposition for AttachFile downloads either inline or attachment depending on mimetype in cfg.mimetypes_xss_protect list
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1510
diff changeset
821 'Content-Disposition: %s; filename="%s"' % (content_dispo, filename_enc),
1115
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
822 ])
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
823
1115
a349adcabffe 304 response support for attached files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
824 # send data
3553
1052c105b16f new request.send_file() call, making it possible to use server-specific optimizations
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3525
diff changeset
825 request.send_file(open(fpath, 'rb'))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
826
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
827
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
828 def _do_install(pagename, request):
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
829 _ = request.getText
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
830
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
831 pagename, target, targetpath = _access_file(pagename, request)
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
832 if not request.user.isSuperUser():
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
833 return _('You are not allowed to install files.')
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
834 if not target:
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
835 return
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
836
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
837 package = packages.ZipPackage(request, targetpath)
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
838
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
839 if package.isPackage():
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
840 if package.installPackage():
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 869
diff changeset
841 msg = _("Attachment '%(filename)s' installed.") % {'filename': wikiutil.escape(target)}
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
842 else:
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 869
diff changeset
843 msg = _("Installation of '%(filename)s' failed.") % {'filename': wikiutil.escape(target)}
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
844 if package.msg:
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
845 msg += "<br><pre>%s</pre>" % wikiutil.escape(package.msg)
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
846 else:
2293
bb76818db210 i18n fixes, mostly _('aa'%xx) -> _('aa')%xx
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
847 msg = _('The file %s is not a MoinMoin package file.') % wikiutil.escape(target)
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
848
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
849 upload_form(pagename, request, msg=msg)
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
850
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
851
3700
9426f34f07b9 AttachFile: refactored unzip subaction code:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3603
diff changeset
852 def _do_unzip(pagename, request, overwrite=False):
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
853 _ = request.getText
3700
9426f34f07b9 AttachFile: refactored unzip subaction code:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3603
diff changeset
854 pagename, filename, fpath = _access_file(pagename, request)
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
855
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
856 if not (request.user.may.delete(pagename) and request.user.may.read(pagename) and request.user.may.write(pagename)):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
857 return _('You are not allowed to unzip attachments of this page.')
3700
9426f34f07b9 AttachFile: refactored unzip subaction code:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3603
diff changeset
858
116
de898247fbff limit file count when unzipping an attachment
Thomas Waldmann <tw@waldmann-edv.de>
parents: 113
diff changeset
859 if not filename:
de898247fbff limit file count when unzipping an attachment
Thomas Waldmann <tw@waldmann-edv.de>
parents: 113
diff changeset
860 return # error msg already sent in _access_file
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
861
3712
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
862 try:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
863 if not zipfile.is_zipfile(fpath):
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
864 return _('The file %(filename)s is not a .zip file.') % {'filename': filename}
3700
9426f34f07b9 AttachFile: refactored unzip subaction code:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3603
diff changeset
865
3712
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
866 # determine how which attachment names we have and how much space each is occupying
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
867 curr_fsizes = dict([(f, size(request, pagename, f)) for f in _get_files(request, pagename)])
3700
9426f34f07b9 AttachFile: refactored unzip subaction code:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3603
diff changeset
868
3712
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
869 # Checks for the existance of one common prefix path shared among
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
870 # all files in the zip file. If this is the case, remove the common prefix.
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
871 # We also prepare a dict of the new filenames->filesizes.
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
872 zip_path_sep = '/' # we assume '/' is as zip standard suggests
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
873 fname_index = None
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
874 mapping = []
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
875 new_fsizes = {}
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
876 zf = zipfile.ZipFile(fpath)
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
877 for zi in zf.infolist():
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
878 name = zi.filename
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
879 if not name.endswith(zip_path_sep): # a file (not a directory)
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
880 if fname_index is None:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
881 fname_index = name.rfind(zip_path_sep) + 1
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
882 path = name[:fname_index]
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
883 if (name.rfind(zip_path_sep) + 1 != fname_index # different prefix len
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
884 or
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
885 name[:fname_index] != path): # same len, but still different
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
886 mapping = [] # zip is not acceptable
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
887 break
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
888 if zi.file_size >= request.cfg.unzip_single_file_size: # file too big
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
889 mapping = [] # zip is not acceptable
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
890 break
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
891 finalname = name[fname_index:] # remove common path prefix
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
892 finalname = finalname.decode(config.charset, 'replace') # replaces trash with \uFFFD char
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
893 mapping.append((name, finalname))
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
894 new_fsizes[finalname] = zi.file_size
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
895
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
896 # now we either have an empty mapping (if the zip is not acceptable),
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
897 # an identity mapping (no subdirs in zip, just all flat), or
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
898 # a mapping (origname, finalname) where origname is the zip member filename
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
899 # (including some prefix path) and finalname is a simple filename.
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
900
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
901 # calculate resulting total file size / count after unzipping:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
902 if overwrite:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
903 curr_fsizes.update(new_fsizes)
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
904 total = curr_fsizes
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
905 else:
3712
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
906 new_fsizes.update(curr_fsizes)
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
907 total = new_fsizes
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
908 total_count = len(total)
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
909 total_size = sum(total.values())
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
910
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
911 if not mapping:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
912 msg = _("Attachment '%(filename)s' not unzipped because some files in the zip "
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
913 "are either not in the same directory or exceeded the single file size limit (%(maxsize_file)d kB)."
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
914 ) % {'filename': filename,
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
915 'maxsize_file': request.cfg.unzip_single_file_size / 1000, }
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
916 elif total_size > request.cfg.unzip_attachments_space:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
917 msg = _("Attachment '%(filename)s' not unzipped because it would have exceeded "
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
918 "the per page attachment storage size limit (%(size)d kB).") % {
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
919 'filename': filename,
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
920 'size': request.cfg.unzip_attachments_space / 1000, }
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
921 elif total_count > request.cfg.unzip_attachments_count:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
922 msg = _("Attachment '%(filename)s' not unzipped because it would have exceeded "
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
923 "the per page attachment count limit (%(count)d).") % {
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
924 'filename': filename,
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
925 'count': request.cfg.unzip_attachments_count, }
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
926 else:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
927 not_overwritten = []
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
928 for origname, finalname in mapping:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
929 try:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
930 # Note: reads complete zip member file into memory. ZipFile does not offer block-wise reading:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
931 add_attachment(request, pagename, finalname, zf.read(origname), overwrite)
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
932 except AttachmentAlreadyExists:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
933 not_overwritten.append(finalname)
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
934 if not_overwritten:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
935 msg = _("Attachment '%(filename)s' partially unzipped (did not overwrite: %(filelist)s).") % {
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
936 'filename': filename,
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
937 'filelist': ', '.join(not_overwritten), }
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
938 else:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
939 msg = _("Attachment '%(filename)s' unzipped.") % {'filename': filename}
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
940 except RuntimeError, err:
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
941 # We don't want to crash with a traceback here (an exception
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
942 # here could be caused by an uploaded defective zip file - and
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
943 # if we crash here, the user does not get a UI to remove the
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
944 # defective zip file again).
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
945 # RuntimeError is raised by zipfile stdlib module in case of
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
946 # problems (like inconsistent slash and backslash usage in the
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
947 # archive).
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
948 logging.exception("An exception within zip file attachment handling occurred:")
b6dcdf55795e AttachFile action: catch RuntimeError when someone tries to unzip a defective archive
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3708
diff changeset
949 msg = _("A severe error occurred:") + ' ' + str(err)
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
950
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
951 upload_form(pagename, request, msg=wikiutil.escape(msg))
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
952
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
953
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
954 def send_viewfile(pagename, request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
955 _ = request.getText
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
956 fmt = request.html_formatter
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
957
3169
a654cf294f4e AttachFile: fix _access_file return parameter count
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3164
diff changeset
958 pagename, filename, fpath = _access_file(pagename, request)
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
959 if not filename:
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
960 return
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
961
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
962 request.write('<h2>' + _("Attachment '%(filename)s'") % {'filename': filename} + '</h2>')
2736
2ffae6c847fd change the default link target for attachments to 'do=view', add a download link at top of the view page
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2733
diff changeset
963 # show a download link above the content
2ffae6c847fd change the default link target for attachments to 'do=view', add a download link at top of the view page
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2733
diff changeset
964 label = _('Download')
3293
65adbf84a0fb AttachFile: Make the download link more prominent
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 3271
diff changeset
965 link = (fmt.url(1, getAttachUrl(pagename, filename, request, do='get'), css_class="download") +
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
966 fmt.text(label) +
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
967 fmt.url(0))
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
968 request.write('%s<br><br>' % link)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
969
801
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
970 mt = wikiutil.MimeType(filename=filename)
3307
f4212fb5ecb0 EmbedObject: fixed bug for image mimetype and configured when to call EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3293
diff changeset
971
3338
5f660b3c1cd7 moved browser_supported_images to config and exchanged it
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3307
diff changeset
972 # destinguishs if browser need a plugin in place
5f660b3c1cd7 moved browser_supported_images to config and exchanged it
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3307
diff changeset
973 if mt.major == 'image' and mt.minor in config.browser_supported_images:
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
974 request.write('<img src="%s" alt="%s">' % (
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
975 getAttachUrl(pagename, filename, request, escaped=1),
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
976 wikiutil.escape(filename, 1)))
801
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
977 return
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
978 elif mt.major == 'text':
1989
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
979 ext = os.path.splitext(filename)[1]
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
980 Parser = wikiutil.getParserForExtension(request.cfg, ext)
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
981 if Parser is not None:
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
982 try:
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
983 content = file(fpath, 'r').read()
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
984 content = wikiutil.decodeUnknownInput(content)
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
985 colorizer = Parser(content, request, filename=filename)
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
986 colorizer.format(request.formatter)
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
987 return
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
988 except IOError:
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
989 pass
6d6379c9a1c9 AttachFile.send_viewfile: uses now colorizing parsers on view for mimetype text
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1988
diff changeset
990
1833
af0feb0e3c7b AttachFile: more usage of the formatter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
991 request.write(request.formatter.preformatted(1))
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2210
diff changeset
992 # If we have text but no colorizing parser we try to decode file contents.
801
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
993 content = open(fpath, 'r').read()
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
994 content = wikiutil.decodeUnknownInput(content)
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
995 content = wikiutil.escape(content)
1833
af0feb0e3c7b AttachFile: more usage of the formatter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
996 request.write(request.formatter.text(content))
af0feb0e3c7b AttachFile: more usage of the formatter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
997 request.write(request.formatter.preformatted(0))
801
1f8976e01c3a fix wrong import, make more use of MimeType class
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 799
diff changeset
998 return
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
999
3706
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1000 try:
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1001 package = packages.ZipPackage(request, fpath)
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1002 if package.isPackage():
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1003 request.write("<pre><b>%s</b>\n%s</pre>" % (_("Package script:"), wikiutil.escape(package.getScript())))
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1004 return
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
1005
3706
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1006 if zipfile.is_zipfile(fpath) and mt.minor == 'zip':
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1007 zf = zipfile.ZipFile(fpath, mode='r')
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1008 request.write("<pre>%-46s %19s %12s\n" % (_("File Name"), _("Modified")+" "*5, _("Size")))
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1009 for zinfo in zf.filelist:
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1010 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1011 request.write(wikiutil.escape("%-46s %s %12d\n" % (zinfo.filename, date, zinfo.file_size)))
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1012 request.write("</pre>")
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1013 return
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1014 except RuntimeError:
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1015 # We don't want to crash with a traceback here (an exception
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1016 # here could be caused by an uploaded defective zip file - and
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1017 # if we crash here, the user does not get a UI to remove the
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1018 # defective zip file again).
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1019 # RuntimeError is raised by zipfile stdlib module in case of
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1020 # problems (like inconsistent slash and backslash usage in the
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1021 # archive).
f0532fa90c6d AttachFile action: catch runtime error raised by zipfile when trying to view a defective zip
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3705
diff changeset
1022 logging.exception("An exception within zip file attachment handling occurred:")
80
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
1023 return
99f0d19d0285 Integrated MoinMoin:PackageInstaller and zip support.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 33
diff changeset
1024
3266
f62792cb2d24 macro.EmbedObject: adjusted to changes of argument parser, escaped output.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3253
diff changeset
1025 from MoinMoin import macro
f62792cb2d24 macro.EmbedObject: adjusted to changes of argument parser, escaped output.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3253
diff changeset
1026 from MoinMoin.parser.text import Parser
f62792cb2d24 macro.EmbedObject: adjusted to changes of argument parser, escaped output.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3253
diff changeset
1027
1952
8ab85e3711dc AttachFile:send_viewfile extended for mimetypes presentable by EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1920
diff changeset
1028 macro.request = request
8ab85e3711dc AttachFile:send_viewfile extended for mimetypes presentable by EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1920
diff changeset
1029 macro.formatter = request.html_formatter
3266
f62792cb2d24 macro.EmbedObject: adjusted to changes of argument parser, escaped output.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3253
diff changeset
1030 p = Parser("##\n", request)
f62792cb2d24 macro.EmbedObject: adjusted to changes of argument parser, escaped output.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3253
diff changeset
1031 m = macro.Macro(p)
1952
8ab85e3711dc AttachFile:send_viewfile extended for mimetypes presentable by EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1920
diff changeset
1032
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2210
diff changeset
1033 # use EmbedObject to view valid mime types
1967
3bc30cdc4555 AttachFile, EmbedObject: code optimisation to use wikiutil.MimeType and refactoring of EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1952
diff changeset
1034 if mt is None:
1952
8ab85e3711dc AttachFile:send_viewfile extended for mimetypes presentable by EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1920
diff changeset
1035 request.write('<p>' + _("Unknown file type, cannot display this attachment inline.") + '</p>')
3149
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
1036 link = (fmt.url(1, getAttachUrl(pagename, filename, request)) +
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
1037 fmt.text(filename) +
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
1038 fmt.url(0))
6749e003d7e2 remove attachments direct serving (cfg.attachments), refactor AttachFile to use formatter for link generation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
1039 request.write('For using an external program follow this link %s' % link)
1952
8ab85e3711dc AttachFile:send_viewfile extended for mimetypes presentable by EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1920
diff changeset
1040 return
3266
f62792cb2d24 macro.EmbedObject: adjusted to changes of argument parser, escaped output.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 3253
diff changeset
1041 request.write(m.execute('EmbedObject', u'target=%s, pagename=%s' % (filename, pagename)))
1952
8ab85e3711dc AttachFile:send_viewfile extended for mimetypes presentable by EmbedObject
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 1920
diff changeset
1042 return
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1043
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1044
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
1045 def _do_view(pagename, request):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1046 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1047
3152
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
1048 orig_pagename = pagename
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
1049 pagename, filename, fpath = _access_file(pagename, request)
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
1050 if not request.user.may.read(pagename):
719c5bf80150 AttachFile: move ACL processing to handlers, simplify
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3151
diff changeset
1051 return _('You are not allowed to view attachments of this page.')
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
1052 if not filename:
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
1053 return
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1054
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1055 # send header & title
1068
ecece5db5288 use emit_http_headers
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1006
diff changeset
1056 request.emit_http_headers()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1057 # Use user interface language for this generated page
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1058 request.setContentLanguage(request.lang)
3122
a1322262398a refactored _() getText calls to match new api
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3084
diff changeset
1059 title = _('attachment:%(filename)s of %(pagename)s') % {
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1060 'filename': filename, 'pagename': pagename}
616
3b08d9413589 move send_title/footer from wikiutil to theme.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 520
diff changeset
1061 request.theme.send_title(title, pagename=pagename)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1062
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1063 # send body
1833
af0feb0e3c7b AttachFile: more usage of the formatter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
1064 request.write(request.formatter.startContent())
3169
a654cf294f4e AttachFile: fix _access_file return parameter count
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3164
diff changeset
1065 send_viewfile(orig_pagename, request)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1066 send_uploadform(pagename, request)
1833
af0feb0e3c7b AttachFile: more usage of the formatter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
1067 request.write(request.formatter.endContent())
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1068
616
3b08d9413589 move send_title/footer from wikiutil to theme.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 520
diff changeset
1069 request.theme.send_footer(pagename)
617
cf420addd95c removed MoinMoinNoFooter at many places, added call to theme.send_closing_html() where needed
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 616
diff changeset
1070 request.theme.send_closing_html()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1071
3151
863d90c05507 AttachFile: more refactorings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3150
diff changeset
1072
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1073 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1074 ### File attachment administration
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1075 #############################################################################
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1076
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1077 def do_admin_browser(request):
3150
8ae94675b9f9 AttachFile: some cosmetical source changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3149
diff changeset
1078 """ Browser for SystemAdmin macro. """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1079 from MoinMoin.util.dataset import TupleDataset, Column
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1080 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1081
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1082 data = TupleDataset()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1083 data.columns = [
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1084 Column('page', label=('Page')),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1085 Column('file', label=('Filename')),
889
ad62767ffd0c pep8 style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 869
diff changeset
1086 Column('size', label=_('Size'), align='right'),
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1087 ]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1088
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1089 # iterate over pages that might have attachments
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1090 pages = request.rootpage.getPageList()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1091 for pagename in pages:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1092 # check for attachments directory
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1093 page_dir = getAttachDir(request, pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1094 if os.path.isdir(page_dir):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1095 # iterate over files of the page
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1096 files = os.listdir(page_dir)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1097 for filename in files:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1098 filepath = os.path.join(page_dir, filename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1099 data.addRow((
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1100 Page(request, pagename).link_to(request, querystr="action=AttachFile"),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1101 wikiutil.escape(filename.decode(config.charset)),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1102 os.path.getsize(filepath),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1103 ))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1104
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1105 if data:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1106 from MoinMoin.widget.browser import DataBrowserWidget
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1107
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1108 browser = DataBrowserWidget(request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1109 browser.setData(data)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1110 return browser.toHTML()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1111
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1112 return ''
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1113