-
Notifications
You must be signed in to change notification settings - Fork 8
/
find-orphants.js
101 lines (87 loc) · 2.67 KB
/
find-orphants.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
var fs = require('fs');
var persist = require("persist");
var type = persist.type;
var models = require('./models');
var path = require('path');
var gm = require('gm');
var im = require('imagemagick');
var exec = require('sync-exec');
var db = require('./routes/api.db');
var DB_CONFIG = {
'driver': 'sqlite3',
'filename': __dirname + '/content/kollektor.sqlite',
'defautFilename': 'content/kollektor.sqlite.default'
};
persist.setDefaultConnectOptions(DB_CONFIG);
var imagesDir = __dirname + '/content/images';
var dbFile = __dirname + '/content/kollektor.sqlite';
var files = fs.readdirSync(imagesDir);
console.log('files', files.length);
console.log(files.slice(0, 10));
persist.connect(function(err, connection) {
console.log('connected');
var cached = {};
var orphants = 0;
var zombies = 0;
models.Image.using(connection).orderBy('id', 'desc').all(function(err, imagesData) {
console.log('imagesData', imagesData.length, 'orphants:', (files.length - imagesData.length*2)/2 + '?');
imagesData.forEach(function(img){
cached[img.cachedUrl] = true;
cached[img.thumbUrl] = true;
})
function processNext() {
if (files.length == 0) {
return;
}
var file = files.shift();
while (cached[file]) {
file = files.shift();
}
if (files.length == 0) {
return;
}
var filePath = imagesDir + '/' + file;
var ext = path.extname(file);
var base = path.basename(file, ext);
var stats = fs.statSync(filePath);
var fileThumb = base + '_thumb' + ext;
var fileThumbPath = imagesDir + '/' + file;
var hasThumb = false;
var tags = ['sys-orphant']
if (files[0] == fileThumb) {
files.shift();
hasThumb = true;
}
else {
fileThumb = file;
fileThumbPath = filePath;
tags.push('sys-no-thumb');
}
//if (ext == '.gif') {
var info = exec("identify -format '%W x %H\n' " + filePath);
if (info.stdout) {
var size = info.stdout.split('\n')[0].split(' x ');
var ratio = size[0]/size[1];
console.log(files.length, file, size, hasThumb);
var imageData = {
title: base,
referer: "",
originalUrl : "",
cachedUrl : file,
thumbUrl : fileThumb,
ratio : ratio,
tags: tags
}
db.addImage(connection, imageData, function(err, ok) {
// console.log(err, ok)
setTimeout(processNext, 1);
})
}
else {
console.log(files.length, file, 'invalid file');
//fs.unlink(filePath);
}
}
processNext();
});
});