182 lines
No EOL
5.3 KiB
JavaScript
182 lines
No EOL
5.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const ExifReader = require('exifreader');
|
|
const sharp = require('sharp');
|
|
const axios = require('axios');
|
|
const loc = require('./geo.js');
|
|
|
|
//var productsDatabase = { photos: []}
|
|
//var i = 1;
|
|
//var s = 0;
|
|
//console.log("start search");
|
|
async function searchFile(dir, fileExt) {
|
|
var i = 1;
|
|
var s = 0;
|
|
s++;
|
|
// read the contents of the directory
|
|
console.log("è una directory?");
|
|
if(fs.existsSync('./public/photos')){
|
|
var ff = fs.statSync('./public/photos');
|
|
if(!ff.isDirectory()){
|
|
console.log("non è una dir");
|
|
};
|
|
};
|
|
const files = fs.readdirSync(dir);
|
|
// search through the files
|
|
for (let k = 0; k < files.length; k++) {
|
|
file = files[k];
|
|
const filePath = path.join(dir, file);
|
|
|
|
// get the file stats
|
|
const fileStat = fs.statSync(filePath);
|
|
// if the file is a directory, recursively search the directory
|
|
if (fileStat.isDirectory()) {
|
|
await searchFile(filePath, fileExt);
|
|
} else if (path.extname(file).toLowerCase() == fileExt) {
|
|
// if the file is a match, print it
|
|
var dd = dir.split("/");
|
|
var d = dd.slice(1);
|
|
var d1 = dd.slice(1);
|
|
d1[1]= "thumbs";
|
|
var ff = file.split(".");
|
|
var fn = ff.slice(0,-1).join(".");
|
|
var f1 = fn+'_min'+'.'+ff.at(-1);
|
|
var f2 = fn+'_avg'+'.'+ff.at(-1);
|
|
var f3 = fn+'_sharp'+'.'+ff.at(-1);
|
|
var extFilePath = path.join(d.join("/"),file);
|
|
var extThumbMinPath = path.join(d1.join("/"),f1);
|
|
var extThumbAvgPath = path.join(d1.join("/"),f2);
|
|
var extThumbSharpPath = path.join("public",d1.join("/"),f3);
|
|
var thumbMinPath = path.join("public",extThumbMinPath);
|
|
var thumbAvgPath = path.join("public",extThumbAvgPath);
|
|
var dt = path.join("public",d1.join("/"));
|
|
if (!fs.existsSync(dt)){
|
|
fs.mkdirSync(dt, { recursive: true });
|
|
}
|
|
var data;
|
|
try {
|
|
data = fs.readFileSync(filePath);
|
|
} catch (err) {
|
|
//console.error(err);
|
|
}
|
|
const tags = await ExifReader.load(filePath, {expanded: true});
|
|
//console.log(tags);
|
|
var time = tags.exif.DateTimeOriginal;
|
|
if (time === undefined){} else {time=time.value[0]}
|
|
var gps = tags['gps'];
|
|
//console.log(gps.Latitude);
|
|
//console.log(gps.latitude);
|
|
//var loc;
|
|
var locat;
|
|
//console.log("ora");
|
|
if (gps === undefined){} else {
|
|
// locat = await loc(gps.Longitude,gps.Latitude);
|
|
}
|
|
//if (time === undefined ){console.log(filePath)}
|
|
//console.log("read: "+filePath);
|
|
await sharp(data)
|
|
.resize(100,100,"inside")
|
|
.withMetadata()
|
|
.toFile(thumbMinPath, (err, info) => {});
|
|
await sharp(data)
|
|
.resize(400)
|
|
.withMetadata()
|
|
.toFile(thumbAvgPath, (err, info) => {});
|
|
console.log(i+" - "+file);
|
|
scrivi({
|
|
id: i,
|
|
name: file,
|
|
path: extFilePath,
|
|
thub1: extThumbMinPath,
|
|
thub2: extThumbAvgPath,
|
|
gps: tags['gps'],
|
|
data: time,
|
|
location: locat
|
|
});
|
|
i++;
|
|
}
|
|
if(k == files.length-1) {
|
|
if (s == 1) {
|
|
//scrivi(productsDatabase.photos);
|
|
//return;
|
|
//console.log("finito1");
|
|
//console.log(productsDatabase);
|
|
} else {
|
|
s--;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
async function thumb(filePath, opt){
|
|
try {
|
|
const thumbnail = await imageThumbnail(filePath, opt);
|
|
//console.log(thumbnail);
|
|
return thumbnail;
|
|
} catch (err) {
|
|
//console.error(err);
|
|
}
|
|
}
|
|
// start the search in the current directory
|
|
async function scanPhoto(dir){
|
|
await searchFile(dir, '.jpg');
|
|
//console.log("finito2");
|
|
}
|
|
|
|
function scrivi(json) {
|
|
fetch('http://192.168.1.3:7771/auth/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({'email':'fabio@gmail.com', 'password':'master66'}),
|
|
})
|
|
.then(response => response.json())
|
|
.then(user1 => {
|
|
const myHeaders = new Headers();
|
|
myHeaders.append('Authorization', 'Bearer ' + user1.token);
|
|
myHeaders.append('Content-Type', 'application/json');
|
|
//console.log(myHeaders.get("Content-Type"));
|
|
//console.log(myHeaders.get("Authorization"));
|
|
fetch('http://192.168.1.3:7771/photos', {
|
|
method: 'POST',
|
|
headers: myHeaders,
|
|
body: JSON.stringify(json),
|
|
})
|
|
.then(response => response.json())
|
|
//.then(user => console.log("caricato"));
|
|
|
|
});
|
|
}
|
|
|
|
function azzera() {
|
|
fetch('http://192.168.1.3:7771/auth/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({'email':'fabio@gmail.com', 'password':'master66'}),
|
|
})
|
|
.then(response => response.json())
|
|
.then(user1 => {
|
|
const myHeaders = new Headers();
|
|
myHeaders.append('Authorization', 'Bearer ' + user1.token);
|
|
myHeaders.append('Content-Type', 'application/json');
|
|
//console.log(myHeaders.get("Content-Type"));
|
|
//console.log(myHeaders.get("Authorization"));
|
|
fetch('http://192.168.1.3:7771/photos', {
|
|
method: 'POST',
|
|
headers: myHeaders,
|
|
body: "",
|
|
})
|
|
.then(response => response.json())
|
|
.then(user => console.log("azzerato totalmente"));
|
|
|
|
});
|
|
}
|
|
|
|
|
|
|
|
module.exports = scanPhoto; |