-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
131 lines (114 loc) · 3.76 KB
/
index.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Thumbnail generation script for images or bzipped images
Usage: node index.js -infiles '/images/image*.tiff.bzip' -outdir /thumbs -width 250 -height 180
*/
var argv = require('optimist')
.usage('Create image thumbnails from .\nUsage: $0')
.demand(['infiles', 'outdir', 'width', 'height'])
.argv,
fs = require('fs'),
path = require('path'),
gm = require('gm'),
glob = require('glob'),
bunzip = require('seek-bzip'),
async = require('async');
var options,
tempFileName,
pictureFileName,
outFileName,
compressedData,
stats,
data;
options = {
nocase: true
};
var filelist = glob.sync(argv.infiles, options);
console.log(filelist.length + ' files for processing');
fs.appendFileSync(argv.outdir + '/index.html', "<html>\n <head>\n");
fs.appendFileSync(argv.outdir + '/index.html',
"<style>\n" +
".tile {\n" +
" float: left;\n" +
" background-color: #CCC;\n" +
" width: 193.333px;\n" +
" height: 172px;\n" +
" line-height: 170px;\n" +
"}\n" +
".thumbnail {\n" +
" max-width: 193.33333px;\n" +
" max-height: 172px;\n" +
" height: auto;\n" +
" vertical-align: middle;\n" +
"}\n" +
"</style>\n" +
"</head>\n" +
"<body>\n");
try { //make img directory if not present
// Query the entry
stats = fs.lstatSync(argv.outdir + '/img');
if (stats.isDirectory()) {
console.log('img directory already present');
} else {
console.log('Making img directory');
fs.createDirSync(argv.outdir + '/img');
}
} catch (e) {
console.log('Making img directory');
fs.createDirSync(argv.outdir + '/img');
}
async.eachLimit(
filelist,
20, //limit asynchronous processing to 20 files at the same time
function (file, callback) {
'use strict';
console.log(file);
/*
if (file.substr(file.length - 4, file.length) === ".bzip") {
compressedData = fs.readFileSync(file);
data = bunzip.decode(compressedData);
tempFileName = '/tmp/' + file.substr(0, file.length - 4); //get rid of bzip extension
fs.writeFileSync(pictureFileName, data);
pictureFileName = tempFileName;
} else {
pictureFileName = String(file);
}
*/
pictureFileName = String(file);
outFileName = pictureFileName.split('/');
outFileName = outFileName[outFileName.length - 1];
fs.appendFileSync(
argv.outdir + '/index.html',
"<div class='tile'>\n" +
"<a href='file://" + pictureFileName + "'>" +
"<img class='thumbnail' src='img/" + outFileName + "'></img>\n" +
"</a>\n" +
"</div>\n"
);
try {
// Query the entry
stats = fs.lstatSync(argv.outdir + '/img/' + outFileName);
if (stats.isFile()) {
console.log(argv.outdir + '/img/' + outFileName + ' already exists, skipping');
callback();
}
} catch (e) {
console.log('File does not exist yet, making thumbnail');
gm(pictureFileName).resize(argv.width, argv.height).write(argv.outdir + '/img/' + outFileName, function (err) {
if (err) {
console.log(err);
callback(err);
} else {
console.log('Wrote ' + argv.outdir + outFileName);
callback();
}
});
}
},
function (err) {
'use strict';
if (err) {
fs.appendFile(argv.outdir + '/errors.txt', new Date().toLocaleString() + " " + pictureFileName + ": " + err + "\n");
}
}
);
fs.appendFile('index.html', "</html>\n </body>\n");