summaryrefslogtreecommitdiff
path: root/thumbnail/macro/ThumbnailGallery.py
blob: 038c3bc7e802e069e8ca8e077ac6108dbe568ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import re
from MoinMoin import wikiutil
from MoinMoin.action import AttachFile


def macro_ThumbnailGallery(macro, page=None, regex=None, height=200, width=None, link=True):
    _ = macro.request.getText
    formatter = macro.formatter
    pagename = page or macro.formatter.page.page_name
    output = []

    if not regex:
        regex = re.compile(".*\.(jpg|jpeg|png|gif)$")
    else:
        regex = re.compile(regex)

    if not macro.request.user.may.read(pagename):
        return _('You are not allowed to view attachments of this page.')

    for attachment in AttachFile._get_files(macro.request, pagename):
        if regex.match(attachment) is None:
            continue

        pagename, filename = AttachFile.absoluteName(attachment, pagename)
        fname = wikiutil.taintfilename(filename)

        if width:
            size, dimension = width, "w"
        else:
            size, dimension = height, "h"

        if AttachFile.exists(macro.request, pagename, fname):
            url = AttachFile.getAttachUrl(pagename, fname, macro.request, do='get')

            output.extend([
                macro.formatter.url(True, url) if link else "",
                macro.formatter.image(
                    macro.request.href(pagename, {
                         "target": fname,
                         "action": "Thumbnail",
                         "do": "tc:{},{}".format(size, dimension)
                    })),
                macro.formatter.url(False) if link else "",
                " ",
            ])

    return "".join(output)