217 lines
6.1 KiB
JavaScript
Executable File
217 lines
6.1 KiB
JavaScript
Executable File
"use strict";
|
|
|
|
const express = require("express"),
|
|
fs = require("fs"),
|
|
url = require("url"),
|
|
config = require("config"),
|
|
moment = require("moment");
|
|
|
|
let photoDB;
|
|
|
|
require("../db/photos").then(function(db) {
|
|
photoDB = db;
|
|
});
|
|
|
|
const router = express.Router();
|
|
|
|
/* Each photos has:
|
|
|
|
* locations
|
|
* people
|
|
* date
|
|
* tags
|
|
* photo info
|
|
|
|
*/
|
|
router.get("/memories/:date?", function(req, res/*, next*/) {
|
|
let limit = parseInt(req.query.limit) || 50,
|
|
id, cursor, index;
|
|
|
|
if (req.query.next) {
|
|
let parts = req.query.next.split("_");
|
|
cursor = new Date(parts[0]);
|
|
id = parseInt(parts[1]);
|
|
} else {
|
|
cursor = "";
|
|
id = -1;
|
|
}
|
|
|
|
if (id == -1) {
|
|
index = "strftime('%m-%d',taken)=strftime('%m-%d',:date)";
|
|
} else {
|
|
index = "((strftime('%Y-%m-%d',taken)=strftime('%Y-%m-%d',:cursor) AND photos.id<"+id+ ") OR " +
|
|
"(strftime('%m-%d',taken)=strftime('%m-%d',:cursor) AND strftime('%Y',taken)<strftime('%Y',:cursor)))";
|
|
}
|
|
|
|
let date = new Date("2016-" + req.params.date);
|
|
let query = "SELECT photos.*,albums.path AS path FROM photos " +
|
|
"INNER JOIN albums ON (albums.id=photos.albumId) " +
|
|
"WHERE (photos.duplicate=0 AND photos.deleted=0 AND photos.scanned NOT NULL AND " + index + ") " +
|
|
"ORDER BY strftime('%Y-%m-%d',taken) DESC,id DESC LIMIT " + (limit + 1);
|
|
|
|
// console.log("Memories for " + date.toISOString().replace(/^2016-(.*)T.*$/, "$1"));
|
|
// if (cursor) {
|
|
// console.log("Cursor" + cursor.toISOString().replace(/T.*/, ""));
|
|
// }
|
|
|
|
|
|
return photoDB.sequelize.query(query, {
|
|
replacements: {
|
|
cursor: cursor,
|
|
date: date
|
|
},
|
|
type: photoDB.Sequelize.QueryTypes.SELECT
|
|
}).then(function(photos) {
|
|
photos.forEach(function(photo) {
|
|
for (var key in photo) {
|
|
if (photo[key] instanceof Date) {
|
|
photo[key] = moment(photo[key]);
|
|
}
|
|
}
|
|
});
|
|
|
|
let more = photos.length > limit; /* We queried one extra item to see if there are more than LIMIT available */
|
|
|
|
let last;
|
|
if (more) {
|
|
photos.splice(limit);
|
|
last = photos[photos.length - 1];
|
|
}
|
|
|
|
let results = {
|
|
items: photos.sort(function(a, b) {
|
|
return new Date(b.taken) - new Date(a.taken);
|
|
})
|
|
};
|
|
|
|
if (more) {
|
|
results.cursor = new Date(last.taken).toISOString().replace(/T.*/, "") + "_" + last.id;
|
|
results.more = true;
|
|
}
|
|
return res.status(200).json(results);
|
|
}).catch(function(error) {
|
|
console.error("Query failed: " + query);
|
|
return Promise.reject(error);
|
|
});
|
|
});
|
|
|
|
router.get("/duplicates", function(req, res/*, next*/) {
|
|
let replacements = {};
|
|
|
|
return photoDB.sequelize.query(
|
|
"SELECT filename,COUNT(*) AS count FROM photos WHERE photos.duplicate!=1 AND photos.deleted!=1 GROUP BY filename HAVING count > 1", {
|
|
replacements: replacements,
|
|
type: photoDB.Sequelize.QueryTypes.SELECT,
|
|
raw: true
|
|
}).then(function(duplicates) {
|
|
let filenames = [];
|
|
duplicates.forEach(function(duplicate) {
|
|
filenames.push(duplicate.filename);
|
|
});
|
|
|
|
replacements.filenames = filenames;
|
|
|
|
return photoDB.sequelize.query(
|
|
"SELECT photos.*,albums.path AS path,photohashes.hash,(albums.path || photos.filename) AS filepath FROM photos " +
|
|
"LEFT JOIN albums ON albums.id=photos.albumId " +
|
|
"LEFT JOIN photohashes ON photohashes.photoId=photos.id " +
|
|
"WHERE filename IN (:filenames) ORDER BY photos.filename", {
|
|
replacements: replacements,
|
|
type: photoDB.Sequelize.QueryTypes.SELECT,
|
|
raw: true
|
|
}).then(function(photos) {
|
|
return res.status(200).json({
|
|
items: photos
|
|
});
|
|
});
|
|
}).catch(function(error) {
|
|
return Promise.reject(error);
|
|
});
|
|
});
|
|
|
|
router.get("/trash", function(req, res/*, next*/) {
|
|
return photoDB.sequelize.query(
|
|
"SELECT photos.*,albums.path AS path,(albums.path || photos.filename) AS filepath FROM photos " +
|
|
"LEFT JOIN albums ON albums.id=photos.albumId " +
|
|
"WHERE deleted=1 ORDER BY photos.filename", {
|
|
type: photoDB.Sequelize.QueryTypes.SELECT,
|
|
raw: true
|
|
}).then(function(photos) {
|
|
return res.status(200).json({
|
|
items: photos
|
|
});
|
|
});
|
|
});
|
|
|
|
router.get("/*", function(req, res/*, next*/) {
|
|
let limit = parseInt(req.query.limit) || 50,
|
|
id, cursor, index;
|
|
|
|
if (req.query.next) {
|
|
let parts = req.query.next.split("_");
|
|
cursor = parts[0];
|
|
id = parseInt(parts[1]);
|
|
} else {
|
|
cursor = "";
|
|
id = -1;
|
|
}
|
|
|
|
if (id == -1) {
|
|
index = "";
|
|
} else {
|
|
index = "AND ((strftime('%Y-%m-%d',taken)=strftime('%Y-%m-%d',:cursor) AND photos.id<"+id+ ") OR " +
|
|
"(strftime('%m-%d',taken)=strftime('%m-%d',:cursor) AND strftime('%Y',taken)<strftime('%Y',:cursor)))";
|
|
}
|
|
|
|
let path = decodeURI(req.url).replace(/\?.*$/, "").replace(/^\//, ""),
|
|
query = "SELECT photos.*,albums.path AS path FROM photos " +
|
|
"INNER JOIN albums ON (albums.id=photos.albumId AND albums.path LIKE :path) " +
|
|
"WHERE (photos.duplicate=0 AND photos.deleted=0 AND photos.scanned NOT NULL " + index + ") " +
|
|
"ORDER BY strftime('%Y-%m-%d',taken) DESC,id DESC LIMIT " + (limit + 1),
|
|
replacements = {
|
|
cursor: cursor,
|
|
path: path + "%"
|
|
};
|
|
|
|
// console.log("Fetching from: " + JSON.stringify(replacements, null, 2) + "\nwith:\n" + query);
|
|
|
|
return photoDB.sequelize.query(query, {
|
|
replacements: replacements,
|
|
type: photoDB.Sequelize.QueryTypes.SELECT
|
|
}).then(function(photos) {
|
|
photos.forEach(function(photo) {
|
|
for (var key in photo) {
|
|
if (photo[key] instanceof Date) {
|
|
photo[key] = moment(photo[key]);
|
|
}
|
|
}
|
|
});
|
|
|
|
let more = photos.length > limit; /* We queried one extra item to see if there are more than LIMIT available */
|
|
|
|
let last;
|
|
if (more) {
|
|
photos.splice(limit);
|
|
last = photos[photos.length - 1];
|
|
}
|
|
|
|
let results = {
|
|
items: photos.sort(function(a, b) {
|
|
return new Date(b.taken) - new Date(a.taken);
|
|
})
|
|
};
|
|
|
|
if (more) {
|
|
results.cursor = new Date(last.taken).toISOString().replace(/T.*/, "") + "_" + last.id;
|
|
results.more = true;
|
|
}
|
|
return res.status(200).json(results);
|
|
}).catch(function(error) {
|
|
console.error("Query failed: " + query);
|
|
return Promise.reject(error);
|
|
});
|
|
});
|
|
|
|
|
|
module.exports = router;
|