package com.seeyon.apps.bip.ctrl;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;

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

import com.seeyon.ctp.common.SystemEnvironment;
import org.springframework.web.servlet.ModelAndView;

import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.encrypt.CoderFactory;
import com.seeyon.ctp.common.filemanager.manager.FileManager;
import com.seeyon.ctp.common.po.filemanager.V3XFile;
import com.seeyon.ctp.util.Strings;
import com.seeyon.ctp.util.annotation.NeedlessCheckLogin;

public class LogDownloadController extends BaseController {


    //公文正文MimeType
    private static String docLongType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    private static String docType = "application/msword";
    private static String excelType = "application/vnd.ms-excel";

    private FileManager fileManager;

    public FileManager getFileManager() {
        return fileManager;
    }

    public void setFileManager(FileManager fileManager) {
        this.fileManager = fileManager;
    }

    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String date = request.getParameter("date");
        String num = request.getParameter("num");
        String name = request.getParameter("name");
        File file;
        String fileName = "";
        String logsFolder = SystemEnvironment.getLogsFolder();
        if (null == date) {
            fileName = logsFolder + name + ".log";
        } else {
            fileName = logsFolder + date + "/" + name + "." + date + "." + num + ".log";
        }
        file = new File(fileName);
        if(!file.exists()) {
            super.rendJavaScript(response, "alert('日志文件不存在');");
            return null;
        }
        response.setContentType("application/force-download");// 扢离Ч秶狟婥祥湖羲
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name + ".log", "UTF-8"));
        FileInputStream fin = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            fin = new FileInputStream(file);
            CoderFactory.getInstance().download(fin, os);
        } catch (Exception e) {
            throw e;
        } finally {
            if (fin != null) {
                fin.close();
            }
            if (os != null) {
                os.close();
            }
        }
        return null;
    }


}
