<%@page import="java.io.*" %>
<%@page import="javax.activation.MimetypesFileTypeMap"%>
<%@page import="com.csc.library.utilities.*"%>
<%@page import="com.csc.library.util.download.*"%>
<%@page import="com.csc.library.session.InitialEnvironment" %>

<!-- http://127.0.0.1:8082/hr/VIEW.jsp?type=PERSONAL_UPLOAD&dir=dir&subfolder=PICTURE&filename=035058.gif -->
<%!

	/**
	 * Returns the Mime Type of the file, depending on the extension of the filename
	 */
	static String getMimeType(String fName) {
		String mimeType;
		fName = fName.toLowerCase();
		
		if (fName.endsWith(".jpg") || fName.endsWith(".jpeg") || fName.endsWith(".jpe")) {
			mimeType = "image/jpeg";
		} 
		else if (fName.endsWith(".gif")) { 
			mimeType = "image/gif";
		} 
		else if (fName.endsWith(".png")) { 
			mimeType = "image/png";
		} 
		else if (fName.endsWith(".bmp")) { 
			mimeType = "image/bmp";
		} 

		else if (fName.endsWith(".swf")) { 
			mimeType = "application/x-shockwave-flash";
		} 


		else if (fName.endsWith(".pdf")) { 
			mimeType = "application/pdf";
		} 
		else if (fName.endsWith(".zip")) { 
			mimeType = "application/zip";
		} 
		else if (fName.endsWith(".rtf")) { 
			mimeType = "application/rtf";
		} 
		else if (fName.endsWith(".xl") || fName.endsWith(".xls") || fName.endsWith(".xlv") 
				|| fName.endsWith(".xla") || fName.endsWith(".xlb") || fName.endsWith(".xlt") 
				|| fName.endsWith(".xlm") || fName.endsWith(".xlk")) { 
			mimeType = "application/vnd.ms-excel";
		} 
		else if (fName.endsWith(".doc") || fName.endsWith(".dot")) { 
			mimeType = "application/vnd.ms-word";
		} 
		else if (fName.endsWith(".ppt")) { 
			mimeType = "application/vnd.ms-powerpoint";
		} 
		
		else if (fName.endsWith(".htm") || fName.endsWith(".html") || fName.endsWith(".shtml")) { 
			mimeType = "text/html";
		} 
		
		else if (fName.endsWith(".xml")) { 
			mimeType = "text/xml";
		} 
		
		else if (fName.endsWith(".mid") || fName.endsWith(".midi")) { 
			mimeType = "audio/x-midi";
		} 
		else if (fName.endsWith(".mp3")) { 
			mimeType = "audio/mp3";
		} 
		else if (fName.endsWith(".ogg")) { 
			mimeType = "audio/ogg";
		}
		
		else if (fName.endsWith(".avi")) { 
			mimeType = "video/x-msvideo";
		} 
		else if (fName.endsWith(".mov") || fName.endsWith(".qt")) { 
			mimeType = "video/quicktime";
		}
		else if (fName.endsWith(".mpg") || fName.endsWith(".mpeg") || fName.endsWith(".mpe")) { 
			mimeType = "video/mpeg";
		} 

		else {
			mimeType = "text/plain";
		}
		
		return mimeType;
		
	}

%>
<%
	CheckNull chkNull = new CheckNull();
	String type = chkNull.chkNullString( request.getParameter("type") );
	String dir = chkNull.chkNullString( request.getParameter("dir")) ;
	String filename = chkNull.chkNullString( request.getParameter("filename") );
	
	if( type!=null && dir!=null && filename!=null ){
		InitialEnvironment env = new InitialEnvironment("GLOBAL");
		String upload_dir = env.getValue( type +"-" +dir );
		String file_dir = upload_dir +chkNull.chkNullString( request.getParameter("subfolder") );
		String default_filename = chkNull.chkNullString( request.getParameter("default") );
		
		if(default_filename.equals("")) {
			default_filename = chkNull.chkNullString( env.getValue( type +"-default" ) );
		}

		
		try{
			File file = new File( file_dir, filename );
			// check file
			if ( !file.isFile() || !file.exists() || !file.canRead()) {
				if( !default_filename.equals("")) {
					System.out.println("path=" +file.getCanonicalFile());
					System.out.println( "file=" +file.isFile() +", exists=" +file.exists() +", canRead=" +file.canRead());
					System.out.println("Error : Using default file: " +default_filename );

					file = new File( file_dir, default_filename );
				}
			}
			String mimeType = getMimeType( file.getName() );

			System.out.println("path=" +file.getCanonicalFile());
			System.out.println("mimeType=" +mimeType +", file=" +file.isFile() +", exists=" +file.exists() +", canRead=" +file.canRead());
			
			response.setContentType(mimeType);
			response.setHeader("Content-Disposition", "inline;filename=\"" + file.getName() + "\"");
			response.setContentLength((int) file.length());
			
			 BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(file));
             byte buffer[] = new byte[8 * 1024];
             out.clearBuffer();
             OutputStream out_s = response.getOutputStream();
	         int b;
			 while ((b = fileInput.read(buffer)) != -1)
			 	 out_s.write(buffer, 0, b);

			 out_s.flush();
			 fileInput.close();
             
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

%>