-
Notifications
You must be signed in to change notification settings - Fork 21
/
view.html
241 lines (208 loc) · 7.05 KB
/
view.html
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Album</title>
<script>
/*
* DEFAULTS
*
* Override the automatically detected parameters here:
*/
ENDPOINT = undefined; // e.g. 's3.amazonaws.com'
BUCKET = undefined; // e.g. 'butketname'
SSL_ENABLED = undefined; // e.g. true
FORCE_PATH_STYLE = undefined; // e.g. false
PREFIX = undefined; // e.g. 'path/to/albums/root/'
</script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">
<style>
.thumbnail {
position: relative;
overflow: hidden;
padding: 0;
height: 150px;
width: max-content;
margin-right: auto;
margin-left: auto;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
.thumbnail .caption {
position: absolute;
white-space: pre;
overflow: hidden;
width: 100%;
bottom: 0;
padding: 5px;
color: white;
background: rgba(0, 0, 0, 0.3);
}
.thumbnail img, .fancybox-image {
image-orientation: from-image;
}
.fancybox-image {
width: initial;
margin: auto;
}
</style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header"></h1>
</div>
<div class="row" id="albumThumbs">
</div>
</div>
<div id="messages"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
<script>
/*
* Helpers
*/
function stripExt(path) {
return path.slice(0, path.lastIndexOf('.'));
}
function basename(path) {
return rmTrailingChar(path, '/').split('/').slice(-1)[0];
}
function rmTrailingChar(str, c) {
if (str.slice(-1)[0] === c) return str.slice(0, -1);
else return str;
}
/*
* Bucket utils
*/
var config = {
bucket: undefined,
endpoint: undefined,
sslEnabled: undefined,
forcePathStyle: undefined,
prefix: undefined
};
function detectConfig() {
var host = window.location.hostname;
var path = window.location.pathname.split('/').slice(1);
var names, me;
config.forcePathStyle = true;
if (host.endsWith('s3.amazonaws.com') &&
(names = host.split('.')).length === 4) {
config.bucket = names[0];
config.endpoint = names.slice(1).join('.');
config.forcePathStyle = false;
me = path;
}
else {
config.bucket = path[0];
me = path.slice(1);
config.endpoint = host;
}
if (me.join('').endsWith('.html'))
config.prefix = me.slice(0, -1).join('/');
else
config.prefix = me.join('/');
config.sslEnabled = SSL_ENABLED || window.location.protocol.startsWith('https');
config.bucket = BUCKET || config.bucket;
config.prefix = PREFIX || config.prefix;
config.endpoint = ENDPOINT || config.endpoint;
config.forcePathStyle = FORCE_PATH_STYLE || config.forcePathStyle;
}
function bucketBaseUrl() {
var proto = config.sslEnabled ? 'https://' : 'http://';
var url = proto;
if (config.forcePathStyle) url += config.endpoint + '/' + config.bucket;
else url += config.bucket + '.' + config.endpoint;
return url;
}
function ls(dir, cb) {
var url = bucketBaseUrl() + '?delimiter=/&prefix=' + dir;
$.get(url).done(function (data) {
if (!$(data).find('ListBucketResult').length) return cb('Malformed response');
var files = $.map($(data).find('Contents Key'), function (k) {
return basename(k.innerHTML);
});
var dirs = $.map($(data).find('CommonPrefixes Prefix'), function (k) {
return basename(k.innerHTML);
});
cb(null, files, dirs);
}).fail(function (x, y, err) {
if (cb) return cb(err);
throw err;
});
}
function albumPhotoPath(album) {
return rmTrailingChar(config.prefix, '/') + '/albums/' + encodeURIComponent(album) + '/photos/';
}
function albumThumbPath(album) {
return rmTrailingChar(config.prefix, '/') + '/albums/' + encodeURIComponent(album) + '/thumbs/';
}
function getPhotos(album, cb) {
if (!album || album === '') return cb('Bad album name');
var photoBase = albumPhotoPath(album), thumbBase = albumThumbPath(album);
ls(photoBase, function (err, files) {
if (err) { if (cb) return cb(err); throw err; }
var baseUrl = bucketBaseUrl() + '/';
cb(null, files.map(function (f) {
return {
title: f,
src: baseUrl + photoBase + f,
thumb: baseUrl + thumbBase + stripExt(basename(f)) + '.jpg'
};
}));
});
}
function albumNameFromHash() {
return decodeURIComponent(location.hash.slice(1).split('/')[0]);
}
var albumName;
function loadAlbum() {
albumName = albumNameFromHash();
if (location.hash.indexOf('/') === -1) location.hash += '/';
$('.page-header').text(albumName && albumName !== '' ? albumName : "''");
getPhotos(albumName, function (err, photos) {
if (err) {
$('#messages').html('<div class="alert alert-danger" role="alert"><b>That\'s a 404!</b> Or, in English: this album does not exist.</div>');
document.title = '404 Not Found: "' + albumName + '"';
return;
}
document.title = albumName;
$('#albumThumbs').empty();
photos.forEach(function (p) {
$('#albumThumbs').append([
'<div class="col-lg-3 col-md-4 col-xs-6">',
`<a class="thumbnail fancybox" rel="group" title="${p.title}" href="${p.src}">`,
`<img src="${p.thumb}" height="150" alt="" />`,
'<div class="caption">', p.title, '</div>',
'</a>',
'</div>',
].join(''));
});
$(".fancybox").fancybox();
});
}
$(window).on('hashchange', function () {
if (albumNameFromHash() !== albumName)
loadAlbum();
});
$(document).ready(function () {
detectConfig();
loadAlbum();
});
</script>
</body>
</html>