package com.angkorteam.pluggable.framework.service;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javaxt.io.Image;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import com.angkorteam.pluggable.framework.FrameworkConstants;
import com.angkorteam.pluggable.framework.core.AbstractWebApplication;
import com.angkorteam.pluggable.framework.doc.ApiMethod;
import com.angkorteam.pluggable.framework.doc.ApiParam;
import com.angkorteam.pluggable.framework.rest.Controller;
import com.angkorteam.pluggable.framework.rest.RequestMapping;
import com.angkorteam.pluggable.framework.rest.RequestMethod;
import com.angkorteam.pluggable.framework.rest.Result;

@Controller
public class ResourceController {

    public static final String FILE = "/file";

    public static final String IMAGE = "/image";

    @ApiMethod(description = "file download")
    @RequestMapping(value = FILE, method = RequestMethod.GET)
    public Result<byte[]> file(AbstractWebApplication application,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String filename = request.getParameter("filename");

        File cache = new File(FileUtils.getTempDirectory(), filename);
        File file = null;
        if (cache.exists()) {
            file = cache;
        } else {
            String repository = application.select(FrameworkConstants.LOCAL,
                    String.class);
            File local = new File(repository, filename);
            FileUtils.copyFile(local, cache);
            file = cache;
        }
        return Result.ok(file, request, response);
    }

    @ApiMethod(description = "image download", requestParameters = { @ApiParam(name = "ratio", description = "keep ratio", type = Boolean.class) })
    @RequestMapping(value = IMAGE, method = RequestMethod.GET)
    public Result<byte[]> image(AbstractWebApplication application,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String filename = request.getParameter("filename");

        boolean ratio = true;
        if (request.getParameter("ratio") != null
                && request.getParameter("ratio") != null) {
            ratio = Boolean.valueOf(request.getParameter("ratio"));
        }

        File cache = new File(FileUtils.getTempDirectory(), filename);
        File file = null;
        if (cache.exists()) {
            file = cache;
        } else {
            String extension = FilenameUtils.getExtension(filename);
            String basename = FilenameUtils.getBaseName(filename);
            String repository = application.select(FrameworkConstants.LOCAL,
                    String.class);
            File local = new File(repository, filename);
            if (local.exists()) {
                FileUtils.copyFile(local, cache);
                file = cache;
            } else {
                int i = basename.lastIndexOf('_');
                String original = basename.substring(0, i);
                int height = Integer.valueOf(basename.substring(i + 1));
                i = original.lastIndexOf('_');
                int width = Integer.valueOf(original.substring(i + 1));
                original = original.substring(0, i) + "." + extension;
                local = new File(repository, original);
                if (local.exists()) {
                    Image image = new Image(local);
                    image.resize(width, height, ratio);
                    image.saveAs(cache);
                    file = cache;
                }
            }
        }
        return Result.ok(file, request, response);
    }

}
@@ -974,16 +974,108 @@
 ownload%22
+, requestParameters = %7B @ApiParam(name = %22filename%22, required = true, type = String.class) %7D
 )%0A    @R
@@ -1900,32 +1900,44 @@
 stParameters = %7B
+%0A           
  @ApiParam(name 
@@ -1992,24 +1992,104 @@
 olean.class)
+,%0A            @ApiParam(name = %22filename%22, required = true, type = String.class)
  %7D)%0A    @Req

add filename param to file download and image download