package org.xcolab.view.files;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import org.xcolab.client.files.FilesClient;
import org.xcolab.client.files.pojo.FileEntry;
import org.xcolab.view.util.entity.upload.FileUploadUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@PropertySource({"file:${user.home}/.xcolab.application.properties"})
public class FileUploadController {

    private static final int IMAGE_CROP_WIDTH_PIXELS = 300;
    private static final int IMAGE_CROP_HEIGHT_PIXELS = 300;

    private final String fileUploadPath;

    @Autowired
    public FileUploadController(Environment env) {
        fileUploadPath = env.getProperty("files.upload.dir");
    }

    @PostMapping("/image/upload")
    public ImageResponse singleFileUpload(@RequestParam("file") MultipartFile file,
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(required = false) Boolean resize) {

        if (request.getParameter("resize") == null) {
            resize = false;
        }
        return uploadImageResponse(file, request, resize);
    }

    private ImageResponse uploadImageResponse(MultipartFile file, HttpServletRequest request,
            Boolean resize) {
        try {
            String path = request.getSession().getServletContext().getRealPath("/");

            path = (fileUploadPath != null) ? (fileUploadPath) : (path);

            byte[] bytes = file.getBytes();

            if (resize != null) {
                bytes = FileUploadUtil
                        .resizeAndCropImage(ImageIO.read(new ByteArrayInputStream(bytes)),
                                IMAGE_CROP_WIDTH_PIXELS, IMAGE_CROP_HEIGHT_PIXELS);
            }

            FileEntry fileEntry = new FileEntry();
            fileEntry.setCreateDate(new Timestamp(new Date().getTime()));
            String nameExt = file.getOriginalFilename();
            fileEntry.setFileEntryExtension((FilenameUtils.getExtension(nameExt)).toLowerCase());
            fileEntry.setFileEntrySize(bytes.length);
            fileEntry.setFileEntryName(FilenameUtils.getName(nameExt));

            fileEntry = FilesClient.createFileEntry(fileEntry, bytes, path);

            final String imageIdString = String.valueOf(fileEntry.getFileEntryId());
            return new ImageResponse(imageIdString, fileEntry.getLinkUrl(), true, "");
        } catch (IOException e) {
            return new ImageResponse(null, null, false, e.getMessage());
        }
    }

    @PostMapping("/image/uploadCkEditor")
    public void singleCKEditorUpload(@RequestParam("upload") MultipartFile file,
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(required = false, name = "CKEditorFuncNum") String ckEditorFuncNum,
            @RequestParam(required = false) Boolean resize) {

        if (request.getParameter("resize") == null) {
            resize = false;
        }
        ImageResponse ir = uploadImageResponse(file, request, resize);
        try {
            response.setContentType("text/html");
            response.getOutputStream()
                    .write(("<script>window.parent.CKEDITOR.tools.callFunction(" + ckEditorFuncNum
                            + ", \"" + ir.getImageUrl() + "\");</script>").getBytes());
        } catch (IOException ignored) {

        }
    }

    private static class ImageResponse {

        private final String imageId;
        private final String imageUrl;
        private final boolean success;
        private final String message;

        private ImageResponse(String imageId, String imageUrl, boolean success, String message) {
            this.imageId = imageId;
            this.imageUrl = imageUrl;
            this.success = success;
            this.message = message;
        }

        public String getImageId() {
            return imageId;
        }

        public String getImageUrl() {
            return imageUrl;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }
    }
}

@@ -73,182 +73,8 @@
 ls;%0A
-import org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.context.annotation.PropertySource;%0Aimport org.springframework.core.env.Environment;%0A
 impo
@@ -129,16 +129,16 @@
 apping;%0A
+
 import o
@@ -306,24 +306,97 @@
 ipartFile;%0A%0A
+import org.xcolab.client.admin.attributes.platform.PlatformAttributeKey;%0A
 import org.x
@@ -793,78 +793,8 @@
 ler%0A
-@PropertySource(%7B%22file:$%7Buser.home%7D/.xcolab.application.properties%22%7D)%0A
 publ
@@ -991,144 +991,55 @@
 Path
-;%0A%0A    @Autowired%0A    public FileUploadController(Environment env) %7B%0A        fileUploadPath = env.getProperty(%22files.upload.dir%22);%0A    %7D
+ = PlatformAttributeKey.FILES_UPLOAD_DIR.get();
 %0A%0A  

Switch to use PlatformAttributes instead of PropertySource