package com.bocloud.ims.booter.controller.resource.compute;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

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

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.ims.common.config.StorageConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Created by hufy on 2017/9/8.
 */
@Api(value = "文件下载", tags = "文件下载")
@RequestMapping("/files")
@Controller
public class FileDownloadController {
	Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	StorageConfig storageConfig;

	/**
	 * 读取文件
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "读取文件信息",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "文件上传下载", operation = "读取文件信息")
	@RequestMapping(value = "/download", method = { RequestMethod.GET })
	public void download(HttpServletRequest request, HttpServletResponse response) {
		String filePath = null;
		try {
			filePath = URLDecoder.decode(request.getParameter("filePath"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("解析失败", e);
			return;
		}

		String path = storageConfig.getPath();
		String finalPath = path + filePath;
		File file = new File(finalPath);
		response.setContentLength((int) file.length());
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			IOUtils.copy(fileInputStream, response.getOutputStream(), 8 * 1000 * 1000);
		} catch (FileNotFoundException e) {
			logger.error("读取文件失败", e);
		} catch (IOException e) {
			logger.error("io流异常", e);
		} finally {
			try {
				if (fileInputStream != null) {
					fileInputStream.close();
				}
			} catch (IOException e) {
				logger.error("流关闭失败", e);
			}
		}

	}

	/**
	 *
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "移除下载文件",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "文件上传下载", operation = "移除下载文件")
	@RequestMapping(value = "/removeDownload", method = { RequestMethod.GET })
	public void removeDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String filePath = null;
		try {
			filePath = URLDecoder.decode(request.getParameter("filePath"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("解析失败", e);
			return;
		}
		String path = storageConfig.getPath();
		String finalPath = path + filePath;
		File file = new File(finalPath);
		boolean delete = false;
		try {
			delete = file.delete();
		} catch (Exception e) {
			logger.error("文件删除失败" + file.getAbsolutePath(), e);
		}
		PrintWriter writer = response.getWriter();
		writer.write(JSONObject.toJSONString(new BocloudResult(delete, "")));
	}
}
