1,4 → 1,4 |
package ak.photoalbum.logic; |
package ak.photoalbum.webapp; |
|
import java.util.List; |
import java.util.ArrayList; |
15,30 → 15,33 |
import java.io.OutputStream; |
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; |
import ak.photoalbum.util.FileUtils; |
import ak.photoalbum.util.FileNameComparator; |
import ak.photoalbum.util.ImagesFilter; |
import ak.photoalbum.config.ConfigRoot; |
import ak.photoalbum.config.ConfigBranch; |
import ak.photoalbum.config.ConfigDirThumbnail; |
|
public class Logic |
{ |
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 ConfigRoot config; |
protected Map branches = new Hashtable(); // <String, Branch> |
protected File imagesRoot; |
protected int columns = DEFAULT_COLUMNS; |
protected int rows = DEFAULT_ROWS; |
protected ImagesFilter imagesFilter; |
protected Comparator fileNameComparator = new FileNameComparator(true); |
protected Digester configDigester = createConfigDigester(); |
protected Digester metaDigester = createMetaDigester(); |
protected Map metaInfos = new Hashtable(); // <File, MetaInfo> |
|
protected Logic() |
{ |
111,38 → 114,59 |
} |
|
public void init(ResourceFactory resourceFactory, String configPath) |
throws IOException, SAXException, LogicException |
{ |
logger.info("starting"); |
config = (ConfigRoot)configDigester.parse(resourceFactory.getAsStream(configPath)); |
/* |
this.imagesRoot = new File(imagesRoot); |
this.imagesFilter = new ImagesFilter(imagesMask); |
if(columns != null) this.columns = columns.intValue(); |
if(rows != null) this.rows = rows.intValue(); |
|
for(Iterator i = config.getBranches(); i.hasNext(); ) { |
Branch branch = new Branch((ConfigBranch)i.next()); |
brnaches.put(branch.getUri(), branch); |
this.thumbnailer = new Thumbnailer(); |
this.thumbnailer.setImagesRoot(this.imagesRoot); |
this.thumbnailer.setCacheDir(new File(cacheDir)); |
|
if(thumbnailFormat != null) |
this.thumbnailer.setFormat(thumbnailFormat); |
if(smallWidth != null) |
this.thumbnailer.setSmallWidth(smallWidth.intValue()); |
if(smallHeight != null) |
this.thumbnailer.setSmallHeight(smallHeight.intValue()); |
if(mediumWidth != null) |
this.thumbnailer.setMediumWidth(mediumWidth.intValue()); |
if(mediumHeight != null) |
this.thumbnailer.setMediumHeight(mediumHeight.intValue()); |
|
thumbnailer.setResizer(new ak.photoalbum.images.jiu.JiuResizer()); |
thumbnailer.setImagesFilter(this.imagesFilter); |
thumbnailer.setDirTemplate(new File(dirTemplate)); |
thumbnailer.setDirThumbnailPositions( |
parseThumbnailPositions(dirThumbnailPositions)); |
|
try { |
thumbnailer.startup(); |
} |
|
catch(Exception ex) { |
logger.error("init thumbnailer", ex); |
} |
*/ |
logger.info("started"); |
} |
|
public Branch getBranch(String uri) |
{ |
return (Branch)branches.get(uri); |
} |
|
public void buildCache(String uri) |
public void buildCache() |
throws IOException |
{ |
getBranch(uri).getThumbnailer().buildCache(); |
thumbnailer.buildCache(); |
} |
|
public void getEntry(String uri, String path, IndexEntry page, |
public void getEntry(String path, IndexEntry page, |
IndexEntry index, IndexEntry prev, IndexEntry current, IndexEntry next) |
throws IOException, SAXException, LogicException |
{ |
Branch branch = getBranch(uri); |
File file = new File(branch.getImagesRoot(), path); |
File file = new File(imagesRoot, path); |
|
securePath(branch.getImagesRoot(), file); |
securePath(imagesRoot, file); |
|
if(!file.exists()) |
throw new FileNotFoundException( |
149,17 → 173,17 |
"[" + file.getCanonicalPath() + "] not found"); |
|
File dir = file.getParentFile(); |
File[] children = dir.listFiles(branch.getImagesFilter()); |
File[] children = dir.listFiles(imagesFilter); |
int pos; |
|
Arrays.sort(children, branch.getFileNameComparator()); |
pos = Arrays.binarySearch(children, file, branch.getFileNameComparator()); |
Arrays.sort(children, fileNameComparator); |
pos = Arrays.binarySearch(children, file, fileNameComparator); |
|
if(pos < 0) |
throw new FileNotFoundException("[" + file.getCanonicalPath() |
+ "] not found in [" + dir.getCanonicalPath() + "]"); |
|
branch.getMetaInfos().clear(); // FIXME make this more intelligent |
metaInfos.clear(); // FIXME make this more intelligent |
setEntryInfo(page, file, false); |
setEntryInfo(current, file, true); |
setEntryInfo(index, dir, true); |
167,7 → 191,7 |
if(pos < children.length-1) setEntryInfo(next, children[pos+1], true); |
} |
|
protected void setEntryInfo(Branch branch, IndexEntry entry, File file, boolean small) |
protected void setEntryInfo(IndexEntry entry, File file, boolean small) |
throws IOException, SAXException |
{ |
String title = file.getName(); |
175,15 → 199,15 |
String path = getPath(file); |
|
if(file.isDirectory()) { |
size = branch.getThumbnailer().getDirSize(file); |
size = thumbnailer.getDirSize(file); |
} |
else { |
title = FileUtils.extractFileName(title); |
|
if(small) |
size = branch.getThumbnailer().getSmallSize(file); |
size = thumbnailer.getSmallSize(file); |
else |
size = branch.getThumbnailer().getMediumSize(file); |
size = thumbnailer.getMediumSize(file); |
} |
|
entry.setFile(file); |
193,7 → 217,7 |
entry.setWidth(size[0]); |
entry.setHeight(size[1]); |
|
MetaInfoItem meta = findMetaInfo(branch.getImagesRoot(), file); |
MetaInfoItem meta = findMetaInfo(imagesRoot, file); |
if(meta != null) { |
if(meta.getTitle() != null) { |
entry.setTitle(meta.getTitle()); |
209,61 → 233,56 |
} |
} |
|
public String getThumbnailMime(String uri) |
public String getThumbnailMime() |
{ |
return getBranch(uri).getThumbnailer().getMime(); |
return thumbnailer.getMime(); |
} |
|
public String getOriginMime(String uri, String path) |
public String getOriginMime(String path) |
throws IOException, LogicException |
{ |
Branch branch = getBranch(uri); |
File file = new File(branch.getImagesRoot(), path); |
File file = new File(imagesRoot, path); |
|
if(!file.exists()) return null; |
securePath(branch.getImagesRoot(), file); |
securePath(imagesRoot, file); |
|
return FileUtils.getMime(FileUtils.extractFileExt(path)); |
} |
|
public void writeDir(String uri, String path, OutputStream out) |
public void writeDir(String path, OutputStream out) |
throws IOException, LogicException |
{ |
Branch branch = getBranch(uri); |
File file = new File(branch.getImagesRoot(), path); |
File file = new File(imagesRoot, path); |
|
securePath(branch.getImagesRoot(), file); |
branch.getThumbnailer().writeDir(file, out); |
securePath(imagesRoot, file); |
thumbnailer.writeDir(file, out); |
} |
|
public void writeSmall(String uri, String path, OutputStream out) |
public void writeSmall(String path, OutputStream out) |
throws IOException, LogicException |
{ |
Branch branch = getBranch(uri); |
File file = new File(branch.getImagesRoot(), path); |
File file = new File(imagesRoot, path); |
|
securePath(branch.getImagesRoot(), file); |
branch.getThumbnailer().writeSmall(file, out); |
securePath(imagesRoot, file); |
thumbnailer.writeSmall(file, out); |
} |
|
public void writeMedium(String uri, String path, OutputStream out) |
public void writeMedium(String path, OutputStream out) |
throws IOException, LogicException |
{ |
Branch branch = getBranch(uri); |
File file = new File(branch.getImagesRoot(), path); |
File file = new File(imagesRoot, path); |
|
securePath(branch.getImagesRoot(), file); |
branch.getThumbnailer().writeMedium(file, out); |
securePath(imagesRoot, file); |
thumbnailer.writeMedium(file, out); |
} |
|
public void writeOrigin(String uri, String path, OutputStream out) |
public void writeOrigin(String path, OutputStream out) |
throws IOException, LogicException |
{ |
Branch branch = getBranch(uri); |
FileInputStream in = null; |
File file = new File(branch.getImagesRoot(), path); |
FileInputStream in = null; |
File file = new File(imagesRoot, path); |
|
securePath(branch.getImagesRoot(), file); |
securePath(imagesRoot, file); |
|
try { |
in = new FileInputStream(file); |
274,10 → 293,10 |
} |
} |
|
protected MetaInfo getMetaInfo(Branch branch, File dir) |
protected MetaInfo getMetaInfo(File dir) |
throws IOException, SAXException |
{ |
MetaInfo meta = (MetaInfo)branch.getMetaInfos().get(dir); |
MetaInfo meta = (MetaInfo)metaInfos.get(dir); |
if(meta != null) return meta; |
|
File metaFile = new File(dir, META_FILE_NAME); |
285,7 → 304,7 |
|
meta = (MetaInfo)metaDigester.parse(new FileInputStream(metaFile)); |
meta.setDir(dir); |
branch.getMetaInfos().put(dir, meta); |
metaInfos.put(dir, meta); |
|
return meta; |
} |
314,45 → 333,44 |
return metaItem; |
} |
|
public boolean listDirectory(String uri, String dirName, int page, List table, List pages) |
public boolean listDirectory(String dirName, int page, List table, List pages) |
throws IOException, LogicException, SAXException |
{ |
Branch branch = getBranch(uri); |
File dir = new File(branch.getImagesRoot(), dirName); |
File dir = new File(imagesRoot, dirName); |
|
securePath(branch.getImagesRoot(), dir); |
securePath(imagesRoot, dir); |
if(!dir.exists()) return false; |
|
File[] children = dir.listFiles(branch.getImagesFilter()); |
int pos = page * branch.getColumns() * branch.getRows(); |
File[] children = dir.listFiles(imagesFilter); |
int pos = page * columns * rows; |
|
Arrays.sort(children, branch.getFileNameComparator()); |
branch.getMetaInfos().clear(); // FIXME do this more intelligent (?) |
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 / branch.getColumns() / branch.getRows()); i++) { |
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) * branch.getColumns() * branch.getRows()) { |
while(pos < children.length && pos < (page+1) * columns * rows) { |
List row = new ArrayList(); |
int rowPos = 0; |
|
table.add(row); |
|
while(rowPos < branch.getColumns() && pos < children.length) { |
while(rowPos < columns && pos < children.length) { |
String path = getPath(children[pos]); |
String title = children[pos].getName(); |
int[] size; |
|
if(children[pos].isDirectory()) { |
size = branch.getThumbnailer().getDirSize(children[pos]); |
size = thumbnailer.getDirSize(children[pos]); |
} |
else { |
size = branch.getThumbnailer().getSmallSize(children[pos]); |
size = thumbnailer.getSmallSize(children[pos]); |
title = FileUtils.extractFileName(title); |
} |
|
360,7 → 378,7 |
URLEncoder.encode(path, URL_ENCODING), |
title, children[pos].isDirectory(), size[0], size[1]); |
|
MetaInfoItem meta = findMetaInfo(branch.getImagesRoot(), children[pos]); |
MetaInfoItem meta = findMetaInfo(imagesRoot, children[pos]); |
if(meta != null) { |
if(meta.getTitle() != null) { |
entry.setTitle(meta.getTitle()); |
380,7 → 398,7 |
pos++; |
} |
|
while(rowPos < branch.getColumns()) { |
while(rowPos < columns) { |
row.add(null); |
rowPos++; |
} |
389,11 → 407,11 |
return true; |
} |
|
protected String getPath(Branch branch, File file) |
protected String getPath(File file) |
throws IOException |
{ |
String path = file.getCanonicalPath(); |
String rootPath = branch.getImagesRoot().getCanonicalPath(); |
String rootPath = imagesRoot.getCanonicalPath(); |
|
if(path.equals(rootPath)) return ""; |
if(!rootPath.endsWith(File.separator)) rootPath += File.separator; |