5,18 → 5,13 |
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; |
26,20 → 21,15 |
|
public class Logic |
{ |
protected static final int DEFAULT_COLUMNS = 5; |
protected static final int DEFAULT_ROWS = 4; |
protected static final int DEFAULT_COLUMNS = 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() |
{ |
49,14 → 39,12 |
public void init(String imagesRoot, String imagesMask, |
String cacheDir, String thumbnailFormat, |
Integer smallWidth, Integer smallHeight, |
Integer mediumWidth, Integer mediumHeight, |
Integer columns, Integer rows, |
Integer mediumWidth, Integer mediumHeight, Integer columns, |
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); |
97,7 → 85,7 |
|
public void getEntry(String path, IndexEntry page, |
IndexEntry index, IndexEntry prev, IndexEntry current, IndexEntry next) |
throws IOException, SAXException, LogicException |
throws IOException, LogicException |
{ |
File file = new File(imagesRoot, path); |
|
118,7 → 106,6 |
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); |
127,7 → 114,7 |
} |
|
protected void setEntryInfo(IndexEntry entry, File file, boolean small) |
throws IOException, SAXException |
throws IOException |
{ |
String title = file.getName(); |
int[] size; |
151,22 → 138,7 |
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() |
{ |
228,92 → 200,25 |
} |
} |
|
protected Digester createMetaDigester() |
public List listDirectory(String dirName) |
throws UnsupportedEncodingException, IOException, LogicException |
{ |
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 false; |
if(!dir.exists()) return null; |
|
File[] children = dir.listFiles(imagesFilter); |
int pos = page * columns * rows; |
List rows = new ArrayList(); |
int pos = 0; |
|
Arrays.sort(children, fileNameComparator); |
metaInfos.clear(); // FIXME do this more intelligent (?) |
|
// 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) { |
while(pos < children.length) { |
List row = new ArrayList(); |
int rowPos = 0; |
|
table.add(row); |
rows.add(row); |
|
while(rowPos < columns && pos < children.length) { |
String path = getPath(children[pos]); |
328,26 → 233,9 |
title = FileUtils.extractFileName(title); |
} |
|
IndexEntry entry = new IndexEntry(children[pos], |
row.add(new IndexEntry(children[pos], |
URLEncoder.encode(path, URL_ENCODING), |
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); |
title, children[pos].isDirectory(), size[0], size[1])); |
rowPos++; |
pos++; |
} |
358,7 → 246,7 |
} |
} |
|
return true; |
return rows; |
} |
|
protected String getPath(File file) |