Browse Source

configuration refactor, add user tweaking, fix bug

pull/2/head
asiekierka 6 years ago
parent
commit
8b17dee956
9 changed files with 97 additions and 78 deletions
  1. 26
    23
      app.js
  2. 51
    30
      config-default.json
  3. 6
    6
      image.js
  4. 3
    3
      templates/header.html
  5. 3
    3
      templates/images-li.html
  6. 2
    2
      templates/images.html
  7. 4
    9
      templates/main.html
  8. 1
    1
      templates/view.html
  9. 1
    1
      userdb.js

+ 26
- 23
app.js View File

@@ -27,7 +27,7 @@ var app = express();
27 27
 
28 28
 var defaultConfig = require('./config-default.json')
29 29
   , defaultImage = { author: "Unknown", source: "/", uploader: "Computer"}
30
-  , defaultSiteConfig = { subtitle: null, title: "Website", htmlTitle: null, noAjaxLoad: false, mobile: false}
30
+  , defaultSiteConfig = { subtitle: null, mobile: false}
31 31
   , templateFunctions = {
32 32
     template: function(name) {
33 33
       return makeRawTemplate(name,this,true);
@@ -54,6 +54,7 @@ if(!_(config).contains("htmlTitle")) {
54 54
   else config.htmlTitle = config.title;
55 55
 }
56 56
 
57
+imageHandler.setConfig(config.images);
57 58
 imageHandler.express(express,app);
58 59
 
59 60
 // Image Adding
@@ -159,7 +160,7 @@ function finishUpload(res,fn,thfn,path,metadata, next) {
159 160
   });
160 161
 }
161 162
 app.get("/upload", restrict, parse, function(req,res,next) {
162
-  res.send(makeTemplate("upload",{req: req, username: req.session.user, useZepto: false},req.params[0]));
163
+  res.send(makeTemplate("upload",{req: req, username: req.session.user, framework: "jQuery"}, req.params[0]));
163 164
 });
164 165
 
165 166
 // Searching
@@ -193,13 +194,13 @@ function handleSearch(req, res, query) {
193 194
           if(stack.length > 1) { error(req,res,"Something quite bad happened! "+JSON.stringify(stack)); return; }
194 195
           var result = stack.pop();
195 196
           cacheDB.set("search:"+query,result,60,null);
196
-          listImages(req,res,result,req.query,{noAjaxLoad: true, isSearch: true, subtitle: Math.min(1000,result.length)+" results found."},1000);
197
+          listImages(req,res,result,req.query,{ajaxFetching: false, isSearch: true, subtitle: Math.min(1000,result.length)+" results found."},1000);
197 198
         });
198 199
       });
199 200
     else { // Found cached!
200 201
       console.log("Loading from cache!");
201 202
       cacheDB.get("search:"+query,function(err,result) {
202
-        listImages(req,res,result,req.query,{noAjaxLoad: true, isSearch: true, subtitle: Math.min(1000,result.length)+" results found."},1000);
203
+        listImages(req,res,result,req.query,{ajaxFetching: false, isSearch: true, subtitle: Math.min(1000,result.length)+" results found."},1000);
203 204
       });
204 205
     }
205 206
   });
@@ -280,6 +281,7 @@ function getImagesTagged(tags,next) {
280 281
 }
281 282
 app.get("/random*", parse, function(req,res) {
282 283
   imageDB.images(function(images) {
284
+    if(images.length == 0) { error(req, res, "No images!", 404); return; }
283 285
     imageDB.get(images[_.random(images.length-1)], function(randomImage) {
284 286
       if(req.query["mode"] == "json") {
285 287
         res.json(randomImage);
@@ -296,7 +298,7 @@ function listImages(req,res,images1,options,defConfig,maxVal) {
296 298
   var images1a, data;
297 299
   var maxValue = options["length"] || maxVal || config.pageSize;
298 300
   if(maxValue > config.maxPageSize) maxValue = config.maxPageSize;
299
-  getImagesTagged(config.hiddenTags,function(hiddenImages) {
301
+  getImagesTagged(config.tags.hidden,function(hiddenImages) {
300 302
     if(!_(req.cookies.showHidden).isUndefined() || req.query["hidden"] == true) images1a = images1;
301 303
     else images1a = _.difference(images1,hiddenImages);
302 304
     imageDB.range(images1a,start,maxValue,function(images2) {
@@ -304,7 +306,7 @@ function listImages(req,res,images1,options,defConfig,maxVal) {
304 306
       console.log(options);
305 307
       if(options["mobile"] == 'true') conf.mobile = true;
306 308
       if(mode=="json") {
307
-        if(config.allowJson == false) { error(req, res, "JSON not allowed!", 403); return; }
309
+        if(config.api.json == false) { error(req, res, "JSON not allowed!", 403); return; }
308 310
         res.json({position: start, length: images2.length, results: images2});
309 311
       } else {
310 312
         if(_(options).has("subtitle2"))
@@ -357,27 +359,26 @@ if(config.salt == defaultConfig.salt || config.salt.length < 16) {
357 359
 }
358 360
 
359 361
 console.log("Connecting to database...")
360
-var client = redis.createClient();
362
+var client = redis.createClient(config.database.port, config.database.host);
363
+if(config.database.password != "")
364
+  client.auth(config.database.password);
361 365
 imageDB.connect(client);
362 366
 cacheDB.connect(client);
363 367
 userDB.connect(client, config.salt);
364
-imageHandler.setMode(config.optimize);
365 368
 
366
-userDB.exists("admin", function(err, exists) {
367
-  if(!err && !exists) {
368
-    console.log("Creating default users...");
369
-    _.each(config.defaultUsers, function(value,key) {
370
-      console.log("[+] "+key);
371
-      userDB.addUser({user: key, pass: userDB.hash(value), nick: key, type: "admin"});
372
-    });
373
-  }
374
-  else if(err) { console.log("Error checking userDB! " + err.message); }
369
+console.log("(Re)Creating users...");
370
+_.each(config.users, function(value,key) {
371
+  console.log("[+] "+key);
372
+  var user = {user: key, nick: key, type: "admin"};
373
+  if(_.isString(value)) user.pass = userDB.hash(value);
374
+  else { user.pass = userDB.hash(value.pass); user.nick = value.nick || user.nick; user.type = value.type || user.type; }
375
+  userDB.addUser(user, function() {});
375 376
 });
376 377
 
377 378
 function start() {
378 379
   imageDB.updateDatabase(function() {
379
-    app.listen(config.port);
380
-    console.log("Working on port " + config.port);
380
+    app.listen(config.server.port);
381
+    console.log("Working on port " + config.server.port);
381 382
   });
382 383
 }
383 384
 
@@ -421,7 +422,9 @@ if(argv.t || argv.thumb) {
421 422
   });
422 423
 }
423 424
 
424
-process.on("uncaughtException", function(err) {
425
-  console.log("Uncaught exception! Please report to author");
426
-  console.log(err);
427
-});
425
+if(!(argv.d || argv.debug)) {
426
+  process.on("uncaughtException", function(err) {
427
+    console.log("Uncaught exception! Please report to author");
428
+    console.log(err);
429
+  });
430
+}

+ 51
- 30
config-default.json View File

@@ -1,34 +1,55 @@
1
-{ "port": 8080, "pageSize": 15, "maxPageSize": 500, "title": "Booru", "htmlTitle": "Booru",
2
-  "noAjaxURL": false,
3
-  "allowJson": true,
4
-  "useLazyLoad": true,
5
-  "socialButtons": true,
6
-  "showClouds": true,
7
-  "showRandom": true,
1
+{
2
+  "server": {
3
+    "port": 8080
4
+  },
5
+  "database": {
6
+    "host": "localhost",
7
+    "port": 6379,
8
+    "password": ""
9
+  },
8 10
   "salt": "",
9
-  "defaultUsers": {
10
-    "admin": "admin"
11
+  "pageSize": 15,
12
+  "maxPageSize": 500,
13
+  "title": "New Booru",
14
+  "htmlTitle": "New Booru",
15
+  "users": {
16
+    "admin": {"pass": "admin", "nick": "admin", "type": "admin" }
17
+  },
18
+  "api": {
19
+    "json": true
20
+  },
21
+  "frameworks": {
22
+    "zeptoJS": "/static/js/zepto.min.js",
23
+    "jQuery": "/static/js/jquery-1.9.1.min.js"
24
+  },
25
+  "framework": "jQuery",
26
+  "ajaxFetching": true,
27
+  "lazyLoading": true,
28
+  "ui": {
29
+    "socialButtons": true,
30
+    "showClouds": true,
31
+    "showRandom": true
32
+  },
33
+  "images": {
34
+    "optimizationEngines": [
35
+      {"name": "pngout", "format": "png", "notes": "Likely the best option for PNG",
36
+       "enabled": false,
37
+       "path": "/usr/bin/pngout '$FILE'"},
38
+      {"name": "pngcrush", "format": "png",
39
+       "enabled": false,
40
+       "path": "/usr/bin/pngcrush '$FILE' '$FILE'"},
41
+      {"name": "jpegrescan", "format": "jpg", "notes": "Just install Perl and File::Slurp",
42
+       "enabled": false,
43
+       "path": "tools/jpegrescan '$FILE' '$FILE'"},
44
+      {"name": "gifsicle", "format": "gif",
45
+       "enabled": false,
46
+       "path": "/usr/bin/gifsicle --careful '$FILE' -o '$FILE'"}
47
+    ],
48
+    "optimizationThreads": 1,
49
+    "optimize": "all"
11 50
   },
12
-  "optimizationEngines": [
13
-    {"name": "pngout", "format": "png", "notes": "Likely the best option for PNG",
14
-     "enabled": false,
15
-     "path": "/usr/bin/pngout '$FILE'"},
16
-    {"name": "pngcrush", "format": "png",
17
-     "enabled": false,
18
-     "path": "/usr/bin/pngcrush '$FILE' '$FILE'"},
19
-    {"name": "jpegrescan", "format": "jpg", "notes": "Just install Perl and File::Slurp",
20
-     "enabled": false,
21
-     "path": "tools/jpegrescan '$FILE' '$FILE'"},
22
-    {"name": "gifsicle", "format": "gif",
23
-     "enabled": false,
24
-     "path": "/usr/bin/gifsicle --careful '$FILE' -o '$FILE'"}
25
-  ],
26
-  "optimizationThreads": 1,
27
-  "optimize": "all",
28
-  "spoilerTags": ["spoiler"],
29
-  "hiddenTags": ["hidden"],
30
-  "useZepto": false,
31
-  "style": {
32
-    "bgcolor": "#fff"
51
+  "tags": {
52
+    "spoiler": ["spoiler"],
53
+    "hidden": ["hidden"]
33 54
   }
34 55
 }

+ 6
- 6
image.js View File

@@ -2,15 +2,15 @@ var _ = require('underscore')
2 2
   , fs = require('fs')
3 3
   , mkdirp = require('mkdirp')
4 4
   , im = require('imagemagick')
5
-  , config = require('./config.json')
6 5
   , child = require('child_process')
7 6
   , async = require('async')
8 7
   , util = require('./util.js');
9 8
 
10 9
 var thumbW = 300, thumbH = 300;
11 10
 
12
-var optimizeMode = "all";
13
-exports.setMode = function(mode) { optimizeMode = mode; }
11
+var config = {};
12
+
13
+exports.setConfig = function(c) { config = c; }
14 14
 
15 15
 exports.resize = function(src,dest,w,h,cb,grav) {
16 16
   var cnf = { srcPath: src, dstPath: dest,
@@ -35,7 +35,7 @@ var optqueue = async.queue(function (task, callback) {
35 35
 }, config.optimizationThreads || 1);
36 36
 
37 37
 exports.optimize = function(path,data) {
38
-  if(optimizeMode == "none" || !fs.existsSync(path)) return;
38
+  if(config.optimize == "none" || !fs.existsSync(path)) return;
39 39
   var oldfilesize = util.filesize(path);
40 40
   var format = (data.format || util.fileExt(path)).toLowerCase();
41 41
   if(format=="jpeg") format="jpg";
@@ -56,7 +56,7 @@ exports.handle = function(src,destName,w1,h1,grav,options) {
56 56
     if(_.isString(dest2x) && (w>thumbW || h>thumbH))
57 57
       self.resize(src,dest2x,thumbW*2,thumbH*2,function(err) {
58 58
         if(err) throw err;
59
-        if(optimizeMode == "all" || optimizeMode == "thumbs" || optimizeMode == "thumbnails") {
59
+        if(config.optimize == "all" || config.optimize == "thumbnails") {
60 60
           self.optimize(dest,{format: util.fileExt(src)});
61 61
           self.optimize(dest2x,{format: util.fileExt(src)});
62 62
         }
@@ -66,7 +66,7 @@ exports.handle = function(src,destName,w1,h1,grav,options) {
66 66
     else if(_.isFunction(options.callback)) options.callback();
67 67
   };
68 68
   this.resize(src,dest,thumbW,thumbH,t2,grav);
69
-  if(options.optimizeSrc && optimizeMode == "all" && _(src).startsWith("./img")) self.optimize(src,{format: util.fileExt(src)});
69
+  if(options.optimizeSrc && config.optimize == "all" && _(src).startsWith("./img")) self.optimize(src,{format: util.fileExt(src)});
70 70
 }
71 71
 
72 72
 // Create missing directories (just in case)

+ 3
- 3
templates/header.html View File

@@ -3,11 +3,11 @@
3 3
 <a class="normal" href="/"><h1><% if(_.isString(htmlTitle)) { print(htmlTitle); } else { print(title); } %></h1></a>
4 4
 <% if(_.isString(subtitle)){ print('<h4>'+subtitle+'</h4>'); } %>
5 5
 <%
6
-if(showClouds) {
6
+if(ui.showClouds) {
7 7
   print('<a href="/cloud/tag">All tags</a> | <a href="/cloud/author">All authors</a>');
8
-  if(showRandom) { print(' | '); }
8
+  if(ui.showRandom) { print(' | '); }
9 9
 }
10
-if(showRandom) {
10
+if(ui.showRandom) {
11 11
   print('<a href="/random"><b>Random</b></a>');
12 12
 }
13 13
 if(req) {

+ 3
- 3
templates/images-li.html View File

@@ -1,16 +1,16 @@
1 1
 <% _.each(images, function(img) {
2 2
   if(img == null) return;
3 3
   print('<li class="span4"><a href="/image/'+img.id+'/" class="thumbnail thumbnail-300">');
4
-  var imgloc = _.some(spoilerTags,function(val) {
4
+  var imgloc = _.some(tags.spoiler,function(val) {
5 5
     if(_(img.tags).contains(val)) return true;
6 6
   })?'/static/img/spoiler.png':'/img/thumb/'+img.filename;
7 7
   var imgtitle = img.name;
8
-  if(useLazyLoad) {
8
+  if(lazyLoading) {
9 9
     print('<img class="lazy to-scriptize" src="/static/img/white.png" data-original="'+imgloc+'"');
10 10
     print(' title="'+imgtitle+'"><noscript>');
11 11
   }
12 12
   print('<img src="'+imgloc+'" class="to-scriptize" title="'+imgtitle+'">');
13
-  if(useLazyLoad) {
13
+  if(lazyLoading) {
14 14
     print('</noscript>');
15 15
   }
16 16
   print('</a></li>');

+ 2
- 2
templates/images.html View File

@@ -25,7 +25,7 @@
25 25
 <script type="text/javascript">
26 26
 function scriptize() {
27 27
 <%
28
-  if(useLazyLoad) {
28
+  if(lazyLoading) {
29 29
     print("$('img.lazy').show().lazyload();");
30 30
   }
31 31
 %>
@@ -50,7 +50,7 @@ function pageLoad() {
50 50
   var maxpos = <%= maxpos %>;
51 51
   var pageSize = <%= pageSize %>;
52 52
   var isUpdating = false;
53
-  var noAjaxLoad = <%= noAjaxLoad %>;
53
+  var noAjaxLoad = <%= !ajaxFetching %>;
54 54
   var update = function() {
55 55
     if(isUpdating || noAjaxLoad) return;
56 56
     if(position+pageSize < maxpos && $(window).scrollTop() >= ($(document).height() - $(window).height() - 300)) {

+ 4
- 9
templates/main.html View File

@@ -11,18 +11,13 @@ a.social:hover { opacity: 0.7; }
11 11
 .thumbnail { background-color: #FFF; }
12 12
 .thumbnail-300 { width: 300px; height: 300px !important; }
13 13
 h1 { color: #222; }
14
-<% if(useLazyLoad) {
15
-  print(".lazy { display: none; }");
16
-} %>
14
+.lazy { display: none; }
17 15
 <%= skin.css %>
18 16
 </style>
19 17
 </head>
20 18
 <body>
21
-<script src="<%
22
-  if(useZepto) print("/static/js/zepto.min.js");
23
-  else print("/static/js/jquery-1.9.1.min.js");
24
-%>"></script>
25
-<% if(useLazyLoad) {
19
+<script src="<%= frameworks[framework] %>"></script>
20
+<% if(lazyLoading) {
26 21
   print('<script src="/static/js/jquery.lazyload.min.js"></script>');
27 22
 } %>
28 23
 <script src="/static/js/bootstrap.min.js"></script>
@@ -30,7 +25,7 @@ h1 { color: #222; }
30 25
 <div class="container" id="page-inject"><%= page %></div>
31 26
 <script>
32 27
 function fixURLs() {
33
-  <% if(noAjaxURL) { print('return;'); } %>
28
+  <% if(!ajaxFetching) { print('return;'); } %>
34 29
   $("a").each(function(idx) {
35 30
     if(!$(this).hasClass("no-ajax"))
36 31
       $(this).click(changeView);

+ 1
- 1
templates/view.html View File

@@ -15,7 +15,7 @@ Tags: <%
15 15
 Author: <a href="/author/<%= image.author %>/"><%= image.author %></a> (<a class="no-ajax" href="<%= image.source %>">Source</a>)<br>
16 16
 Uploaded by <a href="/uploader/<%= image.uploader %>/"><%= image.uploader %></a> 
17 17
 <% if(req.session.user) print('[<a href="/edit/'+image.id+'/">edit</a>]'); %>
18
-<% if(socialButtons)
18
+<% if(ui.socialButtons)
19 19
      print("<br><br>"+template("social-buttons"));
20 20
 %>
21 21
 </div>

+ 1
- 1
userdb.js View File

@@ -30,7 +30,7 @@ UserDB.addUser = function(data,callback) {
30 30
   else if(data.pass == null) callback(new Error("Invalid password!"));
31 31
   else this.exists(data.user, function(err, does) {
32 32
     if(err) callback(err);
33
-    else if(does) callback(new Error("User exists!"));
33
+    //else if(does) callback(new Error("User exists!")); * We overwrite users for now. HACK! TODO!
34 34
     else async.series([
35 35
       _.bind(client.set,client,"user:"+data.user,JSON.stringify(data)),
36 36
       _.bind(client.sadd,client,"users",data.user)

Loading…
Cancel
Save