Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Generate thumbnails in memory with in-memory cache. Aw yea!

  • Loading branch information...
commit 808741825c0c23c5489487493a0df82156574cc8 1 parent 2347247
@cianclarke authored
Showing with 51 additions and 30 deletions.
  1. +47 −27 gallery.js
  2. +2 −1  package.json
  3. +2 −2 views/album.ejs
View
74 gallery.js
@@ -1,8 +1,9 @@
var fs = require('fs'),
exif = require('./exif.js'),
walk = require('walk'),
-util = require('util');
-//im = require('imagemagick'); // TODO: For thumbs.
+util = require('util'),
+path = require('path'),
+im = require('imagemagick');
var gallery = {
/*
@@ -44,6 +45,10 @@ var gallery = {
*/
filter: /^Thumbs.db|^\.[a-zA-Z0-9]+/,
/*
+ * Object used to store binary chunks that represent image thumbs
+ */
+ imageCache: {},
+ /*
* Private function to walk a directory and return an array of files
*/
readFiles: function(params, cb){
@@ -147,9 +152,9 @@ var gallery = {
}
}
}
- path = file.rootDir + '/' + file.name
+ var filepath = file.rootDir + '/' + file.name
if(file.name == "info.json") {
- var fullPath = gallery.directory + "/" + path;
+ var fullPath = gallery.directory + "/" + filepath;
fullPath = (gallery.static) ? gallery.static + "/" + fullPath: fullPath;
var info = fs.readFileSync(fullPath);
try{
@@ -171,7 +176,7 @@ var gallery = {
var photoName = file.name.replace(/.[^\.]+$/, "");
var photo = {
name: photoName,
- path: path
+ path: filepath
};
//curAlbum.photos.push(photo);
@@ -214,7 +219,8 @@ var gallery = {
albumChildren = album.albums;
if (photoChildren.length && photoChildren.length>0){
- return photoChildren[0].path;
+ var albumThumb = photoChildren[0].path;
+ return albumThumb;
}else{
if (albumChildren.length && albumChildren.length>1){
return _buildThumbnails(albumChildren[0]);
@@ -391,21 +397,35 @@ var gallery = {
if (rootURL=="" || url.indexOf(rootURL)===-1 /*|| staticTest.test(url)*/){
// This isn't working just quite yet, let's skip over it
-// var thumbTest = /[a-zA-Z0-9].*(\.png|\.jpg)&tn=1/i;
-// if (thumbTest.test(url)){
-// url = req.url = url.replace("&tn=1", "");
-// console.log("[url:]" + url);
-// console.log("[static:]" + me.static);
-// var imagePath = me.static + decodeURI(url);
-// im.resize({
-// srcData: fs.readFileSync(imagePath, 'binary'),
-// width: 256
-// }, function(err, binary, stderr){
-// if (err) util.inspect(err);
-// req.send(binary);
-// });
-//
-// }
+ var thumbTest = /[a-zA-Z0-9].*(\.png|\.jpg)&tn=1/i;
+ if (thumbTest.test(url)){
+ url = req.url = url.replace("&tn=1", "");
+ var imagePath = me.static + decodeURI(url);
+ if (me.imageCache[imagePath]){
+ res.contentType('image/jpg');
+ res.end(me.imageCache[imagePath], 'binary');
+ }else{
+ fs.readFile(imagePath, 'binary', function(err, file){
+ if (err){
+ console.log(err);
+ return res.send(err);
+ }
+ im.resize({
+ srcData: file,
+ width: 256
+ }, function(err, binary, stderr){
+ if (err){
+ util.inspect(err);
+ res.send('error generating thumb');
+ }
+ res.contentType('image/jpg');
+ res.end(binary, 'binary');
+ me.imageCache[imagePath] = binary;
+ });
+ });
+ }
+ return;
+ }
// Not the right URL. We have no business here. Onwards!
return next();
}
@@ -418,17 +438,17 @@ var gallery = {
url =decodeURIComponent(url);
if (url && url!==""){
- var path = url.trim(),
+ var filepath = url.trim(),
isFile = /\b.(jpg|bmp|jpeg|gif|png|tif)\b$/;
- image = isFile.test(path.toLowerCase());
- path = path.split("/");
+ image = isFile.test(filepath.toLowerCase());
+ filepath = filepath.split("/");
if (image){ // If we detect image file name at end, get filename
- image = path.pop();
+ image = filepath.pop();
}
- path = path.join("/").trim();
+ filepath = filepath.join("/").trim();
requestParams = {
- album: path,
+ album: filepath,
photo: image
};
View
3  package.json
@@ -15,7 +15,8 @@
"walk": "*",
"express": "3.0",
"ejs": "*",
- "exif": "git://github.com/cianclarke/node-exif.git"
+ "exif": "git://github.com/cianclarke/node-exif.git",
+ "imagemagick" : "*"
},
"scripts": {
"test": "node test/tests.js",
View
4 views/album.ejs
@@ -21,7 +21,7 @@
<% album.albums.forEach(function(album){ %>
<li class="thumb">
<a href="<%= rootDir%>/<%= album.path%>">
- <div class="image"><img src="/<%= directory %>/<%= album.thumb %>" alt="<%= album.prettyName %>"><br /></div>
+ <div class="image"><img src="/<%= directory %>/<%= album.thumb %>&tn=1" alt="<%= album.prettyName %>"><br /></div>
<span class="caption"><%= album.prettyName %></span>
</a>
</li>
@@ -34,7 +34,7 @@
<% album.photos.forEach(function(photo){ %>
<li class="thumb">
<a href="<%=rootDir %><%= photo.path %>">
- <img src="/<%= directory %>/<%= photo.path %>" alt="<%= photo.name%>"><br />
+ <img src="/<%= directory %>/<%= photo.path %>&tn=1" alt="<%= photo.name%>"><br />
<%= photo.name%>
</a>
</li>
Please sign in to comment.
Something went wrong with that request. Please try again.