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')
, redis = require('redis')
, queryparser = require('./queryparser.js').QueryParser
, imageHandler = require('./image.js')
, tempurl = require('./tempurl.js');
, tempurl = require('./tempurl.js')
, util = require('./util.js');

_.str = require('underscore.string');
_.mixin(_.str.exports());
@@ -87,13 +88,6 @@ function makeTemplate(name,conf2,raw,noHeader) {
} catch(e) { return "Template error: " + e.message; }
}

// Tagify string
function tagArray(str) {
var t = str.split(",");
t = _.map(t,function(v){return v.trim();});
return t;
}

// Auth code
app.use(express.cookieParser(config.salt));
app.use(express.session({key: "booru-user",secret: config.salt}));
@@ -126,13 +120,8 @@ function getImagePost(req,res,next) {
next();
});
}
// File/thumbnail
function fileExt(name) {
var ext = path.extname(name).split(".");
return ext[ext.length-1];
}
function finishUpload(res,fn,thfn,path,metadata, next) {
var ext = fileExt(fn);
var ext = util.fileExt(fn);
fs.readFile(path, function(err, data) {
if(err) throw err;
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) {
if(!req.body.uploader)
{ res.send(500,"Missing metadata!"); return; }
var metadata = req.body;
metadata.tags = tagArray(req.body.tags_string);
metadata.tags = util.tagArray(req.body.tags_string);
if(req.files && req.files.thumbnail) thumbnailFilename = req.files.thumbnail.path;
if(!req.files || !req.files.image) {
if(!req.body.url) { res.send(500,"No file specified!"); return; }
ext = fileExt(req.body.url);
ext = util.fileExt(req.body.url);
tempurl.download(req.body.url, function(err, tmpfile, callback){
if(err) { res.send(500,"Download error!"); return; }
finishUpload(res, "file."+fileExt(tmpfile), thumbnailFilename, tmpfile, metadata, callback);
finishUpload(res, "file."+util.fileExt(tmpfile), thumbnailFilename, tmpfile, metadata, callback);
});
} else { finishUpload(res, req.files.image.name, thumbnailFilename, req.files.image.path, metadata); }
});
@@ -264,7 +253,7 @@ app.post("/edit", express.bodyParser(), restrict, getImagePost, function(req,res
image.author = req.body.author || image.author;
image.source = req.body.source || image.source;
image.thumbnailGravity = req.body.gravity || image.thumbnailGravity;
image.tags = tagArray(req.body.tags_string) || image.tags || [];
image.tags = util.tagArray(req.body.tags_string) || image.tags || [];
if(req.files && req.files.thumbnail)
imageHandler.thumbnail(req.files.thumbnail.path,"./img/thumb/"+image.filename,"./img/thumb2x/"+image.filename,null,null,image.thumbnailGravity);
imageDB.set(image.id,image,function() {

+ 7
- 20
image.js View File

@@ -5,7 +5,8 @@ var _ = require('underscore')
, config = require('./config.json')
, child = require('child_process')
, async = require('async')
, path = require('path');
, path = require('path')
, util = require('./util.js');

var thumbW = 300, thumbH = 300;

@@ -19,14 +20,6 @@ exports.resize = function(src,dest,w,h,cb,grav) {
im.resize(cnf,cb);
}

function copy(src,dest) {
fs.createReadStream(src).pipe(fs.createWriteStream(dest));
}

function filesize(name) {
return fs.statSync(name).size;
}

var optqueue = async.queue(function (task, callback) {
if(!config.optimizationEngines) callback();
async.eachSeries(config.optimizationEngines, function(engine,b) {
@@ -44,11 +37,11 @@ var optqueue = async.queue(function (task, callback) {

exports.optimize = function(path,data) {
if(optimizeMode == "none" || !fs.existsSync(path)) return;
var oldfilesize = filesize(path);
var format = (data.format || fileExt(path)).toLowerCase();
var oldfilesize = util.filesize(path);
var format = (data.format || util.fileExt(path)).toLowerCase();
if(format=="jpeg") format="jpg";
optqueue.push({path: path, format: format}, function(err) {
console.log("Done optimizing "+path+", "+oldfilesize+" -> "+filesize(path));
console.log("Done optimizing "+path+", "+oldfilesize+" -> "+util.filesize(path));
});
}

@@ -61,8 +54,8 @@ exports.thumbnail = function(src,dest,dest2x,w1,h1,grav,cb) {
if(_.isString(dest2x) && (w>thumbW || h>thumbH))
self.resize(src,dest2x,thumbW*2,thumbH*2,function() {
if(optimizeMode == "all" || optimizeMode == "thumbs" || optimizeMode == "thumbnails") {
self.optimize(dest,{format: fileExt(src)});
self.optimize(dest2x,{format: fileExt(src)});
self.optimize(dest,{format: util.fileExt(src)});
self.optimize(dest2x,{format: util.fileExt(src)});
}
console.log("Done!");
if(_.isFunction(cb)) cb();
@@ -72,12 +65,6 @@ exports.thumbnail = function(src,dest,dest2x,w1,h1,grav,cb) {
this.resize(src,dest,thumbW,thumbH,t2,grav);
}

// File/thumbnail
function fileExt(name) {
var ext = path.extname(name).split(".");
return ext[ext.length-1];
}

// Create missing directories (just in case)
mkdirp.sync("./img/src");
mkdirp.sync("./img/thumb");

+ 3
- 7
tempurl.js View File

@@ -2,18 +2,14 @@ var http = require('http')
, fs = require('fs')
, _ = require('underscore')
, path = require('path')
, os = require('os');

function fileExt(name) {
var ext = path.extname(name).split(".");
return ext[ext.length-1];
}
, os = require('os')
, util = require('./util.js');

exports.download = function(url, callback) {
var now = new Date();
var tmpfn = path.join(os.tmpDir(), [
"tmp-", now.getTime(), "-", Math.floor(Math.random()*1000000),
".", fileExt(url)
".", util.fileExt(url)
].join(""));
console.log("Downloading " + url + " to " + tmpfn);
var stream = fs.createWriteStream(tmpfn);

+ 15
- 0
util.js View File

@@ -0,0 +1,15 @@
exports.fileExt = function(name) {
var ext = path.extname(name).split(".");
return ext[ext.length-1];
}
exports.tagArray = function(str) {
var t = str.split(",");
t = _.map(t,function(v){return v.trim();});
return t;
}
exports.copyFile = function(src,dest) {
fs.createReadStream(src).pipe(fs.createWriteStream(dest));
}
exports.filesize = function(name) {
return fs.statSync(name).size;
}

Loading…
Cancel
Save