Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1248 → Rev 1249

/PhotoAlbum/trunk/src/ak/photoalbum/config/ConfigRoot.java
1,7 → 1,7
package ak.photoalbum.config;
 
import java.util.Map;
import java.util.Hashtable;
import java.util.List;
import java.util.ArrayList;
import org.apache.log4j.Logger;
 
public class ConfigRoot
8,7 → 8,7
{
private static final Logger logger = Logger.getLogger(ConfigRoot.class);
 
private Map branches = new Hashtable(); // <String, ConfigBranch>
private List branches = new ArrayList();
 
public ConfigRoot()
{
17,12 → 17,12
public void addBranch(ConfigBranch branch)
{
logger.info("add branch " + branch.getUri());
branches.put(branch.getUri(), branch);
branches.add(branch);
}
 
public ConfigBranch getBranch(String uri)
public List getBranches()
{
return (ConfigBranch)branches.get(uri);
return branches;
}
}
 
/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
/PhotoAlbum/trunk/src/ak/photoalbum/util/ImagesFilter.java
2,9 → 2,10
 
import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.Iterator;
 
public class ImagesFilter
implements FileFilter
11,23 → 12,24
{
Set extentions = new HashSet();
 
public ImagesFilter(String imagesMask)
public ImagesFilter(List imagesMasks)
{
StringTokenizer tokenizer = new StringTokenizer(imagesMask, ";");
for(Iterator i = imagesMasks.iterator(); i.hasNext(); ) {
String mask = (String)i.next();
 
while(tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if(mask.startsWith("*.")) mask = mask.substring(2);
 
if(token.startsWith("*.")) token = token.substring(2);
 
extentions.add(token.toLowerCase());
extentions.add(mask.toLowerCase());
}
}
 
public boolean accept(File pathname)
{
if(pathname.getName().startsWith(".")) // skip hidden
return false;
 
if(pathname.isDirectory()) {
return !pathname.getName().startsWith("."); // skip hidden dirs
return true;
}
else{
return extentions.contains(FileUtils.extractFileExt(
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/PageItem.java
File deleted
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/ImageAction.java
7,6 → 7,7
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.log4j.Logger;
import ak.photoalbum.logic.Logic;
 
public final class ImageAction
extends BaseAction
18,6 → 19,7
throws Exception
{
PathForm theForm = (PathForm)form;
String branch = theForm.getBranch();
String path = theForm.getPath();
Logic logic = Logic.getLogic();
 
24,20 → 26,20
logger.info("get image " + mapping.getParameter() + " for " + path);
 
if("dir".equals(mapping.getParameter())) {
response.setContentType(logic.getThumbnailMime());
logic.writeDir(path, response.getOutputStream());
response.setContentType(logic.getThumbnailMime(branch));
logic.writeDir(branch, path, response.getOutputStream());
}
else if("small".equals(mapping.getParameter())) {
response.setContentType(logic.getThumbnailMime());
logic.writeSmall(path, response.getOutputStream());
response.setContentType(logic.getThumbnailMime(branch));
logic.writeSmall(branch, path, response.getOutputStream());
}
else if("medium".equals(mapping.getParameter())) {
response.setContentType(logic.getThumbnailMime());
logic.writeMedium(path, response.getOutputStream());
response.setContentType(logic.getThumbnailMime(branch));
logic.writeMedium(branch, path, response.getOutputStream());
}
else if("origin".equals(mapping.getParameter())) {
response.setContentType(logic.getOriginMime(path));
logic.writeOrigin(path, response.getOutputStream());
response.setContentType(logic.getOriginMime(branch, path));
logic.writeOrigin(branch, path, response.getOutputStream());
}
 
return null;
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/PathForm.java
9,8 → 9,19
public class PathForm
extends ActionForm
{
protected String branch;
protected String path;
 
public String getBranch()
{
return branch;
}
 
public void setBranch(String branch)
{
this.branch = branch;
}
 
public String getPath()
{
return path;
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/BuildCacheAction.java
8,6 → 8,7
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.log4j.Logger;
import ak.photoalbum.logic.Logic;
 
public final class BuildCacheAction
extends Action
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/ServletResourceFactory.java
2,6 → 2,7
 
import java.io.InputStream;
import javax.servlet.ServletContext;
import ak.photoalbum.logic.ResourceFactory;
 
public class ServletResourceFactory
implements ResourceFactory
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/IndexAction.java
9,6 → 9,8
import org.apache.struts.action.ActionForward;
import org.apache.log4j.Logger;
import ak.photoalbum.util.FileUtils;
import ak.photoalbum.logic.Logic;
import ak.photoalbum.logic.IndexEntry;
 
public final class IndexAction
extends BaseAction
20,6 → 22,7
throws Exception
{
IndexForm theForm = (IndexForm)form;
String branch = theForm.getBranch();
String dir = theForm.getPath();
int page = theForm.getPageInt();
IndexEntry entry = new IndexEntry();
33,8 → 36,8
if(dir == null) dir = ""; // the images root
 
logger.info("get index for " + dir);
Logic.getLogic().getEntry(dir, entry, top, prev, current, next);
if(!Logic.getLogic().listDirectory(dir, page, index, pages))
Logic.getLogic().getEntry(branch, dir, entry, top, prev, current, next);
if(!Logic.getLogic().listDirectory(branch, dir, page, index, pages))
return mapping.findForward("error");
 
request.setAttribute("dir", FileUtils.replaceFileSeparator(dir, " - "));
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/BaseAction.java
10,6 → 10,7
import org.apache.struts.action.ActionForward;
import org.apache.log4j.Logger;
import ak.photoalbum.util.FileUtils;
import ak.photoalbum.logic.LogicSecurityException;
 
public abstract class BaseAction
extends Action
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/InitServlet.java
6,6 → 6,7
import javax.servlet.ServletResponse;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import ak.photoalbum.logic.Logic;
 
public class InitServlet
extends GenericServlet
/PhotoAlbum/trunk/src/ak/photoalbum/webapp/PageAction.java
6,6 → 6,8
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.log4j.Logger;
import ak.photoalbum.logic.Logic;
import ak.photoalbum.logic.IndexEntry;
 
public final class PageAction
extends BaseAction
17,6 → 19,7
throws Exception
{
PathForm theForm = (PathForm)form;
String branch = theForm.getBranch();
String page = theForm.getPath();
IndexEntry entry = new IndexEntry();
IndexEntry index = new IndexEntry();
27,7 → 30,7
if(page == null) page = ""; // the images root
 
logger.info("get page " + page);
Logic.getLogic().getEntry(page, entry, index, prev, current, next);
Logic.getLogic().getEntry(branch, page, entry, index, prev, current, next);
 
request.setAttribute("page", page);
request.setAttribute("entry", entry);