package com.itstherules.mediacentre.controller;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.itstherules.image.AWTImage;
import com.itstherules.image.IImage;
import com.itstherules.mediacentre.PathResolver;
import com.itstherules.stream.model.ItemsModel;
public class PhotosController extends BaseController {
private static final String EXTENSIONS = ".jpg,.jpeg,.png";
@Override
public void list(Map<String, Object> parameters, HttpServletResponse response) throws IOException {
ItemsModel itemsModel = new ItemsModel("photos", (String) parameters.get("directory"), EXTENSIONS);
parameters.put("items", itemsModel.asList());
merge("photos/list.freemarker", parameters, response.getWriter());
}
@Override
public void show(Map<String, Object> parameters, HttpServletResponse response) throws IOException {
merge("photos/show.freemarker", parameters, response.getWriter());
}
@Override
public void update(Map<String, Object> parameters, HttpServletResponse response) throws IOException {
String directory = (String) parameters.get("directory");
String imageName = (String) parameters.get("image");
IImage image = new AWTImage(PathResolver.resolve("photos/"+directory+"/"+imageName, PhotosController.class));
image.rotate(getIntParameter(parameters.get("rotation")));
image.scale(getIntParameter(parameters.get("width")), getIntParameter(parameters.get("height")), true, true);
image.save();
try {
IImage thumbnailImage = new AWTImage(PathResolver.resolve("photos/"+directory+"/thumbnails/"+imageName, PhotosController.class));
thumbnailImage.rotate(getIntParameter(parameters.get("rotation")));
thumbnailImage.save();
} catch(FileNotFoundException e){
}
PrintWriter writer = response.getWriter();
writer.write("saved=true");
writer.close();
}
private int getIntParameter(Object object) {
return Integer.parseInt((String) object);
}
}