Browse Source

put some stuff into util.js

pull/2/head
asiekierka 6 years ago
parent
commit
20eed68db9
4 changed files with 32 additions and 45 deletions
  1. 7
    18
      app.js
  2. 7
    20
      image.js
  3. 3
    7
      tempurl.js
  4. 15
    0
      util.js

+ 7
- 18
app.js View File

@@ -17,7 +17,8 @@ var express = require('express')
17 17
   , redis = require('redis')
18 18
   , queryparser = require('./queryparser.js').QueryParser
19 19
   , imageHandler = require('./image.js')
20
-  , tempurl = require('./tempurl.js');
20
+  , tempurl = require('./tempurl.js')
21
+  , util = require('./util.js');
21 22
 
22 23
 _.str = require('underscore.string');
23 24
 _.mixin(_.str.exports());
@@ -87,13 +88,6 @@ function makeTemplate(name,conf2,raw,noHeader) {
87 88
   } catch(e) { return "Template error: " + e.message; }
88 89
 }
89 90
 
90
-// Tagify string
91
-function tagArray(str) {
92
-  var t = str.split(",");
93
-  t = _.map(t,function(v){return v.trim();});
94
-  return t;
95
-}
96
-
97 91
 // Auth code
98 92
 app.use(express.cookieParser(config.salt));
99 93
 app.use(express.session({key: "booru-user",secret: config.salt}));
@@ -126,13 +120,8 @@ function getImagePost(req,res,next) {
126 120
     next();
127 121
   });
128 122
 }
129
-// File/thumbnail
130
-function fileExt(name) {
131
-  var ext = path.extname(name).split(".");
132
-  return ext[ext.length-1];
133
-}
134 123
 function finishUpload(res,fn,thfn,path,metadata, next) {
135
-  var ext = fileExt(fn);
124
+  var ext = util.fileExt(fn);
136 125
   fs.readFile(path, function(err, data) {
137 126
     if(err) throw err;
138 127
     addImage(data,ext,metadata,function(){ res.send("OK"); if(_.isFunction(next)) next(); },thfn,metadata.gravity);
@@ -145,14 +134,14 @@ app.post("/upload/post", restrict, function(req,res) {
145 134
   if(!req.body.uploader)
146 135
     { res.send(500,"Missing metadata!"); return; }
147 136
   var metadata = req.body;
148
-  metadata.tags = tagArray(req.body.tags_string);
137
+  metadata.tags = util.tagArray(req.body.tags_string);
149 138
   if(req.files && req.files.thumbnail) thumbnailFilename = req.files.thumbnail.path;
150 139
   if(!req.files || !req.files.image) {
151 140
     if(!req.body.url) { res.send(500,"No file specified!"); return; }
152
-    ext = fileExt(req.body.url);
141
+    ext = util.fileExt(req.body.url);
153 142
     tempurl.download(req.body.url, function(err, tmpfile, callback){
154 143
       if(err) { res.send(500,"Download error!"); return; }
155
-      finishUpload(res, "file."+fileExt(tmpfile), thumbnailFilename, tmpfile, metadata, callback);
144
+      finishUpload(res, "file."+util.fileExt(tmpfile), thumbnailFilename, tmpfile, metadata, callback);
156 145
     });
157 146
   } else { finishUpload(res, req.files.image.name, thumbnailFilename, req.files.image.path, metadata); }
158 147
 });
@@ -264,7 +253,7 @@ app.post("/edit", express.bodyParser(), restrict, getImagePost, function(req,res
264 253
   image.author = req.body.author || image.author;
265 254
   image.source = req.body.source || image.source;
266 255
   image.thumbnailGravity = req.body.gravity || image.thumbnailGravity;
267
-  image.tags = tagArray(req.body.tags_string) || image.tags || [];
256
+  image.tags = util.tagArray(req.body.tags_string) || image.tags || [];
268 257
   if(req.files && req.files.thumbnail)
269 258
     imageHandler.thumbnail(req.files.thumbnail.path,"./img/thumb/"+image.filename,"./img/thumb2x/"+image.filename,null,null,image.thumbnailGravity);
270 259
   imageDB.set(image.id,image,function() {

+ 7
- 20
image.js View File

@@ -5,7 +5,8 @@ var _ = require('underscore')
5 5
   , config = require('./config.json')
6 6
   , child = require('child_process')
7 7
   , async = require('async')
8
-  , path = require('path');
8
+  , path = require('path')
9
+  , util = require('./util.js');
9 10
 
10 11
 var thumbW = 300, thumbH = 300;
11 12
 
@@ -19,14 +20,6 @@ exports.resize = function(src,dest,w,h,cb,grav) {
19 20
   im.resize(cnf,cb);
20 21
 }
21 22
 
22
-function copy(src,dest) {
23
-  fs.createReadStream(src).pipe(fs.createWriteStream(dest));
24
-}
25
-
26
-function filesize(name) {
27
-  return fs.statSync(name).size;
28
-}
29
-
30 23
 var optqueue = async.queue(function (task, callback) {
31 24
   if(!config.optimizationEngines) callback();
32 25
   async.eachSeries(config.optimizationEngines, function(engine,b) {
@@ -44,11 +37,11 @@ var optqueue = async.queue(function (task, callback) {
44 37
 
45 38
 exports.optimize = function(path,data) {
46 39
   if(optimizeMode == "none" || !fs.existsSync(path)) return;
47
-  var oldfilesize = filesize(path);
48
-  var format = (data.format || fileExt(path)).toLowerCase();
40
+  var oldfilesize = util.filesize(path);
41
+  var format = (data.format || util.fileExt(path)).toLowerCase();
49 42
   if(format=="jpeg") format="jpg";
50 43
   optqueue.push({path: path, format: format}, function(err) {
51
-    console.log("Done optimizing "+path+", "+oldfilesize+" -> "+filesize(path));
44
+    console.log("Done optimizing "+path+", "+oldfilesize+" -> "+util.filesize(path));
52 45
   });
53 46
 }
54 47
 
@@ -61,8 +54,8 @@ exports.thumbnail = function(src,dest,dest2x,w1,h1,grav,cb) {
61 54
     if(_.isString(dest2x) && (w>thumbW || h>thumbH))
62 55
       self.resize(src,dest2x,thumbW*2,thumbH*2,function() {
63 56
         if(optimizeMode == "all" || optimizeMode == "thumbs" || optimizeMode == "thumbnails") {
64
-          self.optimize(dest,{format: fileExt(src)});
65
-          self.optimize(dest2x,{format: fileExt(src)});
57
+          self.optimize(dest,{format: util.fileExt(src)});
58
+          self.optimize(dest2x,{format: util.fileExt(src)});
66 59
         }
67 60
 	console.log("Done!");
68 61
         if(_.isFunction(cb)) cb();
@@ -72,12 +65,6 @@ exports.thumbnail = function(src,dest,dest2x,w1,h1,grav,cb) {
72 65
   this.resize(src,dest,thumbW,thumbH,t2,grav);
73 66
 }
74 67
 
75
-// File/thumbnail
76
-function fileExt(name) {
77
-  var ext = path.extname(name).split(".");
78
-  return ext[ext.length-1];
79
-}
80
-
81 68
 // Create missing directories (just in case)
82 69
 mkdirp.sync("./img/src");
83 70
 mkdirp.sync("./img/thumb");

+ 3
- 7
tempurl.js View File

@@ -2,18 +2,14 @@ var http = require('http')
2 2
   , fs = require('fs')
3 3
   , _ = require('underscore')
4 4
   , path = require('path')
5
-  , os = require('os');
6
-
7
-function fileExt(name) {
8
-  var ext = path.extname(name).split(".");
9
-  return ext[ext.length-1];
10
-}
5
+  , os = require('os')
6
+  , util = require('./util.js');
11 7
 
12 8
 exports.download = function(url, callback) {
13 9
   var now = new Date();
14 10
   var tmpfn = path.join(os.tmpDir(), [
15 11
     "tmp-", now.getTime(), "-", Math.floor(Math.random()*1000000),
16
-    ".", fileExt(url)
12
+    ".", util.fileExt(url)
17 13
   ].join(""));
18 14
   console.log("Downloading " + url + " to " + tmpfn);
19 15
   var stream = fs.createWriteStream(tmpfn);

+ 15
- 0
util.js View File

@@ -0,0 +1,15 @@
1
+exports.fileExt = function(name) {
2
+  var ext = path.extname(name).split(".");
3
+  return ext[ext.length-1];
4
+}
5
+exports.tagArray = function(str) {
6
+  var t = str.split(",");
7
+  t = _.map(t,function(v){return v.trim();});
8
+  return t;
9
+}
10
+exports.copyFile = function(src,dest) {
11
+  fs.createReadStream(src).pipe(fs.createWriteStream(dest));
12
+}
13
+exports.filesize = function(name) {
14
+  return fs.statSync(name).size;
15
+}

Loading…
Cancel
Save