Switched to DESC order for date, and to SQLite

Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
James Ketr 2018-08-23 21:55:41 -07:00
parent e8adf6b820
commit 050ce39186
6 changed files with 62 additions and 32 deletions

View File

@ -1,6 +1,6 @@
{
"db": {
"host": "mysql://photos:p4$$w0rd@localhost:3306/photos",
"host": "sqlite:photos.db",
"options": {
"logging" : false,
"timezone": "+00:00"

View File

@ -261,12 +261,12 @@
loadNextPhotos: function() {
var cursor = this.photos[this.photos.length - 1];
this._loadPhotos(cursor.taken.toString().replace(/T.*/, "") + "_" + cursor.id, +1, true);
this._loadPhotos(cursor.taken.toString().replace(/T.*/, "") + "_" + cursor.id, -1, true);
},
loadPrevPhotos: function() {
var cursor = this.photos[0];
this._loadPhotos(cursor.taken.toString().replace(/T.*/, "") + "_" + cursor.id, -1);
this._loadPhotos(cursor.taken.toString().replace(/T.*/, "") + "_" + cursor.id, +1);
},
_loadPhotos: function(start, dir, append) {
@ -275,7 +275,7 @@
}
this.loading = true;
dir = dir || +1;
dir = dir || -1;
var params = {
limit: Math.ceil(this.clientWidth / 200) * Math.ceil(this.clientHeight / 200),
dir: dir

View File

@ -39,6 +39,7 @@ function init() {
allowNull: true
}
}, {
timestamps: false,
classMethods: {
associate: function() {
Album.hasOne(Album, {as:'Album', foreignKey: 'parentId'});
@ -67,6 +68,8 @@ function init() {
key: 'id',
}
}
}, {
timestamps: false
});

View File

@ -28,7 +28,7 @@ router.get("/", function(req, res/*, next*/) {
let start = moment(req.query.start || null) || null,
end = moment(req.query.end || null) || null;
let query = "SELECT DATE(taken) AS date,COUNT(*) AS count FROM photos WHERE path LIKE :path GROUP BY DATE(taken) ORDER BY date";
let query = "SELECT DATE(taken) AS date,COUNT(*) AS count FROM photos WHERE path LIKE :path GROUP BY DATE(taken) ORDER BY date DESC";
return photoDB.sequelize.query(query, {
replacements: {
path: req.url.replace(/\?.*$/, "") + "%"

View File

@ -74,6 +74,7 @@ router.get("/", function(req, res/*, next*/) {
if (order == "DESC") {
if (cursor) {
cursor = moment(cursor);
photos = photos.filter(function(photo) {
if (!cursor.isSame(photo.taken, "day")) {
return true;
@ -81,7 +82,6 @@ router.get("/", function(req, res/*, next*/) {
return photo.id < id;
});
}
photos.reverse();
} else {
if (cursor) {
cursor = moment(cursor);

View File

@ -34,13 +34,13 @@ function scanDir(parent, path) {
}).then(function(results) {
if (results.length == 0) {
// console.log("Adding " + path + " under " + parent, replacements);
return photoDB.sequelize.query("INSERT INTO albums SET path=:path,parentId=:parent", {
return photoDB.sequelize.query("INSERT INTO albums (path,parentId) VALUES(:path,:parent)", {
replacements: {
path: path,
parent: parent || null
},
}).then(function(results) {
return results[0];
return results[1].lastID;
});
} else {
return results[0].id;
@ -78,7 +78,6 @@ function scanDir(parent, path) {
/* If this file has an original NEF on the system, don't add the JPG to the
* DB */
if (/\.nef$/i.exec(files[i]) && file == files[i].replace(/\.nef$/i, ".jpg")) {
console.log("Skipping DB duplicate for " + files[i]);
return false;
}
}
@ -172,6 +171,20 @@ function mkdirPromise(path) {
});
}
function existsPromise(path) {
return new Promise(function(resolve, reject) {
fs.stat(path, function(err, stats) {
if (!err) {
return resolve(true);
}
if (err.code == 'ENOENT') {
return resolve(false);
}
return reject(err);
});
})
}
function convertNefToJpg(path, file) {
console.log("Converting " + path + "/" + file);
@ -246,10 +259,16 @@ function triggerWatcher() {
let tmp = Promise.resolve(file);
/* If this is a Nikon RAW file, convert it to JPG and move to /raw dir */
if (/\.nef$/i.exec(file)) {
tmp = mkdirPromise(picturesPath + path + "/raw").then(function() {
return convertNefToJpg(path, file);
}).then(function() {
return file.replace(/\.nef$/i, ".jpg"); /* We converted from NEF => JPG */
tmp = existsPromise(picturesPath + path + "/" + file.replace(/\.nef$/i, ".jpg")).then(function(exists) {
if (exists) {
return file.replace(/\.nef$/i, ".jpg"); /* We converted from NEF => JPG */
}
return mkdirPromise(picturesPath + path + "/raw").then(function() {
return convertNefToJpg(path, file);
}).then(function() {
return file.replace(/\.nef$/i, ".jpg"); /* We converted from NEF => JPG */
});
});
}
@ -276,16 +295,14 @@ function triggerWatcher() {
filename: file,
width: metadata.width,
height: metadata.height,
added: moment().format().replace(/T.*/, "")
added: moment().format()
};
if (metadata.exif && metadata.exif.exif && metadata.exif.exif.DateTimeOriginal && !isNaN(metadata.exif.exif.DateTimeOriginal.valueOf())) {
metadata.exif.exif.DateTimeOriginal.setHours(0, 0, 0, 0);
metadata.exif.exif.DateTimeOriginal = metadata.exif.exif.DateTimeOriginal.toISOString().replace(/T.*/, "");
replacements.taken = moment(metadata.exif.exif.DateTimeOriginal, "YYYY-MM-DD").format().replace(/T.*/, "");
replacements.modified = moment(metadata.exif.exif.DateTimeOriginal).format().replace(/T.*/, "");
replacements.taken = moment(metadata.exif.exif.DateTimeOriginal).format();
replacements.modified = moment(metadata.exif.exif.DateTimeOriginal).format();
if (replacements.taken == "Invalid date" || replacements.taken == "1899-11-30") {
if (replacements.taken == "Invalid date" || replacements.taken.replace(/T.*/, "") == "1899-11-30") {
console.log("Invalid EXIF date information: ", JSON.stringify(metadata.exif.exif));
replacements.taken = replacements.modified = moment(created).format();
}
@ -303,20 +320,30 @@ function triggerWatcher() {
replacements.taken = replacements.modified = date;
}
return image.resize(256, 256).toFile(dst).then(function() {
return photoDB.sequelize.query("INSERT INTO photos " +
"SET albumId=:albumId,path=:path,filename=:filename,added=DATE(:added),modified=DATE(:modified),taken=DATE(:taken),width=:width,height=:height", {
replacements: replacements
}).then(function() {
toProcess--;
if (moment().add(-5, 'seconds') > lastMessage) {
console.log("Items to be processed: " + toProcess);
lastMessage = moment();
}
return existsPromise(dst).then(function(exists) {
let resize;
if (!exists) {
resize = image.resize(256, 256).toFile(dst);
} else {
resize = Promise.resolve();
}
return resize.then(function() {
return photoDB.sequelize.query("INSERT INTO photos " +
"(albumId,path,filename,added,modified,taken,width,height)" +
"VALUES(:albumId,:path,:filename,DATE(:added),DATE(:modified),DATE(:taken),:width,:height)", {
replacements: replacements
});
}).catch(function(error) {
console.error("Error resizing image, writing to disc, or updating DB: " + src, error);
throw error;
});
}).catch(function(error) {
console.error("Error resizing image, writing to disc, or updating DB: " + src, error);
throw error;
}).then(function() {
toProcess--;
if (moment().add(-5, 'seconds') > lastMessage) {
console.log("Items to be processed: " + toProcess);
lastMessage = moment();
}
});
}).catch(function(error) {
console.error("Error reading image " + src + ": ", error);