<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="weaver.file.ImageFileManager" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%

    String basePath = File.separator+"opt"+File.separator+"weaver"+File.separator+"ecology"+File.separator+"filesystem" ;
    User user = HrmUserVarify.getUser(request, response);
    if(user==null) {
        response.sendRedirect("/login/Login.jsp");
        return;
    }

    String docid = request.getParameter("docid");
    RecordSet rs = new RecordSet();

    String doccreatedate = "";
    String imagefileid = "";
    String imagefilename = "";
    if(StringUtils.isNotBlank(docid)){

        String sql = " select doccreatedate from docdetail where id = "+docid ;
        rs.executeQuery(sql);
        if(rs.next()){
            doccreatedate = Util.null2String(rs.getString("doccreatedate"));
        }

        sql = " select imagefileid from docimagefile where docid = "+docid;
        rs.executeQuery(sql);
        if(rs.next()){
            imagefileid = Util.null2String(rs.getString("imagefileid"));
        }


        String iszip = "";
        String filerealpath = "";

        if(StringUtils.isNotBlank(imagefileid)){
            sql = "select imagefilename,iszip,filerealpath from imagefile where imagefileid="+imagefileid ;
            rs.executeQuery(sql);
            if(rs.next()){
                imagefilename = Util.null2String(rs.getString("imagefilename"));
            }
        }

        String filepath = basePath+File.separator+"convertvideo"+File.separator+doccreatedate+File.separator+imagefileid;
        File path = new File(filepath) ;
        if(!path.exists()){
            path.mkdirs();
        }

        if(StringUtils.isNotBlank(imagefilename)){
            File videoPath = new File(filepath+File.separator+imagefilename) ;

            if(!videoPath.exists()){

                InputStream inputStream = ImageFileManager.getInputStreamById(Integer.parseInt(imagefileid));
                FileOutputStream fileOutputStream = new FileOutputStream(videoPath);
                int size = 0;
                int byteLength = 8196;
                byte[] buffer = new byte[byteLength];
                while ((size = inputStream.read(buffer,0,byteLength)) !=-1){
                    fileOutputStream.write(buffer,0,size);
                }

                fileOutputStream.close();
                inputStream.close();
            }
        }
    }

    String filepath = basePath+File.separator+"convertvideo"+File.separator+doccreatedate+File.separator+imagefileid;
    File videoPath = new File(filepath+File.separator+imagefilename) ;
    String code = "";
    if(videoPath.exists()){
        code = "200";
    }else{
        code = "500";
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("code",code);

%>

<%=jsonObject.toJSONString() %>
