package ogloszenia.serwlet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ogloszenia.exn.NieznanyRekord;
import ogloszenia.util.FotoUtil;
import ogloszenia.util.Ustawienia;

@WebServlet("/foto")
public class Foto extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		try {
			byte[] bajty = FotoUtil.wczytajFoto(Integer.parseInt(id));
			response.setContentType("image/jpeg");
			try(ServletOutputStream out = response.getOutputStream()) {
				out.write(bajty);
			}
		} catch (NumberFormatException e) {
			response.setStatus(500);
			response.setContentType("text/plain");
			response.getWriter().append(e.toString());
		} catch (NieznanyRekord e) {
			response.setStatus(404);
			response.setContentType("text/plain");
			response.getWriter().append(e.toString());			
		}
	}

}
