<%@page contentType="text/html;  charset=UTF-8;" pageEncoding="UTF-8"  %>

<%@page import="java.io.ByteArrayInputStream" %>
<%@page import="java.io.ByteArrayOutputStream" %>
<%@page import="java.io.*" %>
<%@page import="java.io.IOException" %>
<%@page import="java.io.InputStream" %>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@page import="org.apache.commons.fileupload.FileItem" %>
<%@page import="com.csc.library.session.*" %>
<%@page import="com.csc.library.utilities.*,com.csc.library.database.DataSet,java.util.*" %>
<%@page import="com.csc.library.database.CscConnection" %>
<%@page import="com.csc.library.zkprocess.*" %>
<%@page import="com.csc.library.fputility.*" %>
<%@page import="com.google.gson.Gson" %>
<%@page import="com.google.gson.GsonBuilder" %>
<%@page import="com.csc.library.util.download.*"%>

<%

	try {
		System.out.print("!! Load emp picture has been called !!");
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);

		if(isMultipart){
			System.out.print("!! Multipart data detected [sv_getmenu]!!");
			// Create a factory for disk-based file items
			FileItemFactory factory = new DiskFileItemFactory();

			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);

			// Parse the request
			List /* FileItem */ items = upload.parseRequest(request);

			// Process the uploaded items
			Iterator iter = items.iterator();
			HashMap dataitem = new HashMap();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();

				if (item.isFormField()) {
					String name = item.getFieldName();
					String value = item.getString();
					dataitem.put(name, value);
					System.out.print("!! Field name : "+ name + " value " + value);
				} else {
					System.out.print("!! File upload detected !!");
					dataitem.put("attchfile", item);
				}
			}

			/* save detail */
			
			long curtime = System.currentTimeMillis();

			if(dataitem.containsKey("data")){
				try {
					//System.out.print("input upd emp info --> " + (String) dataitem.get("data"));
					/* testing process will be sent in normal http get */
					String str = (String) dataitem.get("data") ;
					ZKPackageBase zkpack = new ZKPackageBase(str);
										
					SV_GetEmpPicture getdev = new SV_GetEmpPicture(zkpack);
					getdev.run();



				}catch(Exception e){
					System.out.print(e.getMessage());
				}
			}

			if(dataitem.containsKey("attchfile")){
				// leave blank
			}
		}else{
			/* testing process will be sent in normal http get */
			/*
			String str = "y4jsIdLJ5lFcHX05+91LzwNn62ePhKPco0ibyweOmZ2t63Gai3m3pr+0Wx8VlZ7ky7RF66xDjYYKaGQ0j6s3DA==";
			
			MyFPSecurity mysec = new MyFPSecurity();
			out.print(mysec.decrypt(str, "UTF-8"));*/

			/*
			String str = "username:myhradmin,password:Zmks094xWGx,picture:4F36B16NJU4.jpg";
			MyFPSecurity mysec = new MyFPSecurity();
			out.print(mysec.encrypt(str, "UTF-8"));*/

			
			//out.print("<br>!! TEST LOAD EMPLOYEE PIC INFO!!<br><br>");
			//String str = "y4jsIdLJ5lFcHX05+91LzwNn62ePhKPco0ibyweOmZ2t63Gai3m3pr+0Wx8VlZ7ky7RF66xDjYYKaGQ0j6s3DA==";
			String str = "username:myhradmin,password:Zmks094xWGx,picture:4F36B16NJU4.jpg";
			ZKPackageBase zkpack = new ZKPackageBase(str);
					
			SV_GetEmpPicture getdev = new SV_GetEmpPicture(zkpack);
			getdev.run();
			
			ThaiUtilities thai = new ThaiUtilities();

			//out.print("-->" + zkpack.isAuth_status());
			if(zkpack.isAuth_status()){
				String filePath =  new InitialEnvironment("GLOBAL").getValue("PERSONAL_UPLOAD-dir") +"/PICTURE/"+zkpack.getPicture();
				System.out.print("!! emp file : " + filePath);
				File f = new File(thai.ASCII2Unicode(filePath));
				String fileName = f.getName();
				String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
				if ( f.exists() && f.canRead() ) {			
					response.setContentType("application/octet-stream");
					response.setHeader("Content-Disposition", "attachment; filename="+thai.Unicode2ASCII(f.getName()));
					//response.setHeader("cache-control", "no-cache");
					response.setContentLength((int) f.length());
					BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
					byte buffer[] = new byte[8 * 1024];
					out.clearBuffer();
					OutputStream out_s = response.getOutputStream();
					CopyStreams.copyStreamsWithoutClose(fileInput, out_s, buffer);
					out_s.flush();
					out_s.close();
					fileInput.close();
				}
			}
		}
	}catch(Exception e){
		System.out.print("xxx"+e.getMessage());
	}

	//out.print(recdata.getPackage());
	
%>