-
Notifications
You must be signed in to change notification settings - Fork 134
/
listing.xsl
143 lines (136 loc) · 6.05 KB
/
listing.xsl
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?xml version="1.0"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:param name="rootPath" />
<xsl:template match="/">
<xsl:choose>
<xsl:when test="//*[local-name()='Contents'] or //*[local-name()='CommonPrefixes']">
<xsl:apply-templates select="*[local-name()='ListBucketResult']" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="no_contents"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- When FOUR_O_FOUR_ON_EMPTY_BUCKET is disabled (the default setting),
the following template will be executed when the bucket is empty. -->
<xsl:template name="no_contents">
<html>
<head><title>No Files Available for Listing</title></head>
<body>
<h1>No Files Available for Listing</h1>
</body>
</html>
</xsl:template>
<xsl:template match="*[local-name()='ListBucketResult']">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<xsl:variable name="globalPrefix"
select="*[local-name()='Prefix']/text()"/>
<html>
<head>
<title><xsl:value-of select="$globalPrefix"/>
</title>
</head>
<body>
<h1>Index of /<xsl:value-of select="concat($rootPath, $globalPrefix)"/></h1>
<hr/>
<table id="list">
<thead>
<tr>
<th style="text-align: left; width:55%">Filename
</th>
<th style="text-align: left; width:20%">File Size
</th>
<th style="text-align: left; width:25%">Date</th>
</tr>
</thead>
<tbody>
<xsl:if test="string-length($globalPrefix) > 0">
<tr>
<td>
<a href="../">..</a>
</td>
</tr>
</xsl:if>
<xsl:apply-templates
select="*[local-name()='CommonPrefixes']">
<xsl:with-param name="globalPrefix"
select="$globalPrefix"/>
</xsl:apply-templates>
<xsl:apply-templates
select="*[local-name()='Contents']">
<xsl:with-param name="globalPrefix"
select="$globalPrefix"/>
</xsl:apply-templates>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="*[local-name()='CommonPrefixes']">
<xsl:param name="globalPrefix"/>
<xsl:apply-templates select=".//*[local-name()='Prefix']">
<xsl:with-param name="globalPrefix" select="$globalPrefix"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*[local-name()='Prefix']">
<xsl:param name="globalPrefix"/>
<xsl:if test="not(text()=$globalPrefix)">
<xsl:variable name="dirName"
select="substring-after(text(), $globalPrefix)"/>
<tr>
<td>
<a><xsl:attribute name="href">/<xsl:call-template name="encode-uri"><xsl:with-param name="uri" select="text()"/></xsl:call-template>/</xsl:attribute>
<xsl:value-of select="$dirName"/>
</a>
</td>
<td/>
<td/>
</tr>
</xsl:if>
</xsl:template>
<xsl:template match="*[local-name()='Contents']">
<xsl:param name="globalPrefix"/>
<xsl:variable name="key" select="*[local-name()='Key']/text()"/>
<xsl:if test="not($key=$globalPrefix)">
<xsl:variable name="fileName"
select="substring-after($key, $globalPrefix)"/>
<xsl:variable name="date"
select="*[local-name()='LastModified']/text()"/>
<xsl:variable name="size" select="*[local-name()='Size']/text()"/>
<tr>
<td>
<a>
<xsl:attribute name="href">/<xsl:call-template name="encode-uri"><xsl:with-param name="uri" select="$key"/></xsl:call-template></xsl:attribute>
<xsl:value-of select="$fileName"/>
</a>
</td>
<td>
<xsl:value-of select="$size"/>
</td>
<td>
<xsl:value-of select="$date"/>
</td>
</tr>
</xsl:if>
</xsl:template>
<!-- This template escapes the URI such that symbols or unicode characters are
encoded so that they form a valid link that NGINX can parse -->
<xsl:template name="encode-uri">
<xsl:param name="uri"/>
<xsl:variable name="prefixed_uri" select="concat($rootPath, $uri)" />
<xsl:for-each select="str:split($prefixed_uri, '/')">
<xsl:variable name="encoded" select="str:encode-uri(., 'true', 'UTF-8')" />
<xsl:variable name="more-encoded" select="
str:replace(
str:replace(
str:replace(
str:replace(
str:replace($encoded, '@', '%40'), '(', '%28'),
')', '%29'),
'!', '%21'),
'*', '%2A')" />
<xsl:value-of select="$more-encoded" /><xsl:if test="position() != last()">/</xsl:if></xsl:for-each>
</xsl:template>
</xsl:stylesheet>