Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1241 → Rev 1242

/PhotoAlbum/trunk/src/ak/photoalbum/webapp/Logic.java
5,13 → 5,18
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.Hashtable;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.FileNotFoundException;
import java.net.URLEncoder;
import org.xml.sax.SAXException;
import org.apache.commons.digester.Digester;
import org.apache.log4j.Logger;
import ak.photoalbum.images.Thumbnailer;
import ak.photoalbum.images.ThumbnailPosition;
21,15 → 26,20
 
public class Logic
{
protected static final int DEFAULT_COLUMNS = 4;
protected static final int DEFAULT_COLUMNS = 5;
protected static final int DEFAULT_ROWS = 4;
protected static final String URL_ENCODING = "UTF-8";
protected static final String META_FILE_NAME = "meta.xml"; // FIXME make configurable (?)
 
protected Logger logger;
protected Thumbnailer thumbnailer;
protected File imagesRoot;
protected int columns = DEFAULT_COLUMNS;
protected int rows = DEFAULT_ROWS;
protected ImagesFilter imagesFilter;
protected Comparator fileNameComparator = new FileNameComparator(true);
protected Digester metaDigester = createMetaDigester();
protected Map metaInfos = new Hashtable(); // <File, MetaInfo>
 
protected Logic()
{
39,12 → 49,14
public void init(String imagesRoot, String imagesMask,
String cacheDir, String thumbnailFormat,
Integer smallWidth, Integer smallHeight,
Integer mediumWidth, Integer mediumHeight, Integer columns,
Integer mediumWidth, Integer mediumHeight,
Integer columns, Integer rows,
String dirTemplate, String dirThumbnailPositions)
{
this.imagesRoot = new File(imagesRoot);
this.imagesFilter = new ImagesFilter(imagesMask);
if(columns != null) this.columns = columns.intValue();
if(rows != null) this.rows = rows.intValue();
 
this.thumbnailer = new Thumbnailer();
this.thumbnailer.setImagesRoot(this.imagesRoot);
85,7 → 97,7
 
public void getEntry(String path, IndexEntry page,
IndexEntry index, IndexEntry prev, IndexEntry current, IndexEntry next)
throws IOException, LogicException
throws IOException, SAXException, LogicException
{
File file = new File(imagesRoot, path);
 
106,15 → 118,16
throw new FileNotFoundException("[" + file.getCanonicalPath()
+ "] not found in [" + dir.getCanonicalPath() + "]");
 
metaInfos.clear(); // FIXME make this more intelligent
setEntryInfo(page, file, false);
setEntryInfo(current, file, true);
setEntryInfo(index, dir, true);
if(pos > 0) setEntryInfo(prev, children[pos-1], true);
if(pos < children.length-1) setEntryInfo(next, children[pos+1], true);
if(pos > 0) setEntryInfo(prev, children[pos-1], true);
if(pos < children.length-1) setEntryInfo(next, children[pos+1], true);
}
 
protected void setEntryInfo(IndexEntry entry, File file, boolean small)
throws IOException
throws IOException, SAXException
{
String title = file.getName();
int[] size;
124,11 → 137,11
size = thumbnailer.getDirSize(file);
}
else {
title = FileUtils.extractFileName(title);
title = FileUtils.extractFileName(title);
 
if(small)
size = thumbnailer.getSmallSize(file);
else
else
size = thumbnailer.getMediumSize(file);
}
 
138,6 → 151,21
entry.setIsDir(file.isDirectory());
entry.setWidth(size[0]);
entry.setHeight(size[1]);
 
MetaInfoItem meta = findMetaInfo(imagesRoot, file);
if(meta != null) {
if(meta.getTitle() != null) {
entry.setTitle(meta.getTitle());
}
if(meta.getSubtitle() != null) {
entry.setSubtitle(meta.getSubtitle());
entry.setSubtitleMime(meta.getSubtitleMime());
}
if(meta.getComment() != null) {
entry.setComment(meta.getComment());
entry.setCommentMime(meta.getCommentMime());
}
}
}
 
public String getThumbnailMime()
200,25 → 228,92
}
}
 
public List listDirectory(String dirName)
throws UnsupportedEncodingException, IOException, LogicException
protected Digester createMetaDigester()
{
Digester digester = new Digester();
digester.setValidating(false);
 
digester.addObjectCreate("meta", MetaInfo.class);
digester.addObjectCreate("meta/item", MetaInfoItem.class);
digester.addSetProperties("meta/item", "id", "id");
digester.addBeanPropertySetter("meta/item/title", "title");
digester.addBeanPropertySetter("meta/item/subtitle", "subtitle");
digester.addSetProperties("meta/item/subtitle", "mime", "subtitleMime");
digester.addBeanPropertySetter("meta/item/comment", "comment");
digester.addSetProperties("meta/item/comment", "mime", "commentMime");
digester.addSetNext("meta/item", "addItem");
 
return digester;
}
 
protected MetaInfo getMetaInfo(File dir)
throws IOException, SAXException
{
MetaInfo meta = (MetaInfo)metaInfos.get(dir);
if(meta != null) return meta;
 
File metaFile = new File(dir, META_FILE_NAME);
if(!metaFile.exists()) return null;
 
meta = (MetaInfo)metaDigester.parse(new FileInputStream(metaFile));
meta.setDir(dir);
metaInfos.put(dir, meta);
 
return meta;
}
 
protected MetaInfoItem findMetaInfo(File rootDir, File file)
throws IOException, SAXException
{
file = file.getCanonicalFile();
rootDir = rootDir.getCanonicalFile();
 
File dir = file;
if(!dir.isDirectory())
dir = dir.getParentFile();
 
MetaInfoItem metaItem = null;
for(; metaItem == null; dir = dir.getParentFile()) {
if(dir == null) break;
 
MetaInfo meta = getMetaInfo(dir);
if(meta != null) {
metaItem = meta.findItem(file);
}
if(rootDir.equals(dir)) break;
}
 
return metaItem;
}
 
public boolean listDirectory(String dirName, int page, List table, List pages)
throws IOException, LogicException, SAXException
{
File dir = new File(imagesRoot, dirName);
 
securePath(imagesRoot, dir);
if(!dir.exists()) return null;
if(!dir.exists()) return false;
 
File[] children = dir.listFiles(imagesFilter);
List rows = new ArrayList();
int pos = 0;
File[] children = dir.listFiles(imagesFilter);
int pos = page * columns * rows;
 
Arrays.sort(children, fileNameComparator);
metaInfos.clear(); // FIXME do this more intelligent (?)
 
while(pos < children.length) {
// the pages list
pages.clear();
for(int i = 0; i < (int)Math.ceil((double)children.length / columns / rows); i++) {
pages.add(new PageItem(i, i == page));
}
 
// the main table
table.clear();
while(pos < children.length && pos < (page+1) * columns * rows) {
List row = new ArrayList();
int rowPos = 0;
 
rows.add(row);
table.add(row);
 
while(rowPos < columns && pos < children.length) {
String path = getPath(children[pos]);
233,9 → 328,26
title = FileUtils.extractFileName(title);
}
 
row.add(new IndexEntry(children[pos],
IndexEntry entry = new IndexEntry(children[pos],
URLEncoder.encode(path, URL_ENCODING),
title, children[pos].isDirectory(), size[0], size[1]));
title, children[pos].isDirectory(), size[0], size[1]);
 
MetaInfoItem meta = findMetaInfo(imagesRoot, children[pos]);
if(meta != null) {
if(meta.getTitle() != null) {
entry.setTitle(meta.getTitle());
}
if(meta.getSubtitle() != null) {
entry.setSubtitle(meta.getSubtitle());
entry.setSubtitleMime(meta.getSubtitleMime());
}
if(meta.getComment() != null) {
entry.setComment(meta.getComment());
entry.setCommentMime(meta.getCommentMime());
}
}
 
row.add(entry);
rowPos++;
pos++;
}
246,7 → 358,7
}
}
 
return rows;
return true;
}
 
protected String getPath(File file)