-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
145 lines (108 loc) · 3.56 KB
/
server.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const util = require('util');
const express = require('express');
const app = express();
const fs = require('fs');
process.env.UV_THREADPOOL_SIZE = 1;
const sharp = require('sharp');
sharp.concurrency(1);
sharp.cache(0);
//const resize = require('./resize');
const Database = require('better-sqlite3');
const db = new Database('meta.db', { }); //verbose: console.log
let albums= {
TEST: "/home/shreyas/Projects/test_images/images",
PHOTOS: "/media/windows/PHOTOS"
}
function resize(path, width, height, composite) {
const readStream = fs.createReadStream(path);
let transform = sharp(null, { failOnError: false })
.rotate()
.resize({
width: width,
height: height,
fit: "inside",
withoutEnlargement: true
});
if(composite){
transform = transform.composite([{ input: 'play-button-small.png', gravity: 'northwest' }])
}
return readStream.pipe(transform);
}
app.get('/getThumbnail', function(req, res){
let {album, filename, width, height, mimetype} = req.query;
width = +width || undefined;
height = +height || undefined;
switch(mimetype.split("/")[0]){
case "image":
resize(albums[album]+"/"+filename, width, height).pipe(res);
break;
case "video":
resize(".video-thumbnails/"+(album+'/'+filename+'.jpg').replace(/\//gi, '_'), width, height, true).pipe(res);
break;
case "audio":
resize('default-audio-image-4.jpg', width, height).pipe(res);
}
});
app.get('/getImage', (req, res) => {
let {album, filename, width, height, format} = req.query;
width = +width || undefined;
height = +height || undefined;
res.type(`image/${format || 'jpg'}`);
res.set({
"Content-Disposition": `inline;filename="${filename.split(/\//).pop()}"`
});
resize(albums[album]+"/"+filename, width, height).pipe(res);
});
app.get('/getFile', (req, res) => {
let {album, filename} = req.query;
res.set({
"Content-Disposition": `inline;filename="${filename.split(/\//).pop()}"`
});
res.sendFile(albums[album]+"/"+filename);
});
app.get(/\/last(\d+)/, function(req, res){
let count = req.params[0];
let stmt = db.prepare(`
select album, filename, mimetype, aspectratio as aspectRatio
from meta
where album = 'PHOTOS'
order by filedate desc limit ?`);
res.json(stmt.all(count));
});
app.get('/search', function(req, res){
let cols = ["folder", "filename", "mimetype", "keywords", "faces", "rating", "make", "model"];
let nonCols = ["group", "filematch", "datefrom", "dateto"];
let filters="", orderby="";
for (let [k,v] of Object.entries(req.query)){
if(cols.includes(k)){
filters = filters + `${filters? " AND " : ""} { ${k} }: "${v}"`
} else if(nonCols.includes(k)){
// not filters; handle these separately
} else {
filters = filters + `${filters? " AND " : ""} ${k}`
}
}
if (req.query.group){
orderby = `${req.query.group} desc, datetime(filedate)`;
} else {
orderby = `folder desc, datetime(filedate)`;
}
let stmt = db.prepare(`
select album,
--replace(filename, '&', '%26') as filename,
filename,
mimetype, aspectratio as aspectRatio, folder
, date(filedate) filedate
from meta
where mimetype is not null
and (mimetype like 'image%' or mimetype like 'video%' or mimetype like 'audio%')
and mimetype not like '%3gpp' and mimetype not like '%x-ms-wmv'
and meta match '${filters}'
order by ${orderby}`);
console.log(stmt);
res.json(stmt.all());
});
app.use(express.static('public'));
app.listen(9000, ()=>{
console.log("Server started and listening in port 9000!");
});