From 050ce391863ab5da563f6aa1a18ceaefce6a01d7 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Thu, 23 Aug 2018 21:55:41 -0700 Subject: [PATCH] Switched to DESC order for date, and to SQLite Signed-off-by: James Ketrenos --- config/default.json | 2 +- frontend/src/ketr-photos/ketr-photos.html | 6 +- server/db/index.js | 3 + server/routes/days.js | 2 +- server/routes/photos.js | 2 +- server/scanner.js | 79 +++++++++++++++-------- 6 files changed, 62 insertions(+), 32 deletions(-) diff --git a/config/default.json b/config/default.json index c7073a6..71084c1 100644 --- a/config/default.json +++ b/config/default.json @@ -1,6 +1,6 @@ { "db": { - "host": "mysql://photos:p4$$w0rd@localhost:3306/photos", + "host": "sqlite:photos.db", "options": { "logging" : false, "timezone": "+00:00" diff --git a/frontend/src/ketr-photos/ketr-photos.html b/frontend/src/ketr-photos/ketr-photos.html index ed10739..7266b07 100755 --- a/frontend/src/ketr-photos/ketr-photos.html +++ b/frontend/src/ketr-photos/ketr-photos.html @@ -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 diff --git a/server/db/index.js b/server/db/index.js index 4ab6924..ea29022 100644 --- a/server/db/index.js +++ b/server/db/index.js @@ -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 }); diff --git a/server/routes/days.js b/server/routes/days.js index 16bb609..959a4c8 100644 --- a/server/routes/days.js +++ b/server/routes/days.js @@ -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(/\?.*$/, "") + "%" diff --git a/server/routes/photos.js b/server/routes/photos.js index 8f86f25..93aa6c5 100644 --- a/server/routes/photos.js +++ b/server/routes/photos.js @@ -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); diff --git a/server/scanner.js b/server/scanner.js index 897daa2..2a2ce48 100644 --- a/server/scanner.js +++ b/server/scanner.js @@ -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);