ImageFileRegistAction.java 8.38 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**
 *
 */
package jp.agentec.sinaburocast.action.admin.setting;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.annotation.Resource;

import jp.agentec.sinaburocast.action.AbstractAction;
import jp.agentec.sinaburocast.common.SinaburoConstant;
import jp.agentec.sinaburocast.common.annotation.OperationHistoryAno;
import jp.agentec.sinaburocast.common.io.FileUtil;
import jp.agentec.sinaburocast.common.util.PropertyUtil;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.common.util.SinaburoViewUtil;
import jp.agentec.sinaburocast.entity.FileManage;
import jp.agentec.sinaburocast.form.admin.setting.ImageFileRegistForm;
import jp.agentec.sinaburocast.service.FileManageService;

import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.log4j.Logger;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.TokenProcessor;
import org.seasar.framework.util.StringUtil;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.upload.S2MultipartRequestHandler;

/**
 * 画像・ドキュメントファイル一覧画面
 * @author arakawa
 *
 */
public class ImageFileRegistAction extends AbstractAction {
	private final Logger logger = Logger.getLogger(getClass());

	@ActionForm
	@Resource
	public ImageFileRegistForm imageFileRegistForm;

	public FileManageService fileManageService;

	/**
	 * 画像・ドキュメントファイル一覧画面 初期表示
	 */
	@Execute(validator = false)
	public String index() {
		SizeLimitExceededException ex = (SizeLimitExceededException) request.getAttribute(S2MultipartRequestHandler.SIZE_EXCEPTION_KEY);
		if(ex != null) {
			ActionMessages messages = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
			if (messages == null) {
				messages = new ActionMessages();
			}
			messages.add("file", new ActionMessage(SinaburoViewUtil.getMessage("errors.E048")));
			request.setAttribute(Globals.ERROR_KEY, messages);

			return "/admin/setting/imageFileRegist.html";
		}

		imageFileSearch();

		return "/admin/setting/imageFileSearch.html";
	}

	/**
	 * 画像・ドキュメントファイル一覧画面 ページング
	 */
	@Execute(validator = false,urlPattern="fileSearch/{pageNo}")
	public String fileSearch() {

		imageFileSearch();

		return "/admin/setting/imageFileSearch.html";
	}

	/**
	 * 画像・ドキュメントファイル削除
	 * @return
	 */
	@Execute(validator = false )
	@OperationHistoryAno(operationTypeId=SinaburoConstant.OperationType.DELETE,target="ファイル管理",detail="削除",screenId=SinaburoConstant.screenId.COS0002)
	public String fileDelete() throws Exception {

		FileManage fileManage = fileManageService.findById(Integer.parseInt(imageFileRegistForm.fileManageId));
		if (fileManage == null){
			request.setAttribute(SinaburoConstant.notRegistOperationHistoryKey.notRegist, SinaburoConstant.notRegistOperationHistoryKey.notRegist);
			ActionMessages messages = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
			if (messages == null) {
				messages = new ActionMessages();
			}
			messages.add("file", new ActionMessage(SinaburoViewUtil.getMessage("errors.E007")));
			request.setAttribute(Globals.ERROR_KEY, messages);

			return "/admin/setting/imageFileSearch.html";
		}

		//ファイル名を取得
		String filePath = PropertyUtil.getProperty(SinaburoConstant.filePath.IMG_UPLOAD_FILE_PATH) + fileManage.fileName;

		//ファイル削除
		FileUtil.delete(filePath);

		//DBレコード削除
		fileManageService.fileDelete(imageFileRegistForm.fileManageId,getAdmin());

		return "/admin/setting/imageFileRegist/";

	}

	/** 画像・ドキュメントファイルを検索する */
	private void imageFileSearch(){

		imageFileRegistForm.searchResultList = fileManageService.findAllByPaging(imageFileRegistForm);

	}

	/**
	 * 画像・ドキュメントファイルアップロード初期表示
	 */
	@Execute(validator = false)
	public String imageFileRegist() {

		TokenProcessor.getInstance().saveToken(request);

		return "/admin/setting/imageFileRegist.html";
	}

	/**
	 * 画像・ドキュメントファイルアップロード
	 * @throws Exception
	 */
	@Execute(validator = false)
	@OperationHistoryAno(operationTypeId=SinaburoConstant.OperationType.UPDATE,target="ファイル管理",detail="登録",screenId=SinaburoConstant.screenId.COS0003)
	public String imageFileUpload() throws Exception {

		SizeLimitExceededException ex = (SizeLimitExceededException) request.getAttribute(S2MultipartRequestHandler.SIZE_EXCEPTION_KEY);
		if(ex != null) {
			ActionMessages messages = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
			if (messages == null) {
				messages = new ActionMessages();
			}
			messages.add("file", new ActionMessage(SinaburoViewUtil.getMessage("errors.E048")));
			request.setAttribute(Globals.ERROR_KEY, messages);

			return "/admin/setting/imageFileRegist.html";
		}


		// トークン確認
		if (!TokenProcessor.getInstance().isTokenValid(request, false)) {

			return "/error/doubleAction.html";
		}

		String path = PropertyUtil.getProperty(SinaburoConstant.filePath.UPLOAD_TMP_FILE_PATH);

		if (imageFileRegistForm.file.getFileSize() == 0 || StringUtil.isBlank(imageFileRegistForm.file.getFileName())) {

			ActionMessages messages = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
			if (messages == null) {
				messages = new ActionMessages();
			}
			messages.add("file", new ActionMessage(SinaburoViewUtil.getMessage("errors.E027")));
			request.setAttribute(Globals.ERROR_KEY, messages);

			return "/admin/setting/imageFileRegist.html";

		}

		//一時保存
		String fileTmpPath = path + imageFileRegistForm.file.getFileName();
		OutputStream out = new BufferedOutputStream(new FileOutputStream(
				fileTmpPath));

		out.write(imageFileRegistForm.file.getFileData(), 0, imageFileRegistForm.file.getFileSize());
		out.close();

		//ファイル名チェック
		if (!fileNameCheck(imageFileRegistForm.file.getFileName())){
			//ファイル名NG
			ActionMessages messages = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
			if (messages == null) {
				messages = new ActionMessages();
			}
			messages.add("file", new ActionMessage(SinaburoViewUtil.getMessage("errors.E043")));
			request.setAttribute(Globals.ERROR_KEY, messages);

			return "/admin/setting/imageFileRegist.html";
		}

		//ファイル名を取得
		String filePath = PropertyUtil.getProperty(SinaburoConstant.filePath.IMG_UPLOAD_FILE_PATH) + imageFileRegistForm.file.getFileName();

		//ファイル存在チェック
		if (FileUtil.checkFileExists(filePath)){
			//ファイルが存在する
			ActionMessages messages = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
			if (messages == null) {
				messages = new ActionMessages();
			}
			messages.add("file", new ActionMessage(SinaburoViewUtil.getMessage("errors.E028")));
			request.setAttribute(Globals.ERROR_KEY, messages);

			return "/admin/setting/imageFileRegist.html";
		}

		//ファイルコピー
		FileUtil.copy(fileTmpPath, filePath);

		FileUtil.delete(fileTmpPath);

		FileManage fileManage = new FileManage();
		fileManage.fileName = imageFileRegistForm.file.getFileName();
		fileManage.memo = imageFileRegistForm.memo;
		fileManage.fileType = fileTypeCheck(imageFileRegistForm.file.getContentType());
		fileManage.uploadDate = SinaburoUtil.getTimestamp();

		fileManageService.inserFileManage(fileManage,getAdmin());

		TokenProcessor.getInstance().resetToken(request);

		imageFileSearch();

		return "/admin/setting/imageFileSearch.html";

	}

	private short fileTypeCheck(String realFileType){
		//ファイルタイプをチェックする
			if ("image/gif".equals(realFileType) || "image/jpeg".equals(realFileType) || "image/bmp".equals(realFileType) || "image/png".equals(realFileType) || "image/x-png".equals(realFileType) || "image/pjpeg".equals(realFileType)){
				//画像ファイル
				return 1;
			}else{
				//画像以外
				return 2;
			}
	}

	private boolean fileNameCheck(String fileName){
		//ファイルタイプをチェックする

		String[] filename_ar = fileName.split("\\.");
		int i;
		String name = "";
		for(i = 0; i < filename_ar.length - 1; i++){
			name += filename_ar[i];
		}

		return name.replace("-", "").replace("_", "").matches(SinaburoConstant.Regex.HANKAKU_E09);

	}

}