?? photoaction.java
字號:
/*
* PhotoAction.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Author: Winter Lau (javayou@gmail.com)
* http://dlog4j.sourceforge.net
*
*/
package com.liusoft.dlog4j.action;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import com.liusoft.dlog4j.DLOGSecurityManager;
import com.liusoft.dlog4j.Globals;
import com.liusoft.dlog4j.HtmlNodeFilters;
import com.liusoft.dlog4j.HttpContext;
import com.liusoft.dlog4j.MailTransportQueue;
import com.liusoft.dlog4j.SessionUserObject;
import com.liusoft.dlog4j.base._PhotoBase;
import com.liusoft.dlog4j.beans.AlbumBean;
import com.liusoft.dlog4j.beans.PhotoBean;
import com.liusoft.dlog4j.beans.PhotoOutlineBean;
import com.liusoft.dlog4j.beans.SiteBean;
import com.liusoft.dlog4j.beans.UserBean;
import com.liusoft.dlog4j.dao.AlbumDAO;
import com.liusoft.dlog4j.dao.PhotoDAO;
import com.liusoft.dlog4j.formbean.PhotoForm;
import com.liusoft.dlog4j.photo.FileSystemSaver;
import com.liusoft.dlog4j.photo.Photo;
import com.liusoft.dlog4j.photo.PhotoSaver;
import com.liusoft.dlog4j.search.SearchProxy;
import com.liusoft.dlog4j.util.DLOG4JUtils;
import com.liusoft.dlog4j.util.MailSender;
import com.liusoft.dlog4j.util.RequestUtils;
import com.liusoft.dlog4j.util.StringUtils;
/**
* 相片Action(只有站長才能對相冊進行操作)
* @author Winter Lau
*/
public class PhotoAction extends AdminActionBase {
private final static Log log = LogFactory.getLog(PhotoAction.class);
private final static String TMP_PHOTO_SHARE = "/WEB-INF/conf/photo_share.html";
private final static String photo_saver_class = "photo_saver_class";
private final static String ERROR_KEY = "upload";
private final static int MAX_MAIL_COUNT = 10;
/**
* 圖片向左旋轉90°
* @param mapping
* @param form
* @param request
* @param response
* @param s_photo_id
* @return
* @throws Exception
*/
protected ActionForward doRotateLeft(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response, String s_photo_id) throws Exception
{
int photo_id = Integer.parseInt(s_photo_id);
_PhotoBase pbean = PhotoDAO.getPhotoOutlineByID(photo_id);
if(pbean != null){
HttpContext http_ctx = getHttpContext(mapping, form, request, response);
rotate(http_ctx, pbean.getImageURL(), 8);
}
return null;
}
/**
* 圖片向右旋轉90°
* @param mapping
* @param form
* @param request
* @param response
* @param s_photo_id
* @return
* @throws Exception
*/
protected ActionForward doRotateRight(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response, String s_photo_id) throws Exception
{
int photo_id = Integer.parseInt(s_photo_id);
_PhotoBase pbean = PhotoDAO.getPhotoOutlineByID(photo_id);
if(pbean != null){
HttpContext http_ctx = getHttpContext(mapping, form, request, response);
rotate(http_ctx, pbean.getImageURL(), 6);
}
return null;
}
/**
* 照片旋轉
* @param ctx
* @param imgURL
* @param orient
* @return
* @throws IOException
*/
protected boolean rotate(HttpContext ctx, String imgURL, int orient) throws IOException{
PhotoSaver saver = this.getPhotoSaver();
InputStream inImg = saver.read(ctx, imgURL);
BufferedImage old_img = (BufferedImage)ImageIO.read(inImg);
int width = old_img.getWidth();
int height = old_img.getHeight();
BufferedImage new_img = new BufferedImage(height,width,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d =new_img.createGraphics();
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform)(origXform.clone());
// center of rotation is center of the panel
double radian = 0;
double xRot = 0;
double yRot = 0;
switch(orient){
case 3:
radian = 180.0;
xRot = width/2.0;
yRot = height/2.0;
case 6:
radian = 90.0;
xRot = height/2.0;
yRot = xRot;
break;
case 8:
radian = 270.0;
xRot = width/2.0;
yRot = xRot;
break;
default:
return false;
}
newXform.rotate(Math.toRadians(radian), xRot, yRot);
g2d.setTransform(newXform);
// draw image centered in panel
g2d.drawImage(old_img, 0, 0, null);
// Reset to Original
g2d.setTransform(origXform);
OutputStream out = saver.write(ctx, imgURL);
try{
ImageIO.write(new_img, "JPG", out);
}finally{
out.close();
}
return true;
}
/**
* 推薦照片
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
protected ActionForward doRecommend(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response, String s_photo_id) throws Exception
{
PhotoForm pform = (PhotoForm)form;
super.validateClientId(request, pform);
ActionMessages msgs = new ActionMessages();
do{
SessionUserObject loginUser = super.getLoginUser(request,response,false);
if(loginUser==null){
msgs.add("photo", new ActionMessage("error.user_not_login"));
break;
}
PhotoOutlineBean pbean = PhotoDAO.getPhotoOutlineByID(pform.getId());
if(pbean==null||pbean.getStatus()!=PhotoBean.STATUS_NORMAL){
msgs.add("photo", new ActionMessage("error.invalid_photo", new Integer(pform.getId())));
break;
}
//發送郵件
String s_mails = request.getParameter("mails");
String[] emails = StringUtils.split(s_mails, ";",MAX_MAIL_COUNT);
if(emails==null || emails.length==0){
msgs.add("photo", new ActionMessage("error.no_share_mails"));
break;
}
List vtMails = new ArrayList();
for(int i=0;i<emails.length;i++){
String sm = emails[i].trim();
if(StringUtils.isEmail(sm) && !vtMails.contains(sm))
vtMails.add(sm);
}
if(vtMails.size()==0){
msgs.add("photo", new ActionMessage("error.no_valid_mail"));
break;
}
sendMails(request, loginUser, pbean, vtMails);
msgs.add("share", new ActionMessage("mail.sent"));
break;
}while(true);
if(!msgs.isEmpty())
saveMessages(request, msgs);
return mapping.findForward("photo_share");
}
/**
* 發送新評論郵件提醒
*
* @param request
* @param rbean
* @throws Exception
*/
protected void sendMails(final HttpServletRequest request,
final SessionUserObject loginUser, final _PhotoBase pbean, final List mails)
throws Exception {
final String contextPath = request.getContextPath();
final String urlPrefix = RequestUtils.getUrlPrefix(request);
final String template = super.getTemplate(TMP_PHOTO_SHARE);
new Thread() {
public void run() {
StringBuffer url = new StringBuffer();
url.append(urlPrefix);
url.append(contextPath);
url.append("/html/photo/show.vm?sid=");
url.append(pbean.getSite().getId());
url.append("&pid=");
url.append(pbean.getId());
String curTime = new SimpleDateFormat("yyyy-MM-dd HH:mm")
.format(new Date());
StringBuffer img = new StringBuffer();
img.append(urlPrefix);
img.append(contextPath);
img.append(pbean.getPreviewURL());
String[] s_mails = new String[mails.size()];
mails.toArray(s_mails);
try {
// 發送郵件
String mail_content = MessageFormat.format(template,
new String[]{loginUser.getNickname(),img.toString(), url.toString(),curTime});
//System.out.println(mail_content);
Parser html = new Parser();
html.setEncoding(Globals.ENC_8859_1);
html.setInputHTML(mail_content);
Node[] nodes = html.extractAllNodesThatMatch(
HtmlNodeFilters.titleFilter).toNodeArray();
String title = nodes[0].toPlainTextString();
MailSender sender = MailSender.getHtmlMailSender(null, 25,
null, null);
sender.setSubject(title);
sender.setSendDate(new Date());
sender.setMailContent(mail_content);
sender.setMailTo(s_mails, "to");
MailTransportQueue queue = (MailTransportQueue) getServlet()
.getServletContext().getAttribute(
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -