Browse Source

refactoring, removing stuff

pull/2/head
asiekierka 6 years ago
parent
commit
d27fa6fd8a
7 changed files with 28 additions and 27 deletions
  1. 13
    16
      app.js
  2. 0
    1
      cachedb.js
  3. 11
    7
      image.js
  4. 0
    1
      imagedb.js
  5. 0
    1
      package.json
  6. 0
    1
      userdb.js
  7. 4
    0
      util.js

+ 13
- 16
app.js View File

@@ -3,12 +3,10 @@ var express = require('express')
3 3
   , mkdirp = require('mkdirp')
4 4
   , _ = require('underscore')
5 5
   , fs = require('fs')
6
-  , JSON = require('JSON2')
7 6
   , imageDB = require('./imagedb.js').ImageDB
8 7
   , userDB = require('./userdb.js').UserDB
9 8
   , cacheDB = require('./cachedb.js').CacheDB
10 9
   , im = require('imagemagick')
11
-  , path = require('path')
12 10
   , qs = require('querystring')
13 11
   , crypto = require('crypto')
14 12
   , argv = require('optimist').argv
@@ -51,17 +49,18 @@ function addImage(rawdata,format,info,callback,thumbnailsrc,grav) {
51 49
   imageDB.hashed(hash,function(cont) {
52 50
     if(!cont) imageDB.count("imageId",function(err,id) {
53 51
       var path = id+"."+format;
52
+      var source = "./img/src/"+path;
54 53
       console.log("Writing file " + id);
55
-      fs.writeFile("./img/src/"+path,rawdata,"utf8",function() {
54
+      fs.writeFile(source,rawdata,"utf8",function() {
56 55
         console.log("Identifying file " + id);
57
-        im.identify("./img/src/"+path, function(err,features) {
56
+        im.identify(source, function(err,features) {
58 57
           if(err) throw err;
59 58
           var data = { id: id, format: format, filename: path, originalFilename: info.filename,
60 59
                        width: features.width, height: features.height, hash: hash, thumbnailGravity: grav || "center"};
61 60
           data = _.defaults(data,info);
61
+          delete data.gravity; // Fix for duplicate data
62 62
           imageDB.set(id,_.defaults(data,defaultImage));
63
-          imageHandler.thumbnail(thumbnailsrc || ("./img/src/"+path),"./img/thumb/"+path,"./img/thumb2x/"+path,thumbnailsrc?600:features.width,thumbnailsrc?600:features.height,grav);
64
-          if(config.optimize == "all") imageHandler.optimize("./img/src/"+path,data);
63
+          imageHandler.handle(thumbnailsrc || source, path, thumbnailsrc?600:features.width,thumbnailsrc?600:features.height,grav,{optimizeSrc: true});
65 64
           if(_.isFunction(callback)) callback();
66 65
         });
67 66
       });
@@ -73,19 +72,17 @@ function addImage(rawdata,format,info,callback,thumbnailsrc,grav) {
73 72
 // Templating
74 73
 function makeRawTemplate(name,conf,noHeader) {
75 74
   try {
76
-    var conf2 = _.defaults(conf,config,defaultSiteConfig);
75
+    var conf2 = _.defaults(conf,config,defaultSiteConfig,defaultConfig);
77 76
     var body = _.template(templates[name],conf2);
78 77
     if(!noHeader)
79 78
       body = _.template(templates["header"],conf2) + body;
80 79
     return body;
81
-  } catch(e) { return "Template error: " + e.message; }
80
+  } catch(e) { var s = "RawTemplate error: " + e.message + " ["+name+"]"; console.trace(s); return s; }
82 81
 }
83 82
 function makeTemplate(name,conf2,raw,noHeader) {
84
-  try {
85
-    if(raw=="raw") return makeRawTemplate(name,conf,noHeader);
86
-    var conf = _.defaults(conf2,config,defaultSiteConfig);
87
-    return _.template(templates["main"],_.defaults(conf,{page: makeRawTemplate(name,conf,noHeader)}));
88
-  } catch(e) { return "Template error: " + e.message; }
83
+  if(raw=="raw") return makeRawTemplate(name,conf2,noHeader);
84
+  var conf = _.defaults(conf2,config,defaultSiteConfig,defaultConfig);
85
+  return _.template(templates["main"],_.defaults(conf, {page: makeRawTemplate(name,conf,noHeader)}));
89 86
 }
90 87
 
91 88
 // Auth code
@@ -239,7 +236,7 @@ app.get("/regenerate/*", restrictAdmin, parse, function(req,res) {
239 236
   console.log("Regenerating ID " + id);
240 237
   imageDB.regenerate(id,function(){
241 238
     imageDB.get(id,function(image) {
242
-      imageHandler.thumbnail("./img/src/"+image.filename,"./img/thumb/"+image.filename,"./img/thumb2x/"+image.filename,null,null,image.thumbnailGravity);
239
+      imageHandler.handle("./img/src/"+image.filename,image.filename,null,null,image.thumbnailGravity,{optimizeSrc: false});
243 240
       res.redirect("/");
244 241
     });
245 242
   });
@@ -255,7 +252,7 @@ app.post("/edit", express.bodyParser(), restrict, getImagePost, function(req,res
255 252
   image.thumbnailGravity = req.body.gravity || image.thumbnailGravity;
256 253
   image.tags = util.tagArray(req.body.tags_string) || image.tags || [];
257 254
   if(req.files && req.files.thumbnail)
258
-    imageHandler.thumbnail(req.files.thumbnail.path,"./img/thumb/"+image.filename,"./img/thumb2x/"+image.filename,null,null,image.thumbnailGravity);
255
+    imageHandler.handle(req.files.thumbnail.path,image.filename,null,null,image.thumbnailGravity,{optimizeSrc: false});
259 256
   imageDB.set(image.id,image,function() {
260 257
     res.redirect("/");
261 258
   }, true);
@@ -393,7 +390,7 @@ if(argv.t || argv.thumb) {
393 390
   imageDB.images(function(images) {
394 391
     imageDB.getArray(images, function(err,idata) {
395 392
       async.eachLimit(idata,config.optimizationThreads || 1,function(image,next) {
396
-        imageHandler.thumbnail("./img/src/"+image.filename,"./img/thumb/"+image.filename,"./img/thumb2x/"+image.filename,null,null,image.thumbnailGravity,next);
393
+        imageHandler.handle("./img/src/"+image.filename,image.filename,null,null,image.thumbnailGravity,{callback: next, optimizeSrc: false});
397 394
       });
398 395
     });
399 396
   });

+ 0
- 1
cachedb.js View File

@@ -1,7 +1,6 @@
1 1
 var CacheDB = {}
2 2
   , redis = require("redis")
3 3
   , _ = require("underscore")
4
-  , JSON = require("JSON2")
5 4
   , async = require("async")
6 5
   , crypto = require('crypto');
7 6
 

+ 11
- 7
image.js View File

@@ -5,7 +5,6 @@ var _ = require('underscore')
5 5
   , config = require('./config.json')
6 6
   , child = require('child_process')
7 7
   , async = require('async')
8
-  , path = require('path')
9 8
   , util = require('./util.js');
10 9
 
11 10
 var thumbW = 300, thumbH = 300;
@@ -45,24 +44,29 @@ exports.optimize = function(path,data) {
45 44
   });
46 45
 }
47 46
 
48
-exports.thumbnail = function(src,dest,dest2x,w1,h1,grav,cb) {
47
+exports.handle = function(src,destName,w1,h1,grav,options) {
49 48
   var w = w1 || thumbW*2
50 49
     , h = h1 || thumbH*2
50
+    , dest = "./img/thumb/"+destName
51
+    , dest2x = "./img/thumb2x/"+destName
51 52
     , self = this;
52
-  console.log("Thumbnailing " + src);
53
-  t2 = function() {
53
+  console.log("Thumbnailing " + src + " to " + dest);
54
+  t2 = function(err) {
55
+    if(err) throw err;
54 56
     if(_.isString(dest2x) && (w>thumbW || h>thumbH))
55
-      self.resize(src,dest2x,thumbW*2,thumbH*2,function() {
57
+      self.resize(src,dest2x,thumbW*2,thumbH*2,function(err) {
58
+        if(err) throw err;
56 59
         if(optimizeMode == "all" || optimizeMode == "thumbs" || optimizeMode == "thumbnails") {
57 60
           self.optimize(dest,{format: util.fileExt(src)});
58 61
           self.optimize(dest2x,{format: util.fileExt(src)});
59 62
         }
60 63
 	console.log("Done!");
61
-        if(_.isFunction(cb)) cb();
64
+        if(_.isFunction(options.callback)) options.callback();
62 65
       },grav);
63
-    else if(_.isFunction(cb)) cb();
66
+    else if(_.isFunction(options.callback)) options.callback();
64 67
   };
65 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)});
66 70
 }
67 71
 
68 72
 // Create missing directories (just in case)

+ 0
- 1
imagedb.js View File

@@ -1,7 +1,6 @@
1 1
 var ImageDB = {}
2 2
   , redis = require("redis")
3 3
   , _ = require("underscore")
4
-  , JSON = require("JSON2")
5 4
   , async = require("async");
6 5
 
7 6
 var client = null;

+ 0
- 1
package.json View File

@@ -5,7 +5,6 @@
5 5
   "private": true,
6 6
   "dependencies": {
7 7
     "express": "3.x",
8
-    "JSON2": "0.1.x",
9 8
     "underscore": "1.4.x",
10 9
     "mkdirp": "0.3.x",
11 10
     "redis": "0.8.x",

+ 0
- 1
userdb.js View File

@@ -1,7 +1,6 @@
1 1
 var UserDB = {}
2 2
   , redis = require("redis")
3 3
   , _ = require("underscore")
4
-  , JSON = require("JSON2")
5 4
   , async = require("async")
6 5
   , crypto = require('crypto');
7 6
 

+ 4
- 0
util.js View File

@@ -1,3 +1,7 @@
1
+var _ = require("underscore")
2
+  , fs = require("fs")
3
+  , path = require("path");
4
+
1 5
 exports.fileExt = function(name) {
2 6
   var ext = path.extname(name).split(".");
3 7
   return ext[ext.length-1];

Loading…
Cancel
Save