<%@ page contentType="text/html; charset=UTF-8"%> <%@ page import="java.io.File"%> <%@page import="java.io.FileFilter"%> <%@page import="java.awt.image.BufferedImage"%> <%@page import="java.awt.Graphics2D"%> <%@page import="javax.imageio.ImageIO"%> <%@page import="java.util.Arrays"%> <%@page import="java.util.Comparator"%> <%@page import="java.util.Vector"%> <% final String imageExtensions[] = { ".GIF", ".JPG", ".PNG" }; final String movieExtensions[] = { ".MOV", ".MP4", ".MPG", ".AVI" }; final String hiddenFolders[] = { ".svn", "META-INF", "WEB-INF", "cache", "private", "thumbs", "thumb", "source", "documents", "xml", "tiles" }; final String forbiddenTokens[] = { ".", "~", ":" }; String location=request.getParameter("l"); if (location == null) location = ""; if (!location.endsWith("/")) location += "/"; // some basic security checks for (int i = 0; i < forbiddenTokens.length; i++) { if (location.indexOf(forbiddenTokens[i]) > -1) return; } String imageName=request.getParameter("i"); String subPath = request.getServletPath(); int pe = subPath.lastIndexOf('/', subPath.length()-2); subPath = (pe > 0) ? subPath.substring(0, pe) : ""; String relPath = request.getContextPath() + subPath; String absPath = request.getSession().getServletContext().getRealPath(subPath+location); File dir = new File(absPath); %> Photos - <%= location %>

<%=location %>

<% File files[] = dir.listFiles(new FileFilter() { public boolean accept(File file) { // assert not a directory if (file.isDirectory()) return false; for (int i = 0; i < imageExtensions.length; i++) { if (file.getName().toUpperCase().endsWith(imageExtensions[i])) { return true; } } for (int i = 0; i < movieExtensions.length; i++) { if (file.getName().toUpperCase().endsWith(movieExtensions[i])) { return true; } } return false; } }); Arrays.sort(files, new Comparator() { public int compare(File f1, File f2) { return f1.getName().compareTo(f2.getName()); } }); %> <% if (location.length() > 1) { pe = location.lastIndexOf('/', location.length()-2); String parent = (pe > 0) ? location.substring(0, pe+1) : "/"; if (imageName != null) { parent = location; } String target = relPath + "/photo.jsp?l="+parent; %>
<% } if ((imageName != null) && (files.length > 0)) { String next = relPath + "/photo.jsp?l="+location+"&i="+files[0].getName(); for (int i = 0; i < files.length-1; i++) { if (imageName.equals(files[i].getName())) { next = relPath + "/photo.jsp?l="+location+"&i="+files[i+1].getName(); } } boolean movie = false; for (int j = 0; j < movieExtensions.length; j++) { if (imageName.toUpperCase().endsWith(movieExtensions[j])) { movie = true; } } if (!movie) { %>

<%=imageName %>

<%= imageName %>

<% } else { %>

" alt="<%=imageName %>" /> Next

<%= imageName %>

<% } } else { File dirs[] = dir.listFiles(); Arrays.sort(dirs, new Comparator() { public int compare(File f1, File f2) { return f1.getName().compareTo(f2.getName()); } }); for (int i = 0; i < dirs.length; i++) { if (dirs[i].isDirectory()) { boolean hidden = false; for (int j = 0; j < hiddenFolders.length; j++) { if (hiddenFolders[j].equals(dirs[i].getName())) { hidden = true; break; } } if (hidden) continue; String target = relPath + "/photo.jsp?l="+location+dirs[i].getName(); %> <% } } %>
 
<% boolean shownViewerLink = false; Vector workspace = new Vector(); for (int i = 0; i < files.length; i++) { boolean movie = false; for (int j = 0; j < movieExtensions.length; j++) { if (files[i].getName().toUpperCase().endsWith(movieExtensions[j])) { movie = true; } } try { String cacheName = relPath + "/cache/" + (location+files[i].getName()).hashCode() + ".jpg"; if (movie) cacheName = "images/movie.gif"; String target = relPath + "/photo.jsp?l="+location+"&i="+files[i].getName(); if (!movie) { File cacheFile = new File(request.getSession().getServletContext().getRealPath(subPath + "/cache/" + (location+files[i].getName()).hashCode() + ".jpg")); if (!cacheFile.exists() || cacheFile.lastModified() < files[i].lastModified()) { BufferedImage image = ImageIO.read(files[i]); int thumbWidth = 240; int thumbHeight = 180; int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); float scaleFactor = (imageWidth > imageHeight) ? (float)thumbWidth / (float)imageWidth : (float)thumbHeight / (float)imageHeight; thumbWidth = (int)(imageWidth * scaleFactor); thumbHeight = (int)(imageHeight * scaleFactor); BufferedImage zimage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = zimage.createGraphics(); g.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); g.dispose(); ImageIO.write(zimage, "jpg", cacheFile); } } workspace.add(files[i].getName()); %> <% if (!shownViewerLink) { shownViewerLink = true; %> <% } } catch (Exception e) {e.printStackTrace();} } session.setAttribute("workspace", workspace); session.setAttribute("root", relPath); session.setAttribute("site", relPath+"/"+location); } %>