21,7 → 21,6 |
import javax.imageio.ImageIO; |
import org.apache.log4j.Logger; |
import marcoschmidt.image.ImageInfo; |
import ak.photoalbum.util.TimestampRecipient; |
import ak.photoalbum.util.FileUtils; |
import ak.photoalbum.util.FileNameComparator; |
|
37,10 → 36,6 |
protected static final int DEFAULT_MEDIUM_WIDTH = 800; |
protected static final int DEFAULT_MEDIUM_HEIGHT = 800; |
|
protected static final int CACHED_NOT_FOUND = 0; |
protected static final int CACHED_NOT_MODIFIED = 1; |
protected static final int CACHED_WRITTEN = 2; |
|
protected Logger logger; |
protected ImageResizer resizer; |
protected int smallWidth = DEFAULT_SMALL_WIDTH; |
227,12 → 222,12 |
|
for(int i = 0; i < children.length; i++) { |
if(children[i].isDirectory()) { |
writeDir(children[i], -1, null, null); |
writeDir(children[i], null); |
buildCache(children[i]); |
} |
else { |
writeSmall(children[i], -1, null, null); |
writeMedium(children[i], -1, null, null); |
writeSmall(children[i], null); |
writeMedium(children[i], null); |
} |
} |
} |
240,11 → 235,9 |
protected CachedFile getCached(Map cache, File imageFile) |
throws IOException |
{ |
String key = imageFile.getCanonicalPath(); |
CachedFile cached = (CachedFile)cache.get(key); |
CachedFile cached = (CachedFile)cache.get(imageFile.getCanonicalPath()); |
|
if(cached == null || !cached.getFile().exists()) { |
cache.remove(key); |
logger.debug("not found in cache"); |
return null; |
} |
251,7 → 244,7 |
|
if(cached.getOriginTimestamp() != imageFile.lastModified()) { |
cached.getFile().delete(); |
cache.remove(key); |
cache.remove(imageFile.getCanonicalPath()); |
logger.debug("timestamps dont match"); |
return null; |
} |
259,25 → 252,16 |
return cached; |
} |
|
protected int writeCached(Map cache, File imageFile, long ifModifiedSince, |
OutputStream out, TimestampRecipient timestampRecipient) |
protected boolean writeCached(Map cache, File imageFile, OutputStream out) |
throws IOException |
{ |
CachedFile cached = getCached(cache, imageFile); |
|
if(cached == null) return CACHED_NOT_FOUND; |
if(cached == null) return false; |
|
if(ifModifiedSince >= 0 && ifModifiedSince <= cached.getTimestamp()) |
return CACHED_NOT_MODIFIED; // cached image found and not modified |
// since given timestamp - do nothing |
|
if(logger.isDebugEnabled()) |
logger.debug("write cached " + imageFile.getCanonicalPath()); |
|
if(timestampRecipient != null) { |
timestampRecipient.setTimestamp(cached.getTimestamp()); |
} |
|
if(out != null) { |
FileInputStream in = null; |
|
290,7 → 274,7 |
} |
} |
|
return CACHED_WRITTEN; |
return true; |
} |
|
protected File getCacheFile(File dir, File imageFile, String suffix) |
298,8 → 282,7 |
return new File(dir, imageFile.getName() + suffix + "." + format); |
} |
|
// @return last modified timestamp pf the cached thumbnail |
protected long cacheThumbnail(Map cache, File imageFile, |
protected void cacheThumbnail(Map cache, File imageFile, |
BufferedImage thumbnail, String suffix, int width, int height) |
throws IOException |
{ |
315,8 → 298,6 |
cache.put(imageFile.getCanonicalPath(), |
new CachedFile(cacheFile, imageFile, |
cacheFile.lastModified(), imageFile.lastModified(), width, height)); |
|
return cacheFile.lastModified(); |
} |
|
public void startup() |
458,75 → 439,75 |
return new File(cacheDir, imagePath.substring(rootPath.length())); |
} |
|
protected boolean writeThumbnail(Map cache, String suffix, int width, int height, |
File imageFile, long ifModifiedSince, OutputStream out, TimestampRecipient timestampRecipient) |
public void writeSmall(File imageFile, OutputStream out) |
throws IOException |
{ |
int cachedResult = writeCached(cache, imageFile, ifModifiedSince, out, timestampRecipient); |
if(cachedResult != CACHED_NOT_FOUND) return (cachedResult == CACHED_WRITTEN); |
if(logger.isInfoEnabled()) |
logger.info("write small " + imageFile.getCanonicalPath()); |
|
if(writeCached(smallCache, imageFile, out)) return; |
|
if(nativeMode) { |
File dir = getCacheFileDir(imageFile); |
File cacheFile = getCacheFile(dir, imageFile, suffix); |
File cacheFile = getCacheFile(dir, imageFile, SMALL_SUFFIX); |
|
createThumbnailNative(dir, cacheFile, imageFile, width, height); |
loadCacheInfo(cacheFile, imageFile, cache); |
writeCached(cache, imageFile, -1, out, timestampRecipient); |
createThumbnailNative(dir, cacheFile, imageFile, smallWidth, smallHeight); |
loadCacheInfo(cacheFile, imageFile, smallCache); |
writeCached(smallCache, imageFile, out); |
} |
else { |
BufferedImage thumbnail = createThumbnail(imageFile, width, height); |
BufferedImage small = createThumbnail(imageFile, smallWidth, smallHeight); |
|
if(thumbnail != null) { |
if(small != null) { |
// a thumbnail returned - save it into the cache dir |
int sizes[] = calcSizes( |
thumbnail.getWidth(null), thumbnail.getHeight(null), width, height); |
long lastModified = cacheThumbnail( |
cache, imageFile, thumbnail, suffix, sizes[0], sizes[1]); |
small.getWidth(null), small.getHeight(null), smallWidth, smallHeight); |
cacheThumbnail( |
smallCache, imageFile, small, SMALL_SUFFIX, sizes[0], sizes[1]); |
|
if(timestampRecipient != null) { |
timestampRecipient.setTimestamp(lastModified); |
if(out != null) ImageIO.write(small, format, out); |
} |
|
if(out != null) { |
ImageIO.write(thumbnail, format, out); |
} |
} |
} |
|
return true; // image written |
} |
|
public boolean writeSmall(File imageFile, long ifModifiedSince, |
OutputStream out, TimestampRecipient timestampRecipient) |
public void writeMedium(File imageFile, OutputStream out) |
throws IOException |
{ |
if(logger.isInfoEnabled()) |
logger.info("write small " + imageFile.getCanonicalPath()); |
logger.info("write medium " + imageFile.getCanonicalPath()); |
|
return writeThumbnail(smallCache, SMALL_SUFFIX, smallWidth, smallHeight, |
imageFile, ifModifiedSince, out, timestampRecipient); |
if(writeCached(mediumCache, imageFile, out)) return; |
|
if(nativeMode) { |
File dir = getCacheFileDir(imageFile); |
File cacheFile = getCacheFile(dir, imageFile, MEDIUM_SUFFIX); |
|
createThumbnailNative(dir, cacheFile, imageFile, mediumWidth, mediumHeight); |
loadCacheInfo(cacheFile, imageFile, mediumCache); |
writeCached(mediumCache, imageFile, out); |
} |
else { |
BufferedImage medium = createThumbnail(imageFile, mediumWidth, mediumHeight); |
|
public boolean writeMedium(File imageFile, long ifModifiedSince, |
OutputStream out, TimestampRecipient timestampRecipient) |
throws IOException |
{ |
if(logger.isInfoEnabled()) |
logger.info("write medium " + imageFile.getCanonicalPath()); |
if(medium != null) { |
// a image returned - save it into the cache dir |
int sizes[] = calcSizes(medium.getWidth(null), medium.getHeight(null), |
mediumWidth, mediumHeight); |
cacheThumbnail( |
mediumCache, imageFile, medium, MEDIUM_SUFFIX, sizes[0], sizes[1]); |
|
return writeThumbnail(mediumCache, MEDIUM_SUFFIX, mediumWidth, mediumHeight, |
imageFile, ifModifiedSince, out, timestampRecipient); |
if(out != null) ImageIO.write(medium, format, out); |
} |
} |
} |
|
public boolean writeDir(File dir, long ifModifiedSince, |
OutputStream out, TimestampRecipient timestampRecipient) |
public void writeDir(File dir, OutputStream out) |
throws IOException |
{ |
if(logger.isInfoEnabled()) |
logger.info("write dir " + dir.getCanonicalPath()); |
|
int cachedResult = writeCached(dirCache, dir, ifModifiedSince, out, timestampRecipient); |
if(cachedResult != CACHED_NOT_FOUND) return (cachedResult == CACHED_WRITTEN); |
if(writeCached(dirCache, dir, out)) return; |
|
BufferedImage thumbnail = createDirThumbnail(dir); |
|
538,21 → 519,13 |
dirTemplateTimestamp = dirTemplate.lastModified(); |
} |
|
long lastModified = cacheThumbnail(dirCache, dir, thumbnail, DIR_SUFFIX, |
cacheThumbnail(dirCache, dir, thumbnail, DIR_SUFFIX, |
dirTemplateSize[0], dirTemplateSize[1]); |
|
if(timestampRecipient != null) { |
timestampRecipient.setTimestamp(lastModified); |
if(out != null) ImageIO.write(thumbnail, format, out); |
} |
|
if(out != null) { |
ImageIO.write(thumbnail, format, out); |
} |
} |
|
return true; // image written |
} |
|
synchronized protected BufferedImage createThumbnail(File imageFile, |
int width, int height) |
throws IOException |