package com.iamberry.app.api.controller;

import static com.iamberry.app.config.Response.SUCCESS;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.iamberry.app.config.Response;
import com.iamberry.app.config.ResponseHeader;
import com.iamberry.wechat.file.FileUtils;
import com.iamberry.wechat.file.UploadResultBean;

/**
 * 接受APP报错日志
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年12月21日
 */
@Controller
@RequestMapping("/log")
public class AppLogController {

	private static String SAVE_MILK_IMAGE_PATH = "/log/";
	
	/**
	 * 防止上传木马文件，需要屏蔽web访问的功能
	 * @param response
	 * @author 献
	 * @throws IOException 
	 * @Time   2016年12月21日
	 */
	@RequestMapping(value = "/{filename}")
	public void get(HttpServletResponse response) throws IOException {
		response.sendRedirect("http://www.iamberry.com");
	}
	
	/**
	 * 防止上传木马文件，需要屏蔽web访问的功能
	 * @param response
	 * @author 献
	 * @throws IOException 
	 * @Time   2016年12月21日
	 */
	@RequestMapping(value = "/{path}/{filename}")
	public void post(HttpServletResponse response) throws IOException {
		response.sendRedirect("http://www.iamberry.com");
	}
	
	/**
	 * @param model
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/upload_error_file", method = RequestMethod.POST)
	public Response uploadFileImage(@RequestParam("file")MultipartFile file, HttpServletRequest request) {
		// 保存文件
		UploadResultBean bean = FileUtils.saveFile(request.getServletContext(), file, SAVE_MILK_IMAGE_PATH, true);
		if (bean == null) {
			// 文件有误
			return new Response(new ResponseHeader(4204, "文件有误!", -1));
		}
		return SUCCESS.setData(null);
	}
}
