⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attachmentcommon.java

📁 个人认为是最好的Java论坛源码
💻 JAVA
字号:
/*
 * Copyright (c) 2003, Rafael Steil
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above 
 * copyright notice, this list of conditions and the 
 * following  disclaimer.
 * 2)  Redistributions in binary form must reproduce the 
 * above copyright notice, this list of conditions and 
 * the following disclaimer in the documentation and/or 
 * other materials provided with the distribution.
 * 3) Neither the name of "Rafael Steil" nor 
 * the names of its contributors may be used to endorse 
 * or promote products derived from this software without 
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 * 
 * Created on Jan 18, 2005 3:08:48 PM
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.view.forum.common;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.jforum.ActionServletRequest;
import net.jforum.SessionFacade;
import net.jforum.entities.Attachment;
import net.jforum.entities.AttachmentExtension;
import net.jforum.entities.AttachmentInfo;
import net.jforum.entities.Group;
import net.jforum.entities.QuotaLimit;
import net.jforum.entities.User;
import net.jforum.exceptions.AttachmentSizeTooBigException;
import net.jforum.exceptions.BadExtensionException;
import net.jforum.model.AttachmentModel;
import net.jforum.model.DataAccessDriver;
import net.jforum.repository.SecurityRepository;
import net.jforum.security.SecurityConstants;
import net.jforum.util.I18n;
import net.jforum.util.MD5;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

/**
 * @author Rafael Steil
 * @version $Id: AttachmentCommon.java,v 1.14 2005/02/24 15:32:00 rafaelsteil Exp $
 */
public class AttachmentCommon
{
	private static Logger logger = Logger.getLogger(AttachmentCommon.class);
	
	private ActionServletRequest request;
	private AttachmentModel am;
	
	public AttachmentCommon(ActionServletRequest request)
	{
		this.request = request;
		this.am = DataAccessDriver.getInstance().newAttachmentModel();
	}
	
	public void insertAttachments(int postId, int forumId) throws Exception
	{
		if (!SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, 
				Integer.toString(forumId))) {
			return;
		}
		
		String t = this.request.getParameter("total_files");
		if (t == null || "".equals(t)) {
			return;
		}
		
		int total = Integer.parseInt(t);
		if (total < 1) {
			return;
		}
		
		if (total > SystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_MAX_POST)) {
			total = SystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_MAX_POST);
		}

		Map filesToSave = new HashMap();
		long totalSize = 0;
		int userId = SessionFacade.getUserSession().getUserId();
		Map extensions = this.am.extensionsForSecurity();
		
		for (int i = 0; i < total; i++) {
			FileItem item = (FileItem)this.request.getObjectParameter("file_" + i);
			if (item == null) {
				continue;
			}

			if (item.getName().indexOf('\000') > -1) {
				logger.warn("Possible bad attachment (null char): " + item.getName()
						+ " - user_id: " + SessionFacade.getUserSession().getUserId());
				continue;
			}
			
			UploadUtils uploadUtils = new UploadUtils(item, this.request);
			
			// Check if the extension is allowed
			if (extensions.containsKey(uploadUtils.getExtension())) {
				if (!((Boolean)extensions.get(uploadUtils.getExtension())).booleanValue()) {
					throw new BadExtensionException(I18n.getMessage("Attachments.badExtension", 
							new String[] { uploadUtils.getExtension() }));
				}
			}
			
			Attachment a = new Attachment();
			a.setPostId(postId);
			a.setUserId(userId);
			
			AttachmentInfo info = new AttachmentInfo();
			info.setFilesize(item.getSize());
			info.setComment(this.request.getParameter("comment_" + i));
			info.setMimetype(item.getContentType());
			
			// Get only the filename, without the path (IE does that)
			String realName = item.getName();
			String separator = "/";
			int index = realName.indexOf(separator);
			
			if (index == -1) {
				separator = "\\";
				index = realName.indexOf(separator);
			}
			
			if (index > -1) {
				if (separator.equals("\\")) {
					separator = "\\\\";
				}
				
				String[] p = realName.split(separator);
				realName = p[p.length - 1];
			}
			
			info.setRealFilename(realName);
			info.setUploadTimeInMillis(System.currentTimeMillis());
			
			AttachmentExtension ext = this.am.selectExtension(uploadUtils.getExtension().toLowerCase());
			if (ext.isUnknown()) {
				ext.setExtension(uploadUtils.getExtension());
			}
			
			info.setExtension(ext);
			String savePath = this.makeStoreFilename(info);
			info.setPhysicalFilename(savePath);
			
			a.setInfo(info);
			filesToSave.put(uploadUtils, a);
			
			totalSize += item.getSize();
		}
		
		// Check upload limits
		QuotaLimit ql = this.getQuotaLimit(userId);
		if (ql != null) {
			if (ql.exceedsQuota(totalSize)) {
				throw new AttachmentSizeTooBigException(I18n.getMessage("Attachments.tooBig", 
						new Integer[] { new Integer(ql.getSizeInBytes() / 1024), 
							new Integer((int)totalSize / 1024) }));
			}
		}
		
		for (Iterator iter = filesToSave.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry entry = (Map.Entry)iter.next();
			Attachment a = (Attachment)entry.getValue();
			String path = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) 
				+ "/" 
				+ a.getInfo().getPhysicalFilename();
			
			this.am.addAttachment(a);
			((UploadUtils)entry.getKey()).saveUploadedFile(path);
		}
	}
	
	public QuotaLimit getQuotaLimit(int userId) throws Exception
	{
		QuotaLimit ql = new QuotaLimit();
		User u = DataAccessDriver.getInstance().newUserModel().selectById(userId);
		
		for (Iterator iter = u.getGroupsList().iterator(); iter.hasNext();) {
			QuotaLimit l = this.am.selectQuotaLimitByGroup(((Group)iter.next()).getId());
			if (l == null) {
				continue;
			}
			
			if (l.getSizeInBytes() > ql.getSizeInBytes()) {
				ql = l;
			}
		}
		
		if (ql.getSize() == 0) {
			return null;
		}
		
		return ql;
	}
	
	public void editAttachments(int postId, int forumId) throws Exception
	{
		// Allow removing the attachments at least
		AttachmentModel am = DataAccessDriver.getInstance().newAttachmentModel();
		
		// Check for attachments to remove
		List deleteList = new ArrayList();
		String[] delete = null;
		String s = this.request.getParameter("delete_attach");
		
		if (s != null) {
			delete = s.split(",");
		}
		
		if (delete != null) {
			for (int i = 0; i < delete.length; i++) {
				if (delete[i] != null && !delete[i].equals("")) {
					int id = Integer.parseInt(delete[i]);
					Attachment a = am.selectAttachmentById(id);
					
					am.removeAttachment(id, postId);
					
					File f = new File(SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR)
							+ "/" + a.getInfo().getPhysicalFilename());
					if (f.exists()) {
						f.delete();
					}
				}
			}
			
			deleteList = Arrays.asList(delete);
		}
		
		if (!SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, 
				Integer.toString(forumId))
				&& !SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD)) {
			return;
		}
		
		// Update
		String[] attachIds = null;
		s = this.request.getParameter("edit_attach_ids");
		if (s != null) {
			attachIds = s.split(",");
		}
		
		if (attachIds != null) {
			for (int i = 0; i < attachIds.length; i++) {
				if (deleteList.contains(attachIds[i]) 
						|| attachIds[i] == null || attachIds[i].equals("")) {
					continue;
				}
				
				int id = Integer.parseInt(attachIds[i]);
				Attachment a = am.selectAttachmentById(id);
				a.getInfo().setComment(this.request.getParameter("edit_comment_" + id));

				am.updateAttachment(a);
			}
		}
	}
	
	private String makeStoreFilename(AttachmentInfo a)
	{
		Calendar c = new GregorianCalendar();
		c.setTimeInMillis(System.currentTimeMillis());
		c.get(Calendar.YEAR);
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		
		String dir = "" + year + "/" + month + "/" + day + "/";
		new File(SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + dir).mkdirs();
		
		return dir
			+ MD5.crypt(a.getRealFilename() + a.getUploadTime()) 
			+ "_" + SessionFacade.getUserSession().getUserId()
			+ "." + a.getExtension().getExtension();
	}
	
	public List getAttachments(int postId, int forumId) throws Exception
	{
		if (!SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD)
				&& !SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, 
						Integer.toString(forumId))) {
			return new ArrayList();
		}
		
		return this.am.selectAttachments(postId);
	}
	
	public void deleteAttachments(int postId, int forumId) throws Exception
	{
		// Attachments
		List attachments = DataAccessDriver.getInstance().newAttachmentModel().selectAttachments(postId);
		String attachIds = "";
		for (Iterator iter = attachments.iterator(); iter.hasNext(); ) {
			Attachment a = (Attachment)iter.next();
			attachIds += a.getId() + ",";
		}
		
		this.request.addParameter("delete_attach", attachIds);
		this.editAttachments(postId, forumId);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -