<commit_before>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 <NAME> <<EMAIL>>
 * @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();
    }
}<commit_msg>Refactor image controller<commit_after>package me.academeg.api.controller;

import me.academeg.api.Constants;
import me.academeg.api.common.ApiResult;
import me.academeg.api.entity.Account;
import me.academeg.api.entity.Image;
import me.academeg.api.exception.EntityNotExistException;
import me.academeg.api.exception.entity.AccountPermissionException;
import me.academeg.api.exception.entity.FileFormatException;
import me.academeg.api.exception.entity.ImageNotExistException;
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 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 <NAME> <<EMAIL>>
 * @version 1.0
 */
@RestController
@RequestMapping("/api/image")
@Validated
public class ImageController {

    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(Constants.IMAGE_PATH);
        if (!imagesDir.exists()) {
            imagesDir.mkdir();
        }

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

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

    @RequestMapping(value = "{uuid}", method = RequestMethod.GET)
    public ApiResult getById(@PathVariable final UUID uuid) {
        Image imageFromDb = imageService.getByUuid(uuid);
        if (imageFromDb == null) {
            throw new EntityNotExistException("Image not exist");
        }
        return ApiUtils.singleResult(imageFromDb);
    }

    @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 = "/file/{name}", method = RequestMethod.GET, produces = "image/jpg")
    public byte[] getByName(@PathVariable final String name) {
        return ImageUtils.toByteArray(new File(Constants.IMAGE_PATH + name));
    }

    private void deleteImageFromStorage(Image image) {
        if (image == null) {
            return;
        }
        new File(image.getOriginalPath()).delete();
        new File(image.getThumbnailPath()).delete();
    }
}
{'fraction_non_alphanumeric': 0.10823250920568123, 'fraction_numerical': 0.0005260389268805891, 'mean_word_length': 3.732420659614188, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016303', 'n_tokens_mistral': 2071, 'n_tokens_neox': 2058, 'n_words': 403}