-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresize.js
More file actions
executable file
·102 lines (102 loc) · 3.36 KB
/
Copy pathresize.js
File metadata and controls
executable file
·102 lines (102 loc) · 3.36 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node
"use strict";
var models = require('./models/index2')
, Photo = models.Photo
, fs = require('fs')
, path = require('path')
, gm = require('gm')
, async = require('async')
, newImgPath = path.join(__dirname,'public','images','engagement-photos')
, publicPath = path.join('/images','engagement-photos')
, walk = function(dir,callback){
console.log('walking dir: '+dir);
var results = [];
fs.readdir(dir,function(err,list){
console.log('found files: %s',JSON.stringify(list));
if(err) { return callback(err); }
var pending = list.length;
if(!pending) { return callback(null,results); }
list.forEach(function(file){
file = dir + '/' + file;
fs.stat(file, function(err,stat){
if(stat && stat.isDirectory()){
walk(file,function(err,res){
results = results.concat(res);
if(!--pending) { callback(null,results); }
});
} else {
results.push(file);
if(!--pending) { callback(null,results); }
}
});
});
});
}
, saving = 0
, order = 0
, resize = function(filepath,thmbpath,pubname,photo,cb){
gm(filepath)
.size(function(err,size){
if(!err){
var newwidth = 1920;
var newheight = null;
gm(filepath).resize(newwidth,newheight).write(thmbpath,function(err){
if(!err){
photo.lrgloc = thmbpath;
photo.lrgsrc = pubname;
photo.save(function(err,newdoc){
saving--;
if(err){
console.log('error when inserting db entry: %s',JSON.stringify(err));
} else {
console.log('saved photo: %s',JSON.stringify(newdoc));
}
cb();
});
} else {
console.log('error when writing image: %s',JSON.stringify(err));
cb();
}
});
} else {
console.log('error when reading image size: %s',JSON.stringify(err));
cb();
}
});
}
, q = async.queue(function(task,callback) {
resize(task.filename,task.outname,task.pubname,task.dbphoto,callback);
}, 4)
;
q.drain = function(){
if(saving === 0){
process.exit(console.log('all done!'));
}
};
//console.log(models);
walk(newImgPath,function(err,files){
console.log('finished walking, found files %s',JSON.stringify(files));
if(err) { process.exit(console.log(err)); }
files.forEach(function(file){
saving++;
if(/thmb|med/.test(file)){
saving--;
return;
}
var newfile = path.join(newImgPath,path.basename(file));
var filepath = path.join(newImgPath,path.basename(file));
var lrgpath = path.join(newImgPath,path.basename(file,'.jpg'))+'-lrg.jpg';
var publicpath = path.join(publicPath,path.basename(file));
var lrgpublicpath = path.join(publicPath,path.basename(file,'.jpg'))+'-lrg.jpg';
if(path.extname(file).toLowerCase() === '.jpg'){
Photo.findOne({location:file},function(err,photo){
if(!err && photo){
q.push({filename:filepath,outname:lrgpath,pubname:lrgpublicpath,dbphoto:photo});
}else{
saving--;
console.log('couldnt find photo in db: %s',JSON.stringify(err));
}
});
}
});
});