Updated thumbnail creation to create a scaled version
Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
parent
baf152d57c
commit
e9fd50a054
@ -151,7 +151,7 @@ Polymer({
|
||||
download: function(event) {
|
||||
console.log("Download tapped");
|
||||
var anchor = document.createElement('a');
|
||||
anchor.href = this.src;
|
||||
anchor.href = this.base + this.item.path + "/" + this.item.filename;
|
||||
anchor.setAttribute("download", this.src.replace(/.*\/([^/]+)$/, "$1"));
|
||||
anchor.style.display = "none";
|
||||
document.body.appendChild(anchor);
|
||||
@ -233,6 +233,12 @@ Polymer({
|
||||
},
|
||||
|
||||
attached: function() {
|
||||
var base = document.querySelector("base");
|
||||
if (base) {
|
||||
this.base = new URL(base.href).pathname.replace(/\/$/, ""); /* Remove trailing slash if there */
|
||||
} else {
|
||||
this.base = "";
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
@ -876,7 +876,8 @@
|
||||
this.lightBoxElement.selected = false;
|
||||
}
|
||||
|
||||
this.$.lightbox.src = this.base + el.item.path + "/" + el.item.filename;
|
||||
this.$.lightbox.item = el.item;
|
||||
this.$.lightbox.src = this.base + el.item.path + "/thumbs/scaled/" + el.item.filename;
|
||||
this.lightBoxElement = el;
|
||||
this.lightBoxElement.selected = true;
|
||||
this.disableScrolling = true;
|
||||
|
@ -71,31 +71,19 @@ function scanDir(parent, path) {
|
||||
}
|
||||
}).then(function(parent) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
console.log("Scanning " + replacements.path);
|
||||
console.log("Scanning ..." + replacements.path);
|
||||
|
||||
fs.readdir(path, function(err, files) {
|
||||
if (err) {
|
||||
console.warn(" Could not readdir " + path);
|
||||
return resolve(null);
|
||||
console.warn("Could not readdir " + path);
|
||||
return resolve([]);
|
||||
}
|
||||
|
||||
return resolve(files);
|
||||
});
|
||||
}).then(function(files) {
|
||||
scanning++;
|
||||
|
||||
let hasThumbs = false;
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
if (files[i] == "thumbs") {
|
||||
hasThumbs = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let tmp;
|
||||
if (!hasThumbs) {
|
||||
tmp = mkdirPromise(path + "/thumbs");
|
||||
} else {
|
||||
tmp = Promise.resolve();
|
||||
}
|
||||
|
||||
/* Remove 'thumbs' and 'raw' directories from being processed */
|
||||
files = files.filter(function(file) {
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
@ -116,32 +104,22 @@ function scanDir(parent, path) {
|
||||
return file != "raw" && file != "thumbs" && file != ".git" && file != "corrupt";
|
||||
});
|
||||
|
||||
return tmp.then(function() {
|
||||
return mkdir(path + "/thumbs/scaled").then(function() {
|
||||
return Promise.map(files, function(file) {
|
||||
let filepath = path + "/" + file;
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.stat(filepath, function(err, stats) {
|
||||
if (err) {
|
||||
console.warn("Could not stat " + filepath);
|
||||
return resolve(false);
|
||||
}
|
||||
|
||||
return stat(filepath).then(function(stats) {
|
||||
if (stats.isDirectory()) {
|
||||
return scanDir(parent, filepath, stats).then(function(entry) {
|
||||
return resolve(true);
|
||||
}).catch(function(error) {
|
||||
console.warn("scanDir failed");
|
||||
return reject(error);
|
||||
return scanDir(parent, filepath).catch(function(error) {
|
||||
console.warn("Could not scanDir " + filepath + ": " + error);
|
||||
});
|
||||
}
|
||||
|
||||
/* Check file extensions */
|
||||
if (!re.exec(file)) {
|
||||
return resolve(true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const replacements = {
|
||||
path: path.slice(picturesPath.length),
|
||||
filename: file.replace(rawExtension, ".jpg") /* We will be converting from NEF/ORF => JPG */
|
||||
@ -156,12 +134,11 @@ function scanDir(parent, path) {
|
||||
if (photo.length == 0) {
|
||||
processQueue.push([ replacements.path, file, stats.mtime, parent ]);
|
||||
}
|
||||
return resolve(true);
|
||||
}).catch(function(error) {
|
||||
console.error("Sequelize.query failed");
|
||||
return reject(error);
|
||||
});
|
||||
console.error("Sequelize.query failed: ", error);
|
||||
});
|
||||
}).catch(function(error) {
|
||||
console.warn("Could not stat " + filepath + ": " + error);
|
||||
});
|
||||
}, {
|
||||
concurrency: 1
|
||||
@ -171,12 +148,6 @@ function scanDir(parent, path) {
|
||||
const endStamp = Date.now();
|
||||
console.log("Scanning completed in " + Math.round(((endStamp - startStamp))) + "ms. " + processQueue.length + " items to process.");
|
||||
}
|
||||
}).then(function() {
|
||||
return resolve();
|
||||
});
|
||||
}).catch(function(error) {
|
||||
console.error("Processing 'tmp' failed");
|
||||
return reject(error);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -190,44 +161,59 @@ let processRunning = false;
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
const sharp = require("sharp"), exif = require("exif-reader");
|
||||
|
||||
function mkdirPromise(path) {
|
||||
const stat = function (path) {
|
||||
if (path.indexOf(picturesPath) != 0) {
|
||||
path = picturesPath + path;
|
||||
}
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.stat(path, function(err) {
|
||||
if (err && err.code != 'ENOENT') {
|
||||
console.warn("Could not stat " + path);
|
||||
return reject();
|
||||
return new Promise(function (resolve, reject) {
|
||||
fs.stat(path, function (error, stats) {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
return resolve(stats);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const mkdir = function (_path) {
|
||||
if (_path.indexOf(picturesPath) == 0) {
|
||||
_path = _path.substring(picturesPath.length + 1);
|
||||
}
|
||||
|
||||
if (!err) {
|
||||
return resolve();
|
||||
let parts = _path.split("/"), path;
|
||||
parts.unshift(picturesPath);
|
||||
return Promise.mapSeries(parts, function (part) {
|
||||
if (!path) {
|
||||
path = picturesPath;
|
||||
} else {
|
||||
path += "/" + part;
|
||||
}
|
||||
return stat(path).catch(function (error) {
|
||||
if (error.code != "ENOENT") {
|
||||
throw error;
|
||||
}
|
||||
|
||||
fs.mkdir(path, function(err) {
|
||||
if (err && err.code != 'EEXIST') {
|
||||
return reject("Unable to create " + path + "\n" + err);
|
||||
return new Promise(function (resolve, reject) {
|
||||
console.log("mkdir " + path);
|
||||
fs.mkdir(path, function (error) {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
const exists = function(path) {
|
||||
return stat(path).then(function() {
|
||||
return true;
|
||||
}).catch(function() {
|
||||
return false;
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
function convertRawToJpg(path, file) {
|
||||
@ -236,10 +222,10 @@ function convertRawToJpg(path, file) {
|
||||
path = picturesPath + path;
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.stat(path + "/" + file.replace(rawExtension, ".jpg"), function(err, stats) {
|
||||
if (!err) {
|
||||
return exists(path + "/" + file.replace(rawExtension, ".jpg")).then(function(exist) {
|
||||
if (exist) {
|
||||
console.log("Skipping already converted file: " + file);
|
||||
return resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const ufraw = spawn("ufraw-batch", [
|
||||
@ -259,6 +245,7 @@ function convertRawToJpg(path, file) {
|
||||
stderr.push(data);
|
||||
});
|
||||
|
||||
return new Promise(function(ufraw, resolve, reject) {
|
||||
ufraw.on('exit', function(stderr, code, signal) {
|
||||
if (signal || code != 0) {
|
||||
let error = "UFRAW for " + path + "/" + file + " returned an error: " + code + "\n" + signal + "\n" + stderr.join("\n") + "\n";
|
||||
@ -271,7 +258,7 @@ function convertRawToJpg(path, file) {
|
||||
return reject(error);
|
||||
});
|
||||
}
|
||||
return mkdirPromise(path + "/raw").then(function() {
|
||||
return mkdir(path + "/raw").then(function() {
|
||||
fs.rename(path + "/" + file, path + "/raw/" + file, function(err) {
|
||||
if (err) {
|
||||
console.error("Unable to move RAW file: " + path + "/" + file);
|
||||
@ -280,9 +267,10 @@ function convertRawToJpg(path, file) {
|
||||
return resolve();
|
||||
});
|
||||
}).catch(function(error) {
|
||||
console.warn("mkdirPromise failed");
|
||||
console.warn("mkdir failed");
|
||||
return reject(error);
|
||||
});
|
||||
}.bind(this, ufraw));
|
||||
}.bind(this, stderr));
|
||||
});
|
||||
});
|
||||
@ -295,7 +283,7 @@ function moveCorrupt(path, file) {
|
||||
|
||||
console.warn("Moving corrupt file '" + file + "' to " + path + "/corrupt");
|
||||
|
||||
return mkdirPromise(path + "/corrupt").then(function() {
|
||||
return mkdir(path + "/corrupt").then(function() {
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.rename(path + "/" + file, path + "/corrupt/" + file, function(err) {
|
||||
if (err) {
|
||||
@ -323,17 +311,15 @@ function triggerWatcher() {
|
||||
return Promise.map(processing, function(entry) {
|
||||
var path = entry[0], file = entry[1], created = entry[2], albumId = entry[3];
|
||||
|
||||
// console.log("Processing " + src);
|
||||
|
||||
let tmp = Promise.resolve(file);
|
||||
/* If this is a Nikon RAW file, convert it to JPG and move to /raw dir */
|
||||
if (rawExtension.exec(file)) {
|
||||
tmp = existsPromise(picturesPath + path + "/" + file.replace(rawExtension, ".jpg")).then(function(exists) {
|
||||
if (exists) {
|
||||
tmp = exists(picturesPath + path + "/" + file.replace(rawExtension, ".jpg")).then(function(exist) {
|
||||
if (exist) {
|
||||
return file.replace(rawExtension, ".jpg"); /* We converted from NEF/ORF => JPG */
|
||||
}
|
||||
|
||||
return mkdirPromise(picturesPath + path + "/raw").then(function() {
|
||||
return mkdir(picturesPath + path + "/raw").then(function() {
|
||||
return convertRawToJpg(path, file);
|
||||
}).then(function() {
|
||||
return file.replace(rawExtension, ".jpg"); /* We converted from NEF/ORF => JPG */
|
||||
@ -343,7 +329,6 @@ function triggerWatcher() {
|
||||
|
||||
return tmp.then(function(file) {
|
||||
var src = picturesPath + path + "/" + file,
|
||||
dst = picturesPath + path + "/thumbs/" + file,
|
||||
image = sharp(src);
|
||||
|
||||
return image.limitInputPixels(1073741824).metadata().then(function(metadata) {
|
||||
@ -407,24 +392,35 @@ function triggerWatcher() {
|
||||
replacements.taken = replacements.modified = date;
|
||||
}
|
||||
|
||||
return existsPromise(dst).then(function(exists) {
|
||||
let resize;
|
||||
if (!exists) {
|
||||
resize = image.resize(256, 256).toFile(dst);
|
||||
} else {
|
||||
resize = Promise.resolve();
|
||||
let dst = picturesPath + path + "/thumbs/" + file;
|
||||
|
||||
return exists(dst).then(function(exist) {
|
||||
if (exist) {
|
||||
return;
|
||||
}
|
||||
|
||||
return resize.then(function() {
|
||||
return image.resize(256, 256).toFile(dst).catch(function(error) {
|
||||
console.error("Error resizing image: " + dst, error);
|
||||
throw error;
|
||||
});
|
||||
}).then(function() {
|
||||
let dst = picturesPath + path + "/thumbs/scaled/" + file;
|
||||
return exists(dst).then(function(exist) {
|
||||
if (exist) {
|
||||
return;
|
||||
}
|
||||
|
||||
return image.resize(Math.min(1024, metadata.width)).toFile(dst).catch(function(error) {
|
||||
console.error("Error resizing image: " + dst, error);
|
||||
throw error;
|
||||
});
|
||||
});
|
||||
}).then(function() {
|
||||
return photoDB.sequelize.query("INSERT INTO photos " +
|
||||
"(albumId,path,filename,added,modified,taken,width,height,name)" +
|
||||
"VALUES(:albumId,:path,:filename,DATETIME(:added),DATETIME(:modified),DATETIME(:taken),:width,:height,:name)", {
|
||||
replacements: replacements
|
||||
});
|
||||
}).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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user