-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgallery-polymer.js
92 lines (76 loc) · 2.73 KB
/
gallery-polymer.js
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
/*
* XD-MVC -- A framework for cross-device applications
* Copyright (C) 2014-2015 Maria Husmann. All rights reserved.
*
* XD-MVC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XD-MVC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XD-MVC. If not, see <http://www.gnu.org/licenses/>.
*
* See the README and LICENSE files for further information.
*
*/
var XDmvcServer = require('xdmvcserver/xdmvcserver.js');
var path = require('path');
var xdmvc = new XDmvcServer();
var connect = require('connect'),
http = require('http'),
bodyParser = require('body-parser'),
serveStatic = require('serve-static');
var url = require('url');
var app = connect().use(bodyParser.json()).use(serveStatic(path.join(__dirname, 'public')));
app.use("/gallery", handleGallery);
var server = http.createServer(app);
var fs = require('fs');
var basePath = path.join('public', 'images');
var albums = [];
function createModel() {
var files = fs.readdirSync(basePath),
thumbs,
album;
files.forEach(function (file) {
thumbs = fs.readdirSync(path.join(basePath, file, 'thumbs'));
album = {title: file, url: "images/" + file + "/thumbs/" + thumbs[0]};
albums.push(album);
});
albums.forEach(function(album){
var title = album.title;
var files = fs.readdirSync(path.join(basePath, title, 'thumbs'));
var largePath = "images/" + title + "/large/";
var thumbsPath = "images/" + title + "/thumbs/";
// Read images from directory
album.thumbs = files.map(function (f) {
return thumbsPath + f;
});
album.large = files.map(function (f) {
return largePath + f;
});
});
console.log(albums);
}
function handleGallery(req, res, next){
var parsedUrl = url.parse(req.url, true)
var query = parsedUrl.query;
var path = parsedUrl.pathname.split("/");
res.setHeader('Access-Control-Allow-Origin', '*')
if (path.length === 2 && path[1].length ===0){
res.writeHead(200, {'Content-Type': 'text/json' });
res.write(JSON.stringify(albums));
res.end('\n');
}
}
createModel();
xdmvc.start(9000, 3001, 9001);
xdmvc.on("objectChanged", function(msg){
console.log(msg);
albums[msg.data.id].url = msg.data.cover;
});
server.listen(8082);