Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1248 → Rev 1249

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