package sgc.portal.cd.cd0301.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;

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

import org.springframework.beans.factory.annotation.Autowired;
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 sgc.portal.SgcPortalProperties;

@Controller
public class CDDownloadController {
    
    public CDDownloadController() {
		super();
		// TODO Auto-generated constructor stub
	}

    @Autowired
    private SgcPortalProperties props;
    
    /**
     * cKeditor 서버로 전송된 이미지 뿌려주기
     * @param uid
     * @param fileName
     * @param request
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //
    @RequestMapping(value="/cd/ckImgSubmit.do" , method={RequestMethod.GET, RequestMethod.POST})
    public void ckSubmit(@RequestParam(value="subDir") String subDir
                            , @RequestParam(value="fileName") String fileName
                            , HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        
    	//C:\SGC\runtime\websquare_home\config\websquare.xml의 <baseDir value="C:/SGC/runtime/websquare_home/upload"/> 경로를 바라봄
    	//아래 props는 /resources/application.yml의 
    	//             web-square:
    	//               home-location: C:\SGC\runtime\websquare_home속성을 가져옴.
    	
    	String path = props.getWebSquare().getHomeLocation() + "/upload/";
    	
    	response.setContentType("text/html; charset=UTF-8");

    	fileName = URLDecoder.decode(fileName, "UTF-8");
    	
        String sDirPath = path + fileName;
    
        File imgFile = new File(sDirPath); 
        
        //사진 이미지 찾지 못하는 경우 예외처리로 빈 이미지 파일을 설정한다.
        if(imgFile.isFile()){
            byte[] buf = new byte[1024];
            int readByte = 0;
            int length = 0;
            byte[] imgBuf = null;
            
            FileInputStream fileInputStream = null;
            ByteArrayOutputStream outputStream = null;
            ServletOutputStream out = null;
            
            try{
                fileInputStream = new FileInputStream(imgFile);
                outputStream = new ByteArrayOutputStream();
                out = response.getOutputStream();
                
                while((readByte = fileInputStream.read(buf)) != -1){
                    outputStream.write(buf, 0, readByte);
                }
                
                imgBuf = outputStream.toByteArray();
                length = imgBuf.length;
                out.write(imgBuf, 0, length);
                out.flush();
                
            }catch(IOException e){
            	e.printStackTrace();
            }finally {
                outputStream.close();
                fileInputStream.close();
                out.close();
            }
        }
    }

}
