package me.academeg.api.controller;

import me.academeg.api.common.ApiResult;
import me.academeg.api.entity.Account;
import me.academeg.api.entity.Image;
import me.academeg.api.exception.entity.AccountPermissionException;
import me.academeg.api.exception.entity.FileFormatException;
import me.academeg.api.service.AccountService;
import me.academeg.api.service.ImageService;
import me.academeg.api.utils.ApiUtils;
import me.academeg.api.utils.ImageUtils;
import me.academeg.api.exception.entity.ImageNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * ImageController Controller
 *
 * @author Yuriy A. Samsonov <y.samsonov@erpscan.com>
 * @version 1.0
 */
@RestController
@RequestMapping("/api/image")
@Validated
public class ImageController {

    private final static String IMAGE_PATH = "image/";

    private final ImageService imageService;
    private final AccountService accountService;

    @Autowired
    public ImageController(ImageService imageService, AccountService accountService) {
        this.imageService = imageService;
        this.accountService = accountService;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ApiResult create(@RequestParam(name = "image") final MultipartFile file) {
        if (!file.getContentType().startsWith("image/")) {
            throw new FileFormatException("You can upload only images");
        }

        File imagesDir = new File(IMAGE_PATH);
        if (!imagesDir.exists()) {
            imagesDir.mkdir();
        }

        Image image = new Image();
        String originalImageName = ImageUtils.saveImage(imagesDir, file);
        image.setOriginalPath(IMAGE_PATH + originalImageName);
        String thumbnailImageName = ImageUtils.compressImage(new File(imagesDir, originalImageName), imagesDir);
        image.setThumbnailPath(IMAGE_PATH + thumbnailImageName);

        return ApiUtils.singleResult(imageService.add(image));
    }

    @RequestMapping(value = "/{uuid}", method = RequestMethod.DELETE)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    public void delete(@AuthenticationPrincipal final User user, @PathVariable final UUID uuid) {
        Image imageFromDb = imageService.getByUuid(uuid);
        if (imageFromDb == null) {
            throw new ImageNotExistException();
        }

        Account authAcc = accountService.getByEmail(user.getUsername());
        if (imageFromDb.getArticle() == null) {
            return;
        }

        if (!imageFromDb.getArticle().getAuthor().getId().equals(authAcc.getId())) {
            throw new AccountPermissionException();
        }

        deleteImageFromStorage(imageFromDb);
        imageService.delete(imageFromDb);
    }

    @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "image/jpg")
    public byte[] getByName(@PathVariable final String name) {
        return ImageUtils.toByteArray(new File(IMAGE_PATH + name));
    }

    private void deleteImageFromStorage(Image image) {
        if (image == null) {
            return;
        }
        new File(image.getOriginalPath()).delete();
        new File(image.getThumbnailPath()).delete();
    }
}

@@ -30,16 +30,50 @@
 oller;%0A%0A
+import me.academeg.api.Constants;%0A
 import m
@@ -181,16 +181,74 @@
 .Image;%0A
+import me.academeg.api.exception.EntityNotExistException;%0A
 import m
@@ -360,32 +360,96 @@
 ormatException;%0A
+import me.academeg.api.exception.entity.ImageNotExistException;%0A
 import me.academ
@@ -608,72 +608,8 @@
 ls;%0A
-import me.academeg.api.exception.entity.ImageNotExistException;%0A
 impo
@@ -1263,64 +1263,8 @@
  %7B%0A%0A
-    private final static String IMAGE_PATH = %22image/%22;%0A%0A
     
@@ -1867,32 +1867,42 @@
 sDir = new File(
+Constants.
 IMAGE_PATH);%0A   
@@ -2110,24 +2110,34 @@
 riginalPath(
+Constants.
 IMAGE_PATH +
@@ -2301,16 +2301,26 @@
 ailPath(
+Constants.
 IMAGE_PA
@@ -2408,32 +2408,387 @@
 image));%0A    %7D%0A%0A
+    @RequestMapping(value = %22%7Buuid%7D%22, method = RequestMethod.GET)%0A    public ApiResult getById(@PathVariable final UUID uuid) %7B%0A        Image imageFromDb = imageService.getByUuid(uuid);%0A        if (imageFromDb == null) %7B%0A            throw new EntityNotExistException(%22Image not exist%22);%0A        %7D%0A        return ApiUtils.singleResult(imageFromDb);%0A    %7D%0A%0A
     @RequestMapp
@@ -3566,16 +3566,21 @@
 lue = %22/
+file/
 %7Bname%7D%22,
@@ -3742,16 +3742,26 @@
 ew File(
+Constants.
 IMAGE_PA

Refactor image controller